Docker üzerinde postgresql replication

Docker container üzerindeki bir PostgreSQL basit bir şekilde çoğalta bilirsiniz.

Daha ileri seviye yöntemler ve ayarlar mümkün ancak ben burada sadece hızlı bir şekilde nasıl yapılabilir bunu anlatacağım.

Öncelikle 2 adet PostgreSQL container ayağa kaldırıralım. Bunlar farklı sunucularda da olabilir. Ben aynı sunucu üzerinde yaptım.

Docker containerleri yönetmek için Portainer kullanmanızı tavsiye ederim.

Örnek YAML: docker-compose.yaml

version: '3.8'
services:
  pgsql-primary:
    image: postgres:16
    hostname: pgsql-primary
    ports:
      - 5436:5432
    environment:
      - TZ=Europe/Istanbul
      - POSTGRES_DB=db1
      - POSTGRES_USER=db1
      - POSTGRES_PASSWORD=db1pas
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /opt/postgresql-cluster-test-primary/docker-entrypoint-initdb.d/init-user-db.sh:/docker-entrypoint-initdb.d/init-user-db.sh
      - /opt/postgresql-cluster-test-primary/db:/var/lib/postgresql/data

  pgsql-secondary:
    image: postgres:16
    hostname: pgsql-secondary
    ports:
      - 5437:5432
    environment:
      - TZ=Europe/Istanbul
      - POSTGRES_DB=db2
      - POSTGRES_USER=db2
      - POSTGRES_PASSWORD=db2pas
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /opt/postgresql-cluster-test-secondary/db:/var/lib/postgresql/data  

Dikkatinizi çekti ise docker primary container ayapa kalkarken “init-user-db.sh” dosyasını çalıştırıyor. Bunu şunu için yaptık bize ek bir kullanıcı lazım replication için ve bu kod container ilk oluşunca “replica_user” kullanıcısını olusturuyor.

# init-user-db.sh

#!/bin/bash
set -e 
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER"  <<-EOSQL
	CREATE ROLE replica_user WITH REPLICATION LOGIN PASSWORD 'P@ssword321';
EOSQL

Şimdi veritabanları ayakta ve bir kaç ayar yapmamız gerekiyor. Primary db için data klasöründe bulunan pg_hba.conf dosyasına aşağıdaki satırı ekleyin

host  replication   replica_user  all   md5

Secondary db’deki tüm data klasörü boşaltılmalı.

rm -rf /opt/postgresql-cluster-test-secondary/db/**

Şimdi en önemli noktalardan biri yapmamız gereken primary db deki düm data klasörünü secondary db data klasörüne kopyalamak bu işlemi manuel de yapabiliriz ancak bir iki dosya oluşturup ayarlar yapmamız gerekir. Bu zahmetten kurtulmak için PosgreSQL bir araç yapmış onu kullanacağım

pg_basebackup -h PRIMARY_HOST -U replica_user -X stream -C -S replica_1 -v -R -W -D /var/lib/postgresql/data/

Şimdi yukarıdaki komutun detayına bakalım

-h: This option specifies the host, in this case, the IP address of the master node.
-U: The option specifies the replication user. This is the user that was configured on the primary node and which will be used by the replica node to connect to it. In our case, the replication user is called replica_user.
-X: The option along with the stream value instructs the pg_basebackup utility to stream and include the WAL files in the backup.
-C: The option allows you to create a replication slot before the backup gets underway. The option is used along with the -S option to specify the slot name. In this case, our replication slot is called replica_1.
-v: This prints out verbose output that indicates the progress of the backup process from the primary node to the replica.
-R: The option creates two files; an empty recovery configuration file called standby.signal and a primary node connection settings file called postgresql.auto.conf. The standby.signal file contains connection information about the primary node and the postgresql.auto.conf file informs your replica cluster that it should operate as a standby server.
-W: This prompts you to provide a password for the replica_user replication user.
-D: Lastly, the -D option allows you to include the directory that you want to export the backup files. In this example, we are placing the data under the/var/lib/postgresql/14/main/ directory in the replica node.

Dikkat etmemiz gereken noktalar -D parametresi burada ki klasör yolu secondary data klasör yolu olmalı
-R parametresi ile de primary database connection bilgileri oluşturuluyor secondary data klasörü içerisine “postgresql.auto.conf” bu dosyanın içeriğin bir kontrol etmekte fayda var. Aşağıdakine benze olmalı. Ben passwfile parametresi yerine password parametresi kullandım

postgresql.auto.conf

`primary_conninfo = 'user=replica_user password=P@ssword321 channel_binding=prefer host=51.38.190.69 port=5436 sslmode=prefer sslcompression=0 sslcertmode=allow sslsni=1 ssl_min_protocol_version=TLSv1.2 gssencmode=prefer krbsrvname=postgres gssdelegation=0 target_session_attrs=any load_balance_hosts=disable'`

İnşallah atladığım bir yer yoktur.

Kolay gelsin

Kaynak :
https://www.cherryservers.com/blog/how-to-set-up-postgresql-database-replication
https://www.postgresql.org/docs/current/runtime-config-replication.html
https://www.pgpool.net/docs/41/en/html/pcp-common-options.html
https://kinsta.com/blog/postgresql-replication/

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir