[root@localhost /] vi /var/lib/pgsql/failover_promote.sh
#!/bin/bash
up_id=$1
name=$2
if [ $name == 'standby_promote' ]; then
declare -A servers
if [ $up_id == 1 ] ; then
down_id=2
else
down_id=1
fi
servers[2]="{2번 노드 IP}"
servers[1]="{1번 노드 IP}"
while [[ `(ssh -o ConnectTimeout=5 ${servers[$down_id]} echo ok 2>&1)` != 'ok' ]]
do
sleep 2
echo Failed node ${servers[$down_id]} is not reachable >> /var/log/repmgr/repmgrd.log
done
ssh -T ${servers[$down_id]} << EOF
sleep 5
sudo systemctl stop postgresql-14.service && sleep 10
rm -rf /var/lib/pgsql/14/data
repmgr -h ${servers[$up_id]} -d repmgr -U repmgr standby clone --force &&
sudo systemctl start postgresql-14.service && sleep 2
repmgr standby register -F
EOF
echo ${servers[$up_id]}
fi
[root@localhost /] chmod +x /var/lib/pgsql/failover_promote.sh
[root@localhost /] mkdir ~/.ssh
[root@localhost /] su - postgres
[root@localhost /]$ cd ~/.ssh
[root@localhost /]$ ssh-keygen -t rsa -f id_rsa_repmgr
[root@localhost /]$ ssh-copy-id -i id_rsa_repmgr.pub {다른 서버 IP}
[root@localhost /] ssh {다른 서버 IP}
혹시 이렇게 했을 때도 비밀번호를 물어본다면 아래와 같이 입력한다
간혹가다가 내가 비밀번호를 설정하지 않아도 SELinux 정책때문에 비밀번호를 반드시 입력 해야하는 상황이 발생할 수도 있기 떄문에 아래 설정을 추가로 해주어야한다.
[root@localhost /] restorecon -R -v ~/.ssh
[root@localhost /] setenforce 0
failover='automatic'
promote_command='repmgr standby promote -f /etc/repmgr/14/repmgr.conf --log-to-file'
follow_command='repmgr standby follow -f /etc/repmgr/14/repmgr.conf --upstream-node-id=%n'
repmgrd_service_start_command = 'sudo systemctl start repmgr14.service'
repmgrd_service_stop_command = 'sudo systemctl stop repmgr14.service'
log_file='/var/log/repmgr/repmgrd.log'
##선택사항
connection_check_type=ping
reconnect_attempts=6
reconnect_interval=10
[root@localhost /] vi /etc/logrotate.d/repmgr
/var/log/repmgr/repmgrd.log {
missingok
compress
rotate 52
maxsize 100M
weekly
create 0600 postgres postgres
postrotate
/usr/bin/killall -HUP repmgrd
endscript
}
[root@localhost /] systemctl start repmgr14.service
repmgr 서비스를 시작한 뒤에 master의 postgresql 서비스를 종료하면 자동으로 failover되는 것을 확인할 수 있습니다.
&저장해둔 곳
https://github.com/yoojin-kwon12/PostgreSQL_HA_MASTER
https://github.com/yoojin-kwon12/PostgreSQL_HA_SLAVE