LAB Hot Standby PostgreSQL 11

O post de hoje é de um lab que fiz montando um grupo de três servidores Hot Standby para PostgreSQL 11.

Duvidas de parâmetros e documentação: https://www.postgresql.org/docs/

Arquitetura:

====== Informações dos servidores ======

192.168.11.132 master.localdomain master
192.168.11.133 slave1.localdomain slave1
192.168.11.134 slave2.localdomain slave2
192.168.11.135 slave3.localdomain slave3

====== Instalar o PostgreSQL em todos os slaves ======

rpm -Uvh https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7.5-x86_64/pgdg-centos11-11-2.noarch.rpm
yum -y install postgresql11 postgresql11-server postgresql11-contrib postgresql11-libs postgresql-client postgresql-common postgresql-devel
systemctl enable postgresql-11

====== SSH entre os servidores sem senha ======

1 - Executar em todos os servidores

cat >> /etc/hosts << EOF
192.168.11.132 master.localdomain master
192.168.11.133 slave1.localdomain slave1
192.168.11.134 slave2.localdomain slave2
192.168.11.135 slave3.localdomain slave3
EOF

su - postgres

ssh-keygen -t rsa

2 -  Executar no master para ter acesso aos slaves sem senha

ssh-copy-id [email protected]
ssh-copy-id [email protected]
ssh-copy-id [email protected]

====== Master ======

1 - Montar o acesso ao storage onde iremos salvar os archivelogs e compartilhar com todos os slaves /wal.

2 - Criar o usuario para ter acesso da replicação via streaming replication

CREATE ROLE replication WITH REPLICATION PASSWORD 'password' LOGIN;
ALTER USER usr_rep WITH SUPERUSER;

3 - Edição do /var/lib/pgsql/11/data/pg_hba.conf

cat >> /var/lib/pgsql/11/data/pg_hba.conf << EOF
########## LIBERANDO ACESSO #########
host all all 0.0.0.0/0 md5
host all all 127.0.0.1/32 md5
##### Liberar acesso Slave1 Streaming Replication ######
host replication usr_rep 192.168.11.133/24 md5
EOF

4 - Edição do /var/lib/pgsql/11/data/postgresql.conf

max_wal_senders = 3
wal_level = replica
fsync = on 
synchronous_commit = on
archive_mode = on
wal_sync_method = fsync
archive_command='test ! -f /wal/%f && cp %p /wal/%f'
archive_timeout = 1800 = 30 minutos switch
#wal_keep_segments = 10
listen_addresses = '*'
port = 5432

5 -  Aplicando as modificações

systemctl reload postgresql-11

6 - Gerando o archivelog/wal

psql -c "select pg_switch_wal();"
ShellScript
7 - Verificando o archivelog gerado no /wal

ls -lat /wal/
ShellScript
8 - Gerar backup e copiar para o slave via rsync

psql -U postgres -c "SELECT pg_start_backup('base');"

rsync -av /var/lib/pgsql/11/data/ --exclude postgresql.conf --exclude postmaster.pid --exclude postmaster.opts --exclude pg_xlog/* /var/lib/pgsql/11/data/ [email protected]:/var/lib/pgsql/11/data/

rsync -av /var/lib/pgsql/11/data/ --exclude postgresql.conf --exclude postmaster.pid --exclude postmaster.opts --exclude pg_xlog/* /var/lib/pgsql/11/data/ [email protected]:/var/lib/pgsql/11/data/

rsync -av /var/lib/pgsql/11/data/ --exclude postgresql.conf --exclude postmaster.pid --exclude postmaster.opts --exclude pg_xlog/* /var/lib/pgsql/11/data/ [email protected]:/var/lib/pgsql/11/data/

psql -U postgres -c "SELECT pg_stop_backup();"

====== Slave1 Streaming Replication ======

su - postgres

1 - Editar /var/lib/pgsql/11/data/postgresql.conf 

listen_addresses = '*'
port = 5432

2 - Editar /var/lib/pgsql/11/data/recovery.conf

standby_mode = on # Libera Acesso das aplicações ou usuários para leitura nos bancos

trigger_file = '/var/lib/pgsql/11/data/failover.trg' # Para promover o standby para master basta criar o arquivo /var/lib/pgsql/11/data/failover.trg

primary_conninfo = 'host=192.168.11.132 port=5432 user=usr_rep password=usr_rep'  # String usada para conexão do Streaming Replication

3 - Iniciar o PostgreSQL

systemctl start postgresql-11

Salve2 Streaming Replication e Archivelog

su - postgres

1 - Editar /var/lib/pgsql/11/data/postgresql.conf 

listen_addresses = '*'
port = 5432

2 - Editar /var/lib/pgsql/11/data/recovery.conf

standby_mode = 'on' # Libera Acesso das aplicações ou usuários para leitura nos bancos
primary_conninfo = 'host=192.168.11.132 port=5432 user=usr_rep password=usr_rep'target_session_attrs=any'

restore_command = 'cp /wal/%f %p'

trigger_file = '/var/lib/pgsql/11/data/failover.trg' # Para promover o standby para master basta criar o arquivo /var/lib/pgsql/11/data/failover.trg
recovery_target_timeline = 'latest'

3 - Iniciar o PostgreSQL

systemctl start postgresql-11

====== Slave3 Archivelog ======

su - postgres

1 - Editar /var/lib/pgsql/11/data/postgresql.conf 
listen_addresses = '*'
port = 5432

2 - Editar /var/lib/pgsql/11/data/recovery.conf
standby_mode = 'on' # Libera Acesso das aplicações ou usuários para leitura nos bancos
restore_command = 'cp /wal/%f %p'
trigger_file = '/var/lib/pgsql/11/data/failover.trg' # Para promover o standby para master basta criar o arquivo /var/lib/pgsql/11/data/failover.trg
recovery_target_timeline = 'latest'

3 - Iniciar o PostgreSQL
systemctl start postgresql-11

====== Logs ======
tail -f /var/log/messages

====== Monitoramento do sincronismo ======

1 - Master
select * from pg_stat_replication;
ShellScript
2 - Atrasos
select now() - pg_last_xact_replay_timestamp() AS replication_delay;
ShellScript
3 - Via sistema operacional

Master 
ps -eaf | grep sender
ShellScript
Slaves
ps -eaf | grep receiver
ps -eaf | grep startup
ShellScript
============== Testando ==============
- Criando banco no master
create database laba;
- Switch Wal/Archivelog
ShellScript
============== Verificando no slave1 ==============
ShellScript
============== Verificando no slave2 ==============
ShellScript
============== Verificando no slave3 ==============
ShellScript

Leave a Reply

Your email address will not be published. Required fields are marked *

search previous next tag category expand menu location phone mail time cart zoom edit close