10. StackGres Primary 인스턴스 변경

Prmary 변경하기
Patroni의 switchover 명령으로 Primary를 pgsql-1에서 pgsql-0으로 변경한다.
| 항목 | 값 |
|---|---|
| Namespace | db-test |
| 클러스터 | pgsql |
| 기존 Primary | pgsql-1 |
| 변경할 Primary | pgsql-0 |
1. 현재 Primary에 접속
patroni 컨테이너를 지정해 접속한다.
kubectl exec -it -n db-test pgsql-1 -c patroni -- /bin/bash
2. 클러스터 상태 확인
patronictl list
+ Cluster: pgsql (7693859295916413835) ----------+----+-------------+-----+------------+-----+
| Member | Host | Role | State | TL | Receive LSN | Lag | Replay LSN | Lag |
+---------+----------------+---------+-----------+----+-------------+-----+------------+-----+
| pgsql-0 | 10.233.103.169 | Replica | streaming | 2 | 0/532C248 | 0 | 0/532C248 | 0 |
| pgsql-1 | 10.233.110.61 | Leader | running | 2 | | | | |
| pgsql-2 | 10.233.103.162 | Replica | streaming | 2 | 0/532C248 | 0 | 0/532C248 | 0 |
+---------+----------------+---------+-----------+----+-------------+-----+------------+-----+
변경 전 상태를 확인한다.
pgsql-1:Leader / runningpgsql-0:Replica / streamingpgsql-0의 Receive 및 Replay Lag: 모두0
3. Primary 변경
현재 Primary와 승격할 인스턴스를 지정한다.
patronictl switchover --leader pgsql-1 --candidate pgsql-0
| 옵션 | 지정값 |
|---|---|
--leader |
현재 Primary인 pgsql-1 |
--candidate |
새 Primary로 변경할 pgsql-0 |
실행 시점은 Enter를 눌러 기본값인 now를 선택하고, 변경 확인에 y를 입력한다.
When should the switchover take place (e.g. 2026-10-12T06:12 ) [now]:
Are you sure you want to switchover cluster pgsql, demoting current leader pgsql-1? [y/N]: y
2026-10-12 05:12:26.90133 Successfully switched over to "pgsql-0"
4. 변경 결과 확인
patronictl list
+ Cluster: pgsql (7693859295916413835) ----------+----+-------------+-----+------------+-----+
| Member | Host | Role | State | TL | Receive LSN | Lag | Replay LSN | Lag |
+---------+----------------+---------+-----------+----+-------------+-----+------------+-----+
| pgsql-0 | 10.233.103.169 | Leader | running | 3 | | | | |
| pgsql-1 | 10.233.110.61 | Replica | streaming | 3 | 0/60001E0 | 0 | 0/60001E0 | 0 |
| pgsql-2 | 10.233.103.162 | Replica | streaming | 3 | 0/60001E0 | 0 | 0/60001E0 | 0 |
+---------+----------------+---------+-----------+----+-------------+-----+------------+-----+
변경 결과는 다음과 같다.
| 인스턴스 | 변경 전 | 변경 후 |
|---|---|---|
pgsql-0 |
Replica | Leader |
pgsql-1 |
Leader | Replica |
pgsql-2 |
Replica | Replica |
pgsql-0이 Leader / running으로 변경되었고, 나머지 인스턴스는 Replica / streaming, Receive 및 Replay Lag 0으로 확인되었다.