本页面上的代码段是您可以用作模型的示例,您可以将其中的值替换为用于 AlloyDB Omni 资源的值。
准备工作
- 在 Kubernetes 上安装 Omni Operator。
- 确保您已创建复制槽、发布方集群和发布内容。如需了解详情,请参阅创建复制槽和发布内容。
创建和配置订阅方集群
- 创建订阅方集群。 - $ cat << EOF | kubectl apply -f - apiVersion: v1 kind: Secret metadata: name: db-pw-DB_CLUSTER_NAME type: Opaque data: DB_CLUSTER_NAME: "ENCODED_PASSWORD" # Password is odspassword --- apiVersion: alloydbomni.dbadmin.goog/v1 kind: DBCluster metadata: name: subscriber spec: databaseVersion: "15.7.0" primarySpec: adminUser: passwordRef: name: db-pw-DB_CLUSTER_NAME resources: memory: MEMORY_SIZE cpu: CPU_COUNT disks: - name: DataDisk size: DISK_SIZE EOF- 替换以下内容: - DB_CLUSTER_NAME:此数据库集群的名称,例如- subscriber-cluster。
- ENCODED_PASSWORD:默认- postgres用户角色的数据库登录密码(以 base64 字符串编码),例如- Q2hhbmdlTWUxMjM=表示- ChangeMe123。
- CPU_COUNT:此数据库集群中的每个数据库实例可用的 CPU 数量。
- MEMORY_SIZE:此数据库集群的每个数据库实例的内存量。建议将此值设置为每个 CPU 8 千兆字节。例如,如果您之前在此清单中将- cpu设置为- 2,则建议将- memory设置为- 16Gi。
- DISK_SIZE:每个数据库实例的磁盘大小,例如- 10Gi。
 
- 找到所需的 Pod。 - $ kubectl get pod -l "alloydbomni.internal.dbadmin.goog/dbcluster=DB_CLUSTER_NAME, alloydbomni.internal.dbadmin.goog/task-type=database, dbs.internal.dbadmin.goog/ha-role=Primary"
- 登录订阅方集群数据库 Pod。 - $ kubectl get pod NAME READY STATUS RESTARTS AGE al-2bce-publisher-0 3/3 Running 0 20h $ kubectl exec -ti SUBSCRIBER_POD_NAME -- /bin/bash Defaulted container "database" out of: database, logrotate-agent, memoryagent, dbinit (init) postgres@al-3513-subscriber-0:/$- 将 - SUBSCRIBER_POD_NAME替换为订阅方 Pod 的名称。
- 找到发布方 DBcluster 上的负载均衡器的 IP 地址,例如 - 10.116.14.190- $ kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE al-publisher-rw-ilb ClusterIP 10.116.14.190 <none> 5432/TCP 21h
- 从发布方获取架构备份,作为发布方数据库中已发布数据的初始副本。逻辑复制不支持 DDL 复制。 在开始逻辑复制之前,您计划复制的架构或表必须存在于目标位置(订阅方集群)中。 - postgres@al-3513-subscriber-0:/$ pg_dump -h PUBLISHER_IP_ADDRESS -U postgres --create --schema-only customer > /tmp/customer.schema-only.sql- 将 - PUBLISHER_IP_ADDRESS替换为发布方 DBcluster 上的负载均衡器的 IP 地址。
- 在订阅方数据库中应用备份。 - postgres@al-3513-subscriber-0:/$ psql -h localhost -U postgres < /tmp/customer.schema-only.sql
- 可选:验证表中是否没有数据。 - # There is no data in table company customer=# select * from company; id | name | age | salary ----+------+-----+-------- (0 rows)
- 为数据库创建订阅。确保已在发布方 DBCluster 上创建发布内容。 - postgres@al-3513-subscriber-0:/$ CREATE SUBSCRIPTION sub_customer CONNECTION 'host=PUBLISHER_IP_ADDRESS port=5432 user=REPLICATION_USER dbname=DATABASE_NAME password=PUBLISHER_CLUSTER_PASSWORD sslmode=require' PUBLICATION PUBLICATION_NAME WITH (slot_name='REPLICATION_SLOT_NAME');- 替换以下内容: - REPLICATION_USER:连接到复制槽的用户的名称。
- DATABASE_NAME:设置为要从复制槽流式传输其更改的数据库的名称。
- PUBLISHER_CLUSTER_PASSWORD:发布方 DBCluster 的 postgres用户的数据库登录密码。
- PUBLICATION_NAME:订阅方订阅的发布内容名称。
- REPLICATION_SLOT_NAME:在发布方 DBCluster 上创建的复制槽的名称。
 
- 可选:验证订阅方集群上的复制。 - postgres@al-3513-subscriber-0:/$ psql -h localhost -U postgres DATABASE_NAME customer=# select * from public.company; id | name | age | salary ----+-------+-----+-------- 1 | Quinn | 25 | 65000 2 | Kim | 22 | 72250 3 | Bola | 31 | 53000 4 | Sasha | 33 | 105000 5 | Yuri | 27 | 85000 (5 rows)
- 在发布方集群中,向表中添加一行。 - # On the publisher database $ kubectl exec -ti al-2bce-publisher-0 -- /bin/bash Defaulted container "database" out of: database, logrotate-agent, memoryagent, dbinit (init) postgres@al-2bce-publisher-0:/$ psql -h localhost -U postgres DATABASE_NAME customer=# insert into TABLE_NAME (id, name, age, salary) values (6, 'Alex', 39, 100000);- 将 TABLE_NAME 替换为相应表在订阅方订阅的发布方 DBCluster 中的名称。 
- 在订阅方集群中,验证向发布方集群中的表添加的行是否已复制到订阅方集群中的表。 - # On the subscriber database, data is synced. postgres@al-3513-subscriber-0:/$ psql -h localhost -U postgres DATABASE_NAME customer=# select * from TABLE_NAME; id | name | age | salary ----+-------+-----+-------- 1 | Quinn | 25 | 65000 2 | Kim | 22 | 72250 3 | Bola | 31 | 53000 4 | Sasha | 33 | 105000 5 | Yuri | 27 | 85000 6 | Alex | 39 | 100000 (6 rows)
手动创建其他表
与 pglogical 中的 replicate_ddl_command 不同,逻辑复制不会自动同步 DDL 更改。开源工具 pgl_ddl_deploy 提供了一种解决方案,不过您也可以在订阅方上手动执行 DDL 命令。
- 为说明这一点,请在发布方集群的 - customer数据库中创建一个名为- finance的新表。- # On the publisher database $ kubectl exec -ti al-2bce-publisher-0 -- /bin/bash Defaulted container "database" out of: database, logrotate-agent, memoryagent, dbinit (init) postgres@al-2bce-publisher-0:/$ psql -h localhost -U postgres customer customer=# create table finance (row text); CREATE TABLE customer=# insert into finance values ('critical data'); INSERT 0 1 customer=# ALTER PUBLICATION pub_customer ADD TABLE finance; ALTER PUBLICATION
- 向发布方集群添加新表时,您需要在订阅方中手动应用 DDL(表创建),然后在订阅方集群中运行以下命令来验证复制。 - postgres@al-3513-subscriber-0:/$ psql -h localhost -U postgres customer customer=# create table finance (row text); CREATE TABLE customer=# ALTER SUBSCRIPTION sub_customer REFRESH PUBLICATION; ALTER SUBSCRIPTION customer=# select * from finance; row --------------- critical data (1 row)