인성장

[PostgreSQL] 수동 Fail-over, Fail-back 방법 본문

PostgreSQL/Admin

[PostgreSQL] 수동 Fail-over, Fail-back 방법

인성장 2023. 11. 24. 14:30

본 문서는 Streaming Replication 방식으로 PostgreSQL 이중화 구성(설치)된 환경에서 수동으로 Fail-over ,Fail-back 하는 방법에 대한 문서입니다.

 

PostgreSQL HA 구성 방법

https://checktech.tistory.com/2

 

Linux 7.9 에 PostgreSQL 15 이중화(HA) 설치 가이드

본 문서는 Streaming Replication 방식으로 PostgreSQL 이중화 구성(설치)에 대한 내용입니다. 설치 환경 1. OS : CentOS Linux Release 7.9 2009 2. DB : PostgreSQL 15.3 3. IP & Hostname Primary Server IP & Hostname Standby Server IP & Ho

checktech.tistory.com

 

 

설치 환경

 

1. OS : CentOS Linux Release 7.9 2009

2. DB : PostgreSQL 15.3

3. IP & Hostname

Primary Server IP & Hostname Standby Server IP & Hostname
192.168.0.148            ispg1  192.168.0.158             ispg2

 

 

 

 

PostgreSQL HA Failover 방법

※   Primary  는 1번 노드에서 수행,  Standby  는 2번 노드에서 수행, 둘 다 있으면 두 노드에서 작업 수행하시면 됩니다.

 

 

장애상황 테스트를 위해  Primary 서버 강제 종료  Primary 

[root@ispg1 ~]# shutdown -h now

 

 

Standby DB postgresql.conf 파일 수정  Standby 

Archive 관련 파라미터 주석 해제

[postgres@ispg2 data]$ vi postgresql.conf
...(생략)...
archive_mode = on                                                                -- 주석(#) 해제
archive_command = 'cp %p /postgres/app/postgres/pgsql15/pg_arch/%f'              -- 주석(#) 해제
archive_timeout = 120                                                            -- 주석(#) 해제
...(생략)...

 

 

Standby -> Primary 전환  Standby 

[postgres@ispg2 data]$ /postgres/app/postgres/pgsql15/bin/pg_ctl promote
waiting for server to promote.... done
server promoted

 

 

 

Standby -> Primary 전환 후 Data Insert 테스트  Standby 

 

 

 

장애난 기존 Primary 서버 기동 후 HA 재구성  Primary 

standby.signal 생성, postgresql.auto.conf 내용 추가

postgresql.auto.conf 는 기존 Standby DB의 postgresql.auto.conf 의 primary_conninfo 내용 중 host 상대 IP로 수정하여 작성하면 됩니다.

[postgres@ispg1 ~]$ cd $PGDATA
[postgres@ispg1 ~]$ touch standby.signal
[postgres@ispg1 ~]$ chmod 600 standby.signal
[postgres@ispg1 ~]$ vi postgresql.auto.conf
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.

primary_conninfo = 'user=hauser passfile=''/home/postgres/.pgpass'' channel_binding=disable host=192.168.0.158 port=5001 sslmode=disable sslcompression=0 sslsni=1 ssl_min_protocol_version=TLSv1.2 gssencmode=disable krbsrvname=postgres target_session_attrs=any'

 

 

기존 Primary 서버 기동  Primary 

[postgres@ispg1 ~]$ pg_ctl -D /postgres/app/postgres/pgsql15/data/ start

 

이렇게 되면 Failover가 완료되어 기존 StandbyPrimary로 바뀌어 Read Write 이 가능하게 되고, 기존 PrimaryStandby로 변경되어 Read Only로만 동작하게 됩니다.

여기서 이전처럼 1번 서버가 Primary로 동작하게끔 하려면 아래 내용대로 Failback 진행하면 됩니다.

 

 

 

 

 

PostgreSQL HA Failback 방법

 

 

현재 Primary DB 서버 종료

  Standby 

[postgres@ispg2 data]$ pg_ctl -D /postgres/app/postgres/pgsql15/data/ stop

 

 

[선택사항]  postgresql.conf파일 수정   Standby 

Standby 쪽에 Archive WAL File 관리가 힘들면 주석처리 해주세요.

[postgres@ispg2 data]$ vi postgresql.conf
...(생략)...
#archive_mode = on                                                                
#archive_command = 'cp %p /postgres/app/postgres/pgsql15/pg_arch/%f'              
#archive_timeout = 120                                                            
...(생략)...

 

 

Standby -> Primary 전환  Primary 

[postgres@ispg1 ~]$ pg_ctl -D /postgres/app/postgres/pgsql15/data/ promote

 

 

Standby 서버 data 디렉토리 아래 standby.signal 파일 생성, postgresql.auto.conf 내용 수정   Standby 

[postgres@ispg2 ~]$ cd $PGDATA
[postgres@ispg2 ~]$ touch standby.signal
[postgres@ispg2 ~]$ chmod 600 standby.signal
[postgres@ispg2 ~]$ vi postgresql.auto.conf
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.

primary_conninfo = 'user=hauser passfile=''/home/postgres/.pgpass'' channel_binding=disable host=192.168.0.148 port=5001 sslmode=disable sslcompression=0 sslsni=1 ssl_min_protocol_version=TLSv1.2 gssencmode=disable krbsrvname=postgres target_session_attrs=any'

 

 

DB 기동  Standby 

[postgres@ispg2 data]$ pg_ctl -D /postgres/app/postgres/pgsql15/data/ start

 

 

데이터 동기화 테스트  Primary   Standby 

 

Failback 완료