Repmgr 을 활용한 PostgreSQL 이중화 서버 구현 2

&&&·2024년 3월 13일

1. failover_promote.sh 쉘 생성 (all server)

[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

2. failover_promote.sh 실행 권한 부여 (all server)

[root@localhost /] chmod +x /var/lib/pgsql/failover_promote.sh

3. SSH 접속 설정 (all server)

[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

4. repmgr.conf 설정하기 (all server)

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
  • failover : 자동으로 failover 설정을 위해선 ‘automatic’ 으로 설정.
  • promote_command : failover가 automatic으로 설정되어있는 상태에서 오류가 감지되고 현재 서버가 새 primary 서버가 되어야 하는 경우 실행하는 명령어이다.
  • follow_command : 현재 서버가 새로운 primary 서버를 따라야 하는 경우
  • connection_check_type : 다른 노드와 connection 체크를 어떤 방식으로 할 것인가
  • reconnect_attempts : connection이 끊어졌을 때 다시 시도할 횟수
  • reconnect_interval : reconnect_attempts 사이의 시간(초)

5. /etc/logrotate.d/repmgr 에 추가 (all server)

[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
}

6. repmgr 서비스 시작 (all server)

[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

0개의 댓글