Press ESC to close

PostgreSQL MultiCluster Replication Automatic Switchover & FailOver

Bir Instance üzerinde birden fazla postgreSQL Cluster çalışıyorsa ve bu Cluster ların başka Instance lar üzerinde Replicaları HA ile yönetilmesi isteniyorsa BFM MultiCluster ayarlarının yapılması ve çalışan PostgreSQL Clusterlarının her biri için ayrı servisler tanımlanması gerekmektedir. Bu İşlemlerin çok fazla yorum katmadan teknik adımlarını aşağıda paylaşıyor olacağım. Örnek olarak PostgreSQL 14 ve PostgreSQL 13 2 adet Cluster bir Instance üzerinde çalışıyor olsun.

Instance üzerindeki clusterları rahat yönetebilmek için profile dosyaları set ediyoruz.

vi .profile_pg14
PGDATA=/pg/14/data
export PGDATA
export PATH=/usr/pgsql-14/bin:$PATH

vi .profile_pg13
PGDATA=/pg/13/data
export PGDATA
export PATH=/usr/pgsql-13/bin:$PATH

Mutalaka kontrol etmemiz gereken dosyalar; (Bu ayarlar için; https://www.farukcevik.com.tr/postgresql-bfm-bidirectional-failover-manager/ )

  • postgres root yetkisi (Sudoers)
  • selinux firewall check
  • pgpass dosyası check
  • pg_hba.conf dosyası check

Örnek pgpass dosyası

$ vi /var/lib/pgsql/.pgpass

#hostname:port:database:username:password
*:*:*:bfmuser:welcome1

bfm ve minipg rpm paketleri yüklenir, sonrasında bu paketleri ve servisleri portlarına göre çoklayarak birden fazla bfm ve minipg servisleri oluşturulur (Kaç tane Cluster dinlenecek ve HA sağlacaksa);

postgres sudoers de eklenmiş mi?

postgres        ALL=(ALL)       NOPASSWD: ALL

Selinux durumu nedir?

# getenforce
Enforcing

---Enforcing durumda bunu permissive veya disable duruma çekelim

# setenforce 0

# getenforce
Permissive

Firewall durumunu kontrol edelim;

# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2021-11-23 17:35:43 +03; 1 years 0 months ago
     Docs: man:firewalld(1)
 Main PID: 1882 (firewalld)
    Tasks: 2 (limit: 32767)
   Memory: 24.3M
   CGroup: /system.slice/firewalld.service
           └─1882 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid

---Durumu açık kapatmak lazım

# systemctl stop firewalld

/etc/bfm altındaki bfm ve minipg dosyaları ayarlamaları yapılır, sonrasında yeni servisler için tar yapıyoruz.

# tar -cvf bfm.tar bfmwatcher minipg
bfmwatcher/
bfmwatcher/bfm.sh
bfmwatcher/out.txt
bfmwatcher/application.properties
bfmwatcher/bfm-2.1.1.jar
minipg/
minipg/minipg-2.0.0.jar
minipg/minipg.sh
minipg/application.properties


# ls
bfm.tar  bfmwatcher  minipg

Oluşturulan tar dosyası yeni servisimiz için oluturulan /etc altındaki servisle aynı isimli klasör içerisine kopyalanır, benim durumumda 2 tane cluster için 2 tane directory oluşturuldu olacak bunlar bfm_4581 ve bfm_4577 boluşturulan tar dosyası bu klsör altında tekrar açılır;

# cp bfm.tar /etc/bfm_4581
# cp bfm.tar /etc/bfm_4577

--Her iki dizin içinde;

# tar -xvf bfm.tar
bfmwatcher/
bfmwatcher/bfm.sh
bfmwatcher/out.txt
bfmwatcher/application.properties
bfmwatcher/bfm-2.1.1.jar
minipg/
minipg/minipg-2.0.0.jar
minipg/minipg.sh
minipg/application.properties

--Kontrol sağlayalım;

# ll
total 49780
-rw-r--r--. 1 root     root     50974720 Dec  8 13:52 bfm.tar
drwxr-xr-x. 2 postgres postgres       86 Dec  8 13:38 bfmwatcher
drwxr-xr-x. 2 postgres postgres       77 Dec  8 13:39 minipg


# cd bfmwatcher
# ll
total 25108
-rwxr-xr-x. 1 postgres postgres      803 Jan 23  2022 application.properties
-rw-r--r--. 1 postgres postgres 25692394 Dec  8 13:38 bfm-2.1.1.jar
-rwxr-xr-x. 1 postgres postgres     4957 Dec  4  2021 bfm.sh
-rw-r--r--. 1 postgres postgres     1095 Dec  4  2021 out.txt

Şimdi rpm kurulumunda set edilen bfm ve minipg servislerini çoklama ve ayarlama işlemlerine geçeceğiz, öndelikle servisler bir kontrol edelim

 $ systemctl status bfm
● bfm.service - BFM Service
   Loaded: loaded (/usr/lib/systemd/system/bfm.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

$ systemctl status minipg
● minipg.service - MiniPg Service
   Loaded: loaded (/usr/lib/systemd/system/minipg.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

/usr/lib/systemd/system/minipg.service ve /usr/lib/systemd/system/bfm.service leri port numaralarına göre çoklayıp modifiye edeceğiz.

# cd /usr/lib/systemd/system/
# cp minipg.service minipg_4581.service
# cp minipg.service minipg_4577.service
# cp bfm.service bfm_4581.service
# cp bfm.service bfm_4577.service

Oluşturulan yeni servis dosyalarını düzenleyelim; vi ile toplu olarak satırları değiştimek için :%s/bfm\//bfm_4581\//g ve :%s/.bash_profile/.profile_pg13/g komutunu kullanabiliriz, biz dizinleri el ile oluşturduğumuz için servislerin içerisinden yine el ile bu dizinleri değiştirmemiz gerekiyor.

# vi minipg_4581.service

[Unit]
Description = MiniPg Service
After=network.target
[Service]
Type=forking

User=postgres
Group=postgres
WorkingDirectory=/var/lib/pgsql
#ilgili userın home u ve bfm klasörünün tanıtımı..
WorkingDirectory=/etc/bfm_4581/minipg

ExecStartPre = /usr/bin/sh /var/lib/pgsql/.profile_pg13
ExecStart = /etc/bfm_4581/minipg/minipg.sh start
ExecStop = /etc/bfm_4581/minipg/minipg.sh stop
ExecReload= /etc/bfm_4581/minipg/minipg.sh reload

# whenever watcher service is down it will be restarted again ...if you don't this behavior you can # this line..
#Restart=always
#RestartSec=3

[Install]
WantedBy=multi-user.target

minpg_4577 içinde aynı işlemleri onun dizinlerine göre değiştiriyoruz, ve bfm leride aşağıdaki şekilde düzenliyoruz :%s/bfm\//bfm_4577\//g ve :%s/bfm\//bfm_4581\//g

# vi bfm_4577.service

[Unit]
 Description = BFM Service
 After=network.target
[Service]
 Type=forking
 User=root
 Group=root
 WorkingDirectory=/root
 WorkingDirectory=/etc/bfm_4577/bfmwatcher

 ExecStartPre=/usr/bin/sh  /root/.bash_profile
 ExecStart = /etc/bfm_4577/bfmwatcher/bfm.sh start
 ExecStop = /etc/bfm_4577/bfmwatcher/bfm.sh stop
 ExecReload = /etc/bfm_4577/bfmwatcher/bfm.sh reload

 # whenever watcher service is down it will be restarted again ...if you don't this behavior you can # this line..
 Restart=always


[Install]
 WantedBy=multi-user.target

Şimdi ise minipg.sh ve bfm.sh dosyalarını düzenlememiz gerekiyor;

# vi /etc/bfm_4581/minipg/minipg.sh
SERVICE_NAME=minipg_4581
--Pathleri toplu olarak değiştirmek için
:%s/bfm\//bfm_4581\//g

# vi /etc/bfm_4581/bfmwatcher/bfm.sh
SERVICE_NAME=bfm_4581
:%s/bfm\//bfm_4581\//g
:%s/bfmwatcher|/$BASE_PATH|/g


# vi /etc/bfm_4577/minipg/minipg.sh

SERVICE_NAME=minipg_4577
--Pathleri toplu olarak değiştirmek için
:%s/bfm\//bfm_4577\//g

# vi /etc/bfm_4577/bfmwatcher/bfm.sh
SERVICE_NAME=bfm_4577
:%s/bfm\//bfm_4577\//g
:%s/bfmwatcher|/$BASE_PATH|/g

Bunlar tanımlandıktan sonra deamon-reload yapılır, yeni servisler system tarafından tanınsın.

 # systemctl daemon-reload

Minipg application properties dosyaları için konfigürasyon aşağıdaki gibidir. (Pg-13)

profile_13 (4581) için minipg

minipg.postgres_bin_path               = /usr/pgsql-13/bin/
minipg.pgctl_bin_path                  = /usr/pgsql-13/bin/
minipg.postgres_data_path              = /pg/13/data/
minipg.restore-command                 = restore_command = '/bin/true'
minipg.os                              = linux
minipg.version                         = @project.version@
minipg.postgres_pass_file_path         = /var/lib/pgsql/.pgpass
minipg.pg-version                      = V12X
application.vip-interface              = eno1:1
application.vip-ip                     = 10.10.10.11
application.vip-ip-netmask             = 255.255.255.0
bfm.approval-key = test
minipg.port                            = 7778
bfm.rep_user                           = bfmuser
application.replication-user            = bfmuser
minipg.use-tls                         = false
bfm.user-crypted                       = false
minipg.username                        = bfmuser
minipg.password                        = welcome1

Bfm application Properties aşağıdaki gibidir,

server.bfmList              = 127.0.0.1
server.pguser               = bfmuser
server.pgpassword           = B1S0ft1471
watcher.cluster-port        = 9993
watcher.cluster-pair        = 192.168.5.111:9993
app.bfm-vip-enabled         = true
# use only one of {availability,manual,performance,protection}
# or it will run in availability mode!
#server.masterip=
server.postgres_bin_path    = /usr/pgsql-13/bin
server.postgres_data_path   = /hdd2/pgsql/13/tepesumainpostgres1/data
server.postgres_pgpass_path = /var/lib/pgsql
server.pglist=192.168.5.111:4581,192.168.5.112:4581
app.bfm-ssl-enabled         = false
bfm.user-crypted              = false
bfm.use-tls                 = false
minipg.use-tls              = false
minipg.username              = bfmuser
minipg.password              = welcome1
minipg.port                 = 7778

pgprofile_14 (4577 için); minipg application properties

minipg.postgres_bin_path               = /usr/pgsql-14/bin/
minipg.pgctl_bin_path                  = /usr/pgsql-14/bin/
minipg.postgres_data_path              = /pg/14/data/
minipg.restore-command                 = restore_command = '/bin/true'
minipg.os                              = linux
minipg.version                         = @project.version@
minipg.postgres_pass_file_path         = /var/lib/pgsql/.pgpass
minipg.pg-version                      = V12X
application.vip-interface              = eno2:1
application.vip-ip                     = 10.10.10.12
application.vip-ip-netmask             = 255.255.255.0
bfm.approval-key = test
minipg.port                            = 7779
bfm.rep_user                           = bfmuser
application.replication-user            = bfmuser
minipg.use-tls                         = false
bfm.user-crypted                       = false
minipg.username                        = bfmuser
minipg.password                        = welcome1

bfm_4577 application properties

server.bfmList              = 127.0.0.1
server.pguser               = bfmuser
server.pgpassword           = B1S0ft1471
watcher.cluster-port        = 9994
watcher.cluster-pair        = 192.168.5.112:9994
app.bfm-vip-enabled         = true
# use only one of {availability,manual,performance,protection}
# or it will run in availability mode!
#server.masterip=
server.postgres_bin_path    = /usr/pgsql-14/bin
server.postgres_data_path   = /pg/14/data
server.postgres_pgpass_path = /var/lib/pgsql
server.pglist=192.168.5.111:4577,192.168.5.112:4577
app.bfm-ssl-enabled         = false
bfm.user-crypted              = false
bfm.use-tls                 = false
minipg.use-tls              = false
minipg.username              = bfmuser
minipg.password              = welcome1
minipg.port                 = 7779

Bir yanıt yazın

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