本文提供範例,說明如何建立及設定發布端叢集和訂閱端叢集。閱讀本文前,請先熟悉 AlloyDB Omni 總覽。您也應瞭解 PostgreSQL 邏輯複製的限制。
本頁的程式碼片段是範例,您可以做為範本,將值替換成 AlloyDB Omni 資源的值。
建立叢集
建立發布者叢集。
$ cat << EOF | kubectl apply -f - apiVersion: v1 kind: Secret metadata: name: db-pw-publisher type: Opaque data: publisher: "b2RzcGFzc3dvcmQ=" # Password is odspassword --- apiVersion: alloydbomni.dbadmin.goog/v1 kind: DBCluster metadata: name: publisher spec: primarySpec: adminUser: passwordRef: name: db-pw-publisher databaseVersion: "15.5.5" resources: memory: 10Gi cpu: 1 disks: - name: DataDisk size: 40Gi EOF建立訂閱端叢集。
$ cat << EOF | kubectl apply -f - apiVersion: v1 kind: Secret metadata: name: db-pw-subscriber type: Opaque data: subscriber: "b2RzcGFzc3dvcmQ=" # Password is odspassword --- apiVersion: alloydbomni.dbadmin.goog/v1 kind: DBCluster metadata: name: subscriber spec: primarySpec: adminUser: passwordRef: name: db-pw-subscriber databaseVersion: "15.5.5" resources: memory: 10Gi cpu: 1 disks: - name: DataDisk size: 40Gi EOF
設定發布端叢集
設定發布者叢集並建立資料表。您也可以選擇發布資料做為測試,確保資料會複製到訂閱者。
將參數
wal_level更新為logical。$ kubectl patch dbclusters.al publisher -p '{"spec":{"primarySpec":{"parameters":{"wal_level":"logical"}}}}' --type=merge找出所需的 Pod。
$ kubectl get pod -l "alloydbomni.internal.dbadmin.goog/dbcluster=publisher, alloydbomni.internal.dbadmin.goog/task-type=database, dbs.internal.dbadmin.goog/ha-role=Primary"登入發布端叢集的資料庫 Pod。
NAME READY STATUS RESTARTS AGE al-2bce-publisher-0 3/3 Running 0 36m $ kubectl exec -ti al-2bce-publisher-0 -- /bin/bash建立名為
customer的資料庫。CREATE DATABASE customer;選用:如要測試,請在資料庫中新增資料表,並插入一些資料。您可以使用這項資料,觀察從發布端到訂閱端的資料複製作業。
$ psql -h localhost -U postgres customer customer=# CREATE TABLE COMPANY( customer(# ID INT PRIMARY KEY NOT NULL, customer(# NAME TEXT NOT NULL, customer(# AGE INT NOT NULL, customer(# SALARY REAL customer(# ); CREATE TABLE customer=# INSERT INTO COMPANY (ID,NAME,AGE,SALARY) VALUES customer-# (1, 'Quinn', 25, 65000.00), customer-# (2, 'Kim', 22, 72250.00), customer-# (3, 'Bola', 31, 53000.00), customer-# (4, 'Sasha', 33, 105000.00), customer-# (5, 'Yuri', 27, 85000.00); INSERT 0 5 customer=# \dt List of relations Schema | Name | Type | Owner --------+---------+-------+---------- public | company | table | postgres (1 row) customer=# select * from 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)建立使用者
logicalreplica,用於複製及授予權限。CREATE USER logicalreplica WITH REPLICATION LOGIN PASSWORD '123';授予權限。這個範例使用公開結構定義。
GRANT SELECT ON ALL TABLES IN SCHEMA public TO logicalreplica; GRANT USAGE ON SCHEMA public TO logicalreplica; ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO logicalreplica;在
customer資料庫中建立發布作業。CREATE PUBLICATION pub_customer; ALTER PUBLICATION pub_customer ADD TABLE company;
設定訂閱端叢集
啟用訂閱端叢集,接收發布端叢集的資料更新。
在訂閱者資料庫中,將
wal_level參數設為logical。$ kubectl patch dbclusters.al subscriber -p '{"spec":{"primarySpec":{"parameters":{"wal_level":"logical"}}}}' --type=merge找出所需的 Pod。
$ kubectl get pod -l "alloydbomni.internal.dbadmin.goog/dbcluster=subscriber, 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 al-3513-subscriber-0 -- /bin/bash Defaulted container "database" out of: database, logrotate-agent, memoryagent, dbinit (init) postgres@al-3513-subscriber-0:/$找出發布者 Pod 的 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 10.116.14.190 -U postgres --create --schema-only customer > /tmp/customer.schema-only.sql在訂閱者資料庫中套用備份。
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)為資料庫
customer建立訂閱項目。postgres@al-3513-subscriber-0:/$ psql -h localhost -U postgres customer customer=# CREATE SUBSCRIPTION sub_customer CONNECTION 'host=10.116.14.190 port=5432 user=logicalreplica dbname=customer password=123' PUBLICATION pub_customer;選用:驗證訂閱端叢集的複製作業。
postgres@al-3513-subscriber-0:/$ psql -h localhost -U postgres customer 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 customer customer=# insert into company(id, name, age, salary) values (6, 'Alex', 39, 100000);在訂閱端叢集,確認發布端叢集資料表新增的資料列,已複製到訂閱端叢集的資料表。
# On the subscriber database, data is synced. postgres@al-3513-subscriber-0:/$ psql -h localhost -U postgres customer customer=# select * from 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 6 | Alex | 39 | 100000 (6 rows)
手動建立其他資料表
邏輯複製不會自動同步 DDL 變更,這與 pglogical 中的 replicate_ddl_command 不同。雖然開放原始碼工具 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)