为 CDC 配置 AlloyDB for PostgreSQL 数据库

本页面介绍了如何配置变更数据捕获 (CDC),以将数据从 AlloyDB for PostgreSQL 数据库流式传输到支持的目标位置,例如 BigQuery 或 Cloud Storage。

在 Datastream 和 AlloyDB 之间建立连接

如需将 Datastream 连接到 AlloyDB 实例,您需要在使用方项目中设置 TCP 代理。TCP 代理映像已面向所有客户公开提供。

  1. 启动代理容器:

    1. 在使用方项目中激活 Cloud Shell。
    2. 运行以下脚本以创建 TCP 代理并停用网桥网络,从而避免连接问题:
    gcloud compute instances create-with-container \
      --zone=REGION_ID VM_NAME \
      --container-image gcr.io/dms-images/tcp-proxy \
      --tags=dms-tcp-proxy \
      --container-env=SOURCE_CONFIG=ALLOYDB_IP:ALLOYDB_PORT \
      --can-ip-forward \
      --network=SOURCE_AND_DEST_VPC \
      --machine-type=VM_TIER \
      --metadata=startup-script='#! /bin/bash
      mkdir -p /etc/docker
      cat <<EOF > /etc/docker/daemon.json
      {"bridge":"none"}
      EOF
      systemctl restart docker'
    

    替换以下内容:

    • REGION_ID:您要在其中创建 TCP 代理的区域。
    • VM_NAME:虚拟机的名称。
    • ALLOYDB_IP:AlloyDB for PostgreSQL 实例的 IP 地址。
    • ALLOYDB_PORT:AlloyDB for PostgreSQL 实例的端口号。
    • SOURCE_AND_DEST_VPC:来源和目的地所连接到的 VPC 网络。
    • VM_TIER:虚拟机的类型。

    包含更新后的参数的示例命令:

    gcloud compute instances create-with-container \
      --zone=us-central1-c ds-tcp-proxy \
      --container-image gcr.io/dms-images/tcp-proxy \
      --tags=ds-tcp-proxy \
      --container-env=SOURCE_CONFIG=10.16.0.5:5432 \
      --can-ip-forward \
      --network=default \
      --machine-type=e2-micro
      --metadata=startup-script='#! /bin/bash
      mkdir -p /etc/docker
      cat <<EOF > /etc/docker/daemon.json
      {"bridge":"none"}
      EOF
      systemctl restart docker'
    
  2. 输入以下内容,运行脚本以输出 TCP 代理的 IP 地址:

    gcloud compute instances describe VM_NAME \
      --format="yaml(networkInterfaces[].networkIP)" \
      --zone=REGION_ID
    

    替换以下内容:

    • VM_NAME:虚拟机的名称。
    • REGION_ID:您在其中创建 TCP 代理的区域。
  3. (可选)创建防火墙规则来限制 TCP 代理的入站流量:

    gcloud compute firewall-rules create FIREWALL_RULE_NAME \
      --direction=INGRESS \
      --priority=1000 \
      --target-tags=dms-tcp-proxy \
      --network=SOURCE_VPC \
      --action=ALLOW \
      --rules=tcp:ALLOYDB_PORT \
      --source-ranges=IP_RANGE
    

    替换以下内容:

    • FIREWALL_RULE_NAME:防火墙规则的名称。
    • SOURCE_VPC:来源所连接到的 VPC 网络。
    • ALLOYDB_PORT:AlloyDB for PostgreSQL 实例的端口号。