Failover란?
primary 서버에 장애가 생겼을 때, 예비 서버(예: standby 서버) 로 대체하여 운영하는 것을 Failover(장애 극복)라고 합니다.
Streaming Replication을 구성하고 primary 서버에 문제가 생겼을 때, 어떻게 failover를 해야 하는지 알아봅시다!
그 전에 Streaming replication 포스트 안보신 분들은 한번 보고오시는걸 추천드립니다!
2023.06.03 - [PostgreSQL] - PostgreSQL 14 Streaming Replication
PostgreSQL 14 Streaming Replication
PostgreSQL의 고가용성을 높이기 위한 방법 중에 하나이자, 가장 많이 사용 하는 방법인 streaming replication에 대해 알아보자! Streaming Replication 이란? standby는 WAL파일이 채워질 때까지 기다리지 않고 WAL
omi-1101.tistory.com
Streaming Replication Failover
primary 서버 장애 발생
primary 서버에 장애 발생을 가정하여 PostgreSQL 서버를 내립니다.
pg_ctl stop
standby 서버 promote
그 후, standby 서버에서 pg_ctl promote 명령어로 standby 모드를 종료합니다.
pg_ctl promote
waiting for server to promote.... done
server promoted
pg_ctl promote 명령을 받아 archive recovery가 종료하였고, 접속 및 쿼리 수행이 가능한 서버(즉, primary)가 되었습니다. 아래 로그를 봅시다!
2023-06-04 22:30:49.811 KST [83811] LOG: received promote request
2023-06-04 22:30:49.811 KST [83811] LOG: redo done at 0/16000060 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 179.66 s
2023-06-04 22:30:49.811 KST [83811] LOG: selected new timeline ID: 3
2023-06-04 22:30:49.840 KST [83811] LOG: archive recovery complete
2023-06-04 22:30:49.843 KST [83809] LOG: database system is ready to accept connections
프로세스 확인
ps -ef | grep postgres
test 83809 1 0 22:27 ? 00:00:00 /usr/pgsql-14/bin/postgres
test 83810 83809 0 22:27 ? 00:00:00 postgres: logger
test 83812 83809 0 22:27 ? 00:00:00 postgres: checkpointer
test 83813 83809 0 22:27 ? 00:00:00 postgres: background writer
test 83814 83809 0 22:27 ? 00:00:00 postgres: stats collector
test 85566 83809 0 22:30 ? 00:00:00 postgres: walwriter
test 85567 83809 0 22:30 ? 00:00:00 postgres: autovacuum launcher
test 85568 83809 0 22:30 ? 00:00:00 postgres: logical replication launcher
이렇게 하면 failover가 완료 됐고 정상적으로 서버 운영이 가능해졌습니다! recovery 모드도 아닌 것도 다음과 같이 확인할 수 있습니다!
postgres=# select pg_is_in_recovery();
pg_is_in_recovery
-------------------
f
(1 row)
Streaming Replication Fail-Back
Fail-Back은 장애가 일어나기 전처럼 Streaming Replication으로 다시 구성하는 과정을 말합니다. pg_basebackup으로 초기 streaming replication 구성할 때 처럼 재 구성해도 되지만, 데이터가 클 경우 오랜시간이 걸릴 수 있습니다. 이번 포스트에서는 pg_rewind라는 유틸리티를 사용하여 진행해보겠습니다!
- Failover(장애복구) 된 상태에서 진행되어야 합니다.
- standby 서버의 standby모드가 종료된 상태 즉,standby 서버가 접속 및 쿼리 수행이 가능한 새로운 primary 서버가 되었는지 확인하고 진행해야 합니다.
파라미터 설정
- pg_rewind를 사용하기 위해서는 다음 파라미터가 설정되어 있어야합니다.
- 두 서버 둘 다 설정해주고, 새 primary(구 standby) 서버는 재시작합니다. 새 standby(구 primary) 서버는 PostgreSQL을 실행하여 파라미터를 적용하고 종료해줍니다.
wal_log_hints=on
full_page_writes=on
pg_rewind 실행
pg_rewind --target-pgdata $PGDATA --source-server='host=192.168.40.134 port=5432' -P
#pg_rewind --target-pgdata $PGDATA --source-server='host=[새 primary IP] port=5432 dbname=[데이터베이스 이름]' -P
pg_rewind: connected to server
pg_rewind: servers diverged at WAL location 0/160000D8 on timeline 2
pg_rewind: rewinding from last common checkpoint at 0/16000060 on timeline 2
pg_rewind: reading source file list
pg_rewind: reading target file list
pg_rewind: reading WAL in target
pg_rewind: need to copy 51 MB (total source directory size is 74 MB)
53125/53125 kB (100%) copied
pg_rewind: creating backup label and updating control file
pg_rewind: syncing target data directory
pg_rewind: Done!
replication slot 생성
새 primary 서버에서 streaming replication 처음 구성했을 때 처럼 replciation slot을 구성해줍니다.
SELECT * FROM pg_create_physical_replication_slot('192_168_40_133');
myrecovery.conf 수정 및 standby.signal 파일 생성
standby가 될 서버의 myrecovery.conf 파일에 새 primary 정보를 입력합니다.
primary_conninfo='host=192.168.40.134 port=5432 user=repluser password=password application_name=192.168.40.133'
primary_slot_name='192_168_40_133'
그리고 standby 서버에 데이터베이스 클러스터 디렉토리에 standby 서버임을 알려주는 standby.signal 파일을 생성 한 후, PostgreSQL 서버를 기동해줍니다.
touch $PGDATA/standby.signal
pg_ctl start
여기서 싱크 맞추는데 필요한 WAL 파일을 삭제하면, 다음과 같은 오류가 발생할 수도 있습니다.
2023-04-24 10:51:10.994 KST [8099] STATEMENT: START_REPLICATION SLOT "192_168_200_141" 1C/7D000000 TIMELINE 3
2023-04-24 10:51:16.000 KST [8109] ERROR: requested WAL segment 000000030000001C0000007D has already been removed
2023-04-24 10:51:16.000 KST [8109] STATEMENT: START_REPLICATION SLOT "192_168_200_141" 1C/7D000000 TIMELINE 3
2023-04-24 10:51:21.003 KST [8110] ERROR: requested WAL segment 000000030000001C0000007D has already been removed
2023-04-24 10:51:21.003 KST [8110] STATEMENT: START_REPLICATION SLOT "192_168_200_141" 1C/7D000000 TIMELINE 3
2023-04-24 10:51:26.017 KST [8111] ERROR: requested WAL segment 000000030000001C0000007D has already been removed
- archive mode 사용 중일때는 postgresql.conf에 restore_command를 추가하거나 수동으로 archive 파일을 옮긴 후에 서버를 시작해주면 됩니다.
- 보통 restore_command 안에 scp 커맨드를 사용하는데, 암호 없이 scp 커맨드를 사용하기 위해 ssh 접속 설정이 되어 있어야합니다.
restore_command = 'scp opensql@[PRIMARY IP]:/opensql/pg/14/archive/%f %p'
- 그렇지 않다면 pg_basebackup으로 다시 Fail-back 해야하지 …않을까…? 싶습니다…😥
프로세스 확인
- primary 서버
ps -ef | grep test
test 53182 1 0 00:16 ? 00:00:00 /usr/pgsql-14/bin/postgres
test 53183 53182 0 00:16 ? 00:00:00 postgres: logger
test 53185 53182 0 00:16 ? 00:00:00 postgres: checkpointer
test 53186 53182 0 00:16 ? 00:00:00 postgres: background writer
test 53187 53182 0 00:16 ? 00:00:00 postgres: stats collector
test 53256 53182 0 00:16 ? 00:00:00 postgres: walwriter
test 53257 53182 0 00:16 ? 00:00:00 postgres: autovacuum launcher
test 53258 53182 0 00:16 ? 00:00:00 postgres: logical replication launcher
test 53685 53182 0 00:19 ? 00:00:00 postgres: walsender repluser 192.168.40.133(56896) streaming 0/1C000110
- standby 서버
ps -ef |grep postgres
test 103129 1 0 00:19 ? 00:00:00 /usr/pgsql-14/bin/postgres
test 103130 103129 0 00:19 ? 00:00:00 postgres: logger
test 103131 103129 0 00:19 ? 00:00:00 postgres: startup recovering 00000004000000000000001C
test 103132 103129 0 00:19 ? 00:00:00 postgres: checkpointer
test 103133 103129 0 00:19 ? 00:00:00 postgres: background writer
test 103134 103129 0 00:19 ? 00:00:00 postgres: stats collector
test 103135 103129 0 00:19 ? 00:00:00 postgres: walreceiver streaming 0/1C000110
'PostgreSQL' 카테고리의 다른 글
| pgpool-II Overview | PostgreSQL 14 HA Tool (0) | 2023.06.14 |
|---|---|
| PostgreSQL14 논리적 복제 | Logical Replication (0) | 2023.06.08 |
| PostgreSQL14 Replication 관련 파라미터 (0) | 2023.06.05 |
| PostgreSQL 14 Streaming Replication (0) | 2023.06.03 |
| PostgreSQL 14.2 File-based Log Shipping Replication - Failover (0) | 2023.06.02 |