Streaming 이중화 방식 구성
Streaming Replication은 Primary(주 서버)가 생성한 WAL(Write-Ahead Log) 파일을
Standby(대기 서버)에 실시간으로 전송하고, Standby는 이를 받아서 복제본을 지속적으로 재생합니다.
1. Primary는 데이터 변경을 WAL로 기록
2. Standby는 Primary에 연결해 WAL을 스트리밍 방식으로 수신
3. 수신한 WAL을 디스크에 저장하고, 재생(Replay)하여 데이터 일관성 유지
이중화 구성도 (async)
- 아카이브 위치 : /backups
- 환경파일 : /etc/postgresql/15/main/postgresql.conf
- 데이터 디렉토리 위치 : /var/lib/postgresql/15/main
replication 설정
postgresql.conf 수정
listion_addresses='*' -- 클라이언트 허용
wal_level=replica -- 복제방식
hot_standby=on -- standby 에서 read-only
archive_mode=on -- 아카이브 모드 활성화
archive_command ='cp %p /backups/%f' --wal file 복사 명령
max_wal_senders=10 -- wal 전송 최대 프로세스 수
max_replication_slots=10 -- 복제 슬롯 최대 수
pg_hba.conf 수정 ( primary DB 에서 위의 설정과 repl 유저 허용도 추가 )
replication 구성
1. (primary) replication 권한 유저 생성
create role repl with replication password 'repl' login;
2. (primary) 복제슬롯 생성
select * from pg_create_physical_replication_slot('repl_slot_01');
3. (primary) 복제슬롯 조회 ( replication 세팅전의 active 상태는 f 일 것임)
select slot_name, slot_type, active from pg_replication_slots;
4. (standby) pg_basebackup 수행
pg_basebackup -h 192.168.100.136 -D /var/lib/postgresql/15/main -U repl -P -v -X stream -S repl_slot_01 -R
-h 192.168.100.136 | Primary 서버의 IP 주소입니다. 복제를 받을 대상 주소입니다. |
-D /var/lib/postgresql/15/main | 데이터를 저장할 로컬 경로입니다. PostgreSQL 데이터 디렉토리입니다. 기존 데이터가 있으면 오류 발생! |
-U repl | 백업을 받을 때 사용할 PostgreSQL 사용자 이름입니다. 이 사용자는 REPLICATION 권한을 가지고 있어야 합니다. |
-P | 진행률(progress bar)을 출력합니다. 작업 상황을 실시간으로 확인할 수 있습니다. |
-v | verbose 모드. 실행되는 작업에 대해 자세한 로그를 출력합니다. |
-X stream | WAL 파일을 스트리밍 방식으로 함께 백업받겠다는 의미입니다. |
-S repl_slot_01 | 지정된 replication slot을 사용합니다. 이 슬롯은 Primary 서버에 미리 생성되어 있어야 하며, WAL 보존에 도움이 됩니다. |
-R | Standby 서버용 설정 파일(standby.signal, postgresql.auto.conf)을 자동으로 생성합니다. 이 옵션을 사용하면 별도로 복제 설정을 수동으로 추가할 필요가 없습니다. |
postgres@lkmpg:~/15$ pg_basebackup -h 192.168.100.136 -D /var/lib/postgresql/15/main -U repl -P -v -X stream -S
Password:
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/53000028 on timeline 10
pg_basebackup: starting background WAL receiver
30677/30677 kB (100%), 1/1 tablespace
pg_basebackup: write-ahead log end point: 0/53000138
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: syncing data to disk ...
pg_basebackup: renaming backup_manifest.tmp to backup_manifest
pg_basebackup: base backup completed
5. (standby) db 기동
6. (primary) walsender 확인
postgres@lkmpg:~/15/main$ ps -ef | grep post | grep wal
postgres 1293 1233 0 19:29 ? 00:00:00 postgres: 15/main: walwriter
postgres 1749 1233 0 19:55 ? 00:00:00 postgres: 15/main: walsender repl 192.168.100.137(60650) streaming 0/5C000000
postgres 2369 1710 0 21:22 pts/0 00:00:00 grep wal
7. (standby) walreceiver 확인
postgres@lkmpg:~/15/main$ ps -ef | grep post | grep wal
postgres 1804 1789 0 19:55 ? 00:00:07 postgres: 15/main: walreceiver streaming 0/5C000000
postgres 2658 1710 0 21:23 pts/0 00:00:00 grep wal
replication 테스트
1. (primary) DB 생성
create database repl_test_db;
2. (standby) DB 생성 확인
3. (standby) DB 생성 > 실패 확인
replication 모니터링
- 복제상태 확인 ( 이 4개의 값이 같을 수록 복제 지연이 적다고 보면 된다. 지연이 크면 lag 가 표시된다.)
sent_lsn | 0/54000060 ← primary 가 standby 로 전송한 마지막 위치
write_lsn | 0/54000060 ← standby 가 디스크에 기록한 마지막 위치
flush_lsn | 0/54000060 ← standby 가 디스크에 flush 한 마지막 위치
replay_lsn | 0/54000060 ← standby 에서 반영한 마지막 위치
'Postgresql > 설치' 카테고리의 다른 글
pg DB 이중화 관리 repmgr 구축 (0) | 2025.04.05 |
---|---|
pg DB 이중화 구성 (streaming replication) - primary crash (0) | 2025.04.05 |
pgBackRest 설치 (0) | 2025.03.15 |
유용한 extension 정리 (0) | 2025.03.13 |
postgresql 15 삭제 후 재설치 (0) | 2025.03.10 |