本頁面的程式碼片段是範例,您可以做為模型使用,並將值替換為 AlloyDB Omni 資源的值。
事前準備
- 在 Kubernetes 上安裝 Omni 運作資源。
- 請務必建立複製運算單元、發布者叢集和發布作業。詳情請參閱「建立複製位置和發布項目」。
建立及設定訂閱端叢集
建立訂閱者叢集。
$ 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: "16.8.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 字串編碼,例如ChangeMe123
的Q2hhbmdlTWUxMjM=
。CPU_COUNT
:這個資料庫叢集中每個資料庫執行個體可用的 CPU 數量。MEMORY_SIZE
:這個資料庫叢集的每個資料庫執行個體記憶體量。建議您將每個 CPU 的記憶體設為 8 GB。舉例來說,如果您在資訊清單中將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)