Questo documento fornisce esempi che mostrano come creare e configurare un cluster publisher e un cluster sottoscrittore. Prima di leggere questo documento, devi avere familiarità con la panoramica di AlloyDB Omni. Devi anche tenere presente le limitazioni della replica logica di PostgreSQL.
Gli snippet di codice in questa pagina sono esempi che puoi utilizzare come modelli, sostituendo i valori con quelli delle tue risorse AlloyDB Omni.
Crea i cluster
Crea un cluster di publisher.
$ 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.4" resources: memory: 10Gi cpu: 1 disks: - name: DataDisk size: 40Gi EOF
Crea un cluster di iscritti.
$ 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.4" resources: memory: 10Gi cpu: 1 disks: - name: DataDisk size: 40Gi EOF
Configura il cluster publisher
Configura il cluster del publisher e crea una tabella. Se vuoi, puoi pubblicare i dati come test per assicurarti che vengano replicati nell'abbonato.
Aggiorna il parametro
wal_level
alogical
.$ kubectl patch dbclusters.al publisher -p '{"spec":{"primarySpec":{"parameters":{"wal_level":"logical"}}}}' --type=merge
Trova il pod che ti serve.
$ kubectl get pod -l "alloydbomni.internal.dbadmin.goog/dbcluster=publisher, alloydbomni.internal.dbadmin.goog/task-type=database, dbs.internal.dbadmin.goog/ha-role=Primary"
Accedi al pod del database per il cluster dell'editore.
NAME READY STATUS RESTARTS AGE al-2bce-publisher-0 3/3 Running 0 36m $ kubectl exec -ti al-2bce-publisher-0 -- /bin/bash
Crea un database di nome
customer
.CREATE DATABASE customer;
(Facoltativo) A scopo di test, aggiungi una tabella al database e inserisci alcuni dati. Puoi utilizzare questi dati per osservare la replica dei dati dall'editore all'abbonato.
$ 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)
Crea un utente
logicalreplica
per la replica e per concedere le autorizzazioni.CREATE USER logicalreplica WITH REPLICATION LOGIN PASSWORD '123';
Concedere le autorizzazioni. Questo esempio utilizza uno schema pubblico.
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;
Crea una pubblicazione nel database
customer
.CREATE PUBLICATION pub_customer; ALTER PUBLICATION pub_customer ADD TABLE company;
Configura il cluster sottoscrittore
Attiva il cluster abbonato per ricevere gli aggiornamenti dei dati dal cluster editore.
Imposta il parametro
wal_level
sulogical
nel database degli abbonati.$ kubectl patch dbclusters.al subscriber -p '{"spec":{"primarySpec":{"parameters":{"wal_level":"logical"}}}}' --type=merge
Trova il pod che ti serve.
$ kubectl get pod -l "alloydbomni.internal.dbadmin.goog/dbcluster=subscriber, alloydbomni.internal.dbadmin.goog/task-type=database, dbs.internal.dbadmin.goog/ha-role=Primary"
Accedi al pod del database del cluster di abbonati.
$ 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:/$
Trova l'indirizzo IP del pod publisher, ad esempio
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
Esegui un backup dello schema dall'editore come copia iniziale dei dati pubblicati nel database dell'editore. La replica logica non supporta la replica DDL. Uno schema o una tabella che prevedi di replicare deve esistere nella destinazione (cluster abbonato) prima dell'inizio della replica logica.
postgres@al-3513-subscriber-0:/$ pg_dump -h 10.116.14.190 -U postgres --create --schema-only customer > /tmp/customer.schema-only.sql
Applica il backup nel database degli abbonati.
postgres@al-3513-subscriber-0:/$ psql -h localhost -U postgres < /tmp/customer.schema-only.sql
(Facoltativo) Verifica che la tabella non contenga dati.
# There is no data in table company customer=# select * from company; id | name | age | salary ----+------+-----+-------- (0 rows)
Crea una sottoscrizione per il database
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;
(Facoltativo) Verifica la replica sul cluster di abbonamento.
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)
Nel cluster dell'editore, aggiungi una riga alla tabella.
# 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);
Nel cluster del sottoscrittore, verifica che la riga aggiunta alla tabella nel cluster del publisher sia stata replicata nella tabella del cluster del sottoscrittore.
# 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)
Creare manualmente tabelle aggiuntive
La replica logica non sincronizza automaticamente le modifiche DDL, a differenza di replicate_ddl_command
in pglogical
. Sebbene lo strumento open source
pgl_ddl_deploy
offra una soluzione, puoi anche eseguire manualmente i comandi DDL sul
sottoscrittore.
Per illustrare questo concetto, crea una nuova tabella denominata
finance
nel databasecustomer
sul cluster publisher.# 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
Quando viene aggiunta una nuova tabella al cluster publisher, devi applicare manualmente il DDL (creazione della tabella) nel sottoscrittore, quindi verificare la replica eseguendo il seguente comando nel cluster sottoscrittore.
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)