Patroni 관리

patronictl은 Patroni 패키지와 함께 설치되는 Python 3로 작성된 CLI (커맨드라인 인터페이스)로, Patroni 클러스터가 제공하는 REST API를 이용하여 클러스터를 관제 하거나 DCS에 접근하기 위한 기능을 제공한다.

PostgreSQL 클러스터의 관리와 상태 체크, 설정 값 등을 확인하기 위해 사용한다.

설치 확인

patronictl은 기본적으로 Python 3 패키지 patroni 와 함께 제공된다. 아래와 같이 노드에 patronictl이 설치되어 있는지 여부와 버전을 확인할 수 있다.

$ which patronictl 
/usr/local/bin/patronictl

$ patronictl version
patronictl version 3.3.0


Local Configuration File 설정하기

Patroni 프로세스 실행 시 매개변수로 입력 받는 경로에 위치한 yml 파일로부터 읽어오는 설정 값이다.

Patroni 프로세스에 SIGHUP 시그널을 보내거나 REST API 서버에 POST /reload 요청을 보내 설정 파일을 새로 읽어올 수 있다. 기본 템플릿 환경 구성 파일의 경로는 /etc/patroni/patroni.yml 이다. 해당 경로에 yml 파일을 생성하고 해당 파일의 내용을 수정하여 구성하고자 하는 환경에 맞게 변경한다.

  • Patroni 클러스터의 메타 정보, etcd 연결 정보, 로깅 구성, REST API 서버 구성 및 PostgreSQL 파라미터 정보를 정의할 수 있다.

  • PostgreSQL 파라미터 셋은 Local Configuration 및 Global Dynamic Configuration로 설정할 수 있다. 중복되는 키가 있는 경우 Local Configuration의 값이 우선한다.

  • bootstrap.dcs 항목을 정의해 아래의 Global Dynamic Configuration의 초기 구성 셋을 설정할 수 있다.

클러스터 메타 정보

scope: batman
#namespace: /service/
name: postgresql0
  • scope: 구성하고자 하는 Patroni 클러스터의 이름. PostgreSQL 파라미터 cluster_name 에 적용된다.

  • namespace: Configuration Store 내에서 사용할 키의 접두어.

  • name: 해당 인스턴스 (노드) 의 이름. 클러스터 내에서 Unique 해야하며 설정하지 않는 경우 호스트네임이 사용된다.

로깅

log:
  type: plain
  format: "[%(asctime)s] [%(module)s] [%(levelname)s]: %(message)s"
  dir: /etc/patroni/logs
  • log.type: 로그 형식을 지정. plainjson 두 가지 옵션을 지원한다. json 타입 사용을 위해서는 Python 패키지 patroni[jsonlogger] 설치가 추가로 필요하다.

  • log.format: 로그 메세지 형식을 지정. Python logging 패키지의 LogRecord 모듈을 사용한다.

  • log.dir: Patroni 로그를 작성할 디렉토리 경로이며, 로그 파일의 기본 보존 (Retention) 사이즈는 425 MB이다.

  • 언급되지 않은 항목은 아래 링크의 문서를 참조한다.

Rest API

restapi:
  listen: 0.0.0.0:8008
  connect_address: 192.168.0.100:8008
  • restapi.listen: Patroni REST API 서버가 바인딩 될 IPv4 주소와 포트 번호

  • restapi.connect_address: Patroni 멤버 간 통신을 위해 사용할 이 노드의 외부 식별 가능한 Rest API 서버 주소이며, 클러스터 멤버를 조회하는 API 호출 시에도 이 값이 Parsing 되어 Host 주소로 사용된다.

DCS (etcd)

etcd v3 인 경우의 예시

etcd3:
  protocol: http
  # host: 192.168.0.100:2379
  hosts:
  - 192.168.0.1:2379
  - 192.168.0.2:2379
  - 192.168.0.3:2379
  • etcd3.protocol: etcd3 클러스터에 접근 시 사용할 프로토콜로 http 혹은 https 를 지원한다. http 가 기본 값으로 사용되며 https 인 경우 etcd3.cacert, etcd3.cert, etcd3.key 항목 설정이 추가로 필요하다.

  • etcd3.host: etcd3 엔드포인트 URL

  • etcd3.hosts: etcd3 클러스터 엔드포인트 URL의 배열

Bootstrapping

Patroni 시작 시 노드에 PostgreSQL 데이터베이스가 초기화되지 않은 경우 이 섹션의 내용을 참조하여 데이터베이스 인스턴스를 초기화한다. 이미 구성된 PostgreSQL 데이터베이스가 노드에 있는 경우 이 섹션의 내용 또는 추가되는 변경사항은 Patroni에 반영되지 않는다.

하위 항목 bootstrap.dcs 의 내용은 Patroni 클러스터를 초기화하며 DCS에 Global Dynamic Configuration 으로 저장할 값들이다.

bootstrap:
  # This section will be written into Etcd:/<namespace>/<scope>/config after initializing new cluster
  # and all other cluster members will use it as a `global configuration`.
  # WARNING! If you want to change any of the parameters that were set up
  # via `bootstrap.dcs` section, please use `patronictl edit-config`!
  dcs:
    ttl: 30
    loop_wait: 10
    retry_timeout: 10
    maximum_lag_on_failover: 1048576
#    primary_start_timeout: 300
#    synchronous_mode: false
    #standby_cluster:
      #host: 127.0.0.1
      #port: 1111
      #primary_slot_name: patroni
    slots:
      barman:
        type: physical
    postgresql:
      use_pg_rewind: true
      use_slots: true
      parameters:
#        wal_level: hot_standby
#        hot_standby: "on"
        max_connections: 100
        max_worker_processes: 8
#        wal_keep_segments: 8
#        max_wal_senders: 10
#        max_replication_slots: 10
#        max_prepared_transactions: 0
#        max_locks_per_transaction: 64
#        wal_log_hints: "on"
#        track_commit_timestamp: "off"
#        archive_mode: "on"
#        archive_timeout: 1800s
#        archive_command: mkdir -p ../wal_archive && test ! -f ../wal_archive/%f && cp %p ../wal_archive/%f
#      recovery_conf:
#        restore_command: cp ../wal_archive/%f %p

  # some desired options for 'initdb'
  initdb:  # Note: It needs to be a list (some options need values, others are switches)
  - encoding: UTF8
  - data-checksums
  • bootstrap.dcs: Patroni 클러스터 환경설정으로 초기화 시 DCS의 /<namespace>/<scope>/config 에 저장되는 Global Dynamic Configuration 셋이다.

  • bootstrap.initdb: 데이터베이스 초기화 방법으로 initdb (기본값) 를 설정한 경우 initdb 실행 시 넘겨줄 파라미터의 배열이다.

PostgreSQL

PostgreSQL 데이터베이스의 시스템 파라미터, 기본 사용자, Host Based Authentication 규칙 및 데이터베이스 파라미터 등을 정의한다.

Global Dynamic Configuration의 키 postgresql 로 지정하는 항목과 동일하다. 같은 키가 이 파일과 Global Dynamic Configuration에 중복으로 정의되는 경우 이 파일 (즉 Local Configuration File) 에 정의되는 값이 우선한다.

postgresql:
  listen: 0.0.0.0:5432
  connect_address: 192.168.0.100:5432
  proxy_address: 127.0.0.1:6432  # The address of connection pool (e.g., pgbouncer) running next to Patroni/Postgres. Only for service discovery.
  #data_dir: data/postgresql0
  data_dir: /var/lib/pgsql/16/data
  bin_dir: /usr/pgsql-16/bin
#  config_dir:
  pgpass: /tmp/pgpass0
  authentication:
    replication:
      username: patroni_repl
      password: patroni_repl
    superuser:
      username: postgres
      password: zalando
    rewind:  # Has no effect on postgres 10 and lower
      username: patroni_rewind
      password: patroni_rewind
  pg_hba:
  # For kerberos gss based connectivity (discard @.*$)
  - local all all trust
  - host replication patroni_repl 192.168.0.0/24 trust
  - host replication patroni_repl 127.0.0.1/32 trust
  - host all all 0.0.0.0/0 md5
  - host all barman 192.168.0.0/24 trust
  - host replication streaming_barman 192.168.0.0/24 trust
  parameters:
    log_line_prefix: '%m [%r] [%u] [%a]'
    archive_command: 'barman-wal-archive node4 pg %p'
    archive_mode: 'true'
    wal_level: 'replica'
  • postgresql.listen: 해당 노드의 Patroni가 실행할 PostgreSQL 서버가 Listen할 주소를 <IP주소>:<Port번호> 형태로 입력한다.

  • postgresql.connect_address: 다른 노드 혹은 클라이언트 어플리케이션에서 참조할 PostgreSQL의 접속 URL을 입력한다. 클러스터 정보 및 DSN을 조회할 때 반환되는 값이다.

  • postgresql.proxy_address: PostgreSQL 서버에 접근하기 위한 Proxy 서버가 있는 경우 필요에 따라 서비스 디스커버리를 위해 그 Proxy 서버의 URL을 입력할 수 있으며 이 값은 DCS의 클러스터 정보에 같이 저장된다.

  • postgresql.data_dir: PostgreSQL 서버의 데이터 경로. Patroni 프로세스를 실행하는 사용자가 해당 경로에 대한 접근 권한을 가지고 있어야 한다. 해당 경로가 비어있으면 Patroni 프로세스 실행 시 initdb 동작이 같이 실행된다.

  • postgresql.bin_dir: PostgreSQL 실행 바이너리 pg_ctl, initdb, postgres 등이 위치한 경로를 지정한다.

  • postgresql.config_dir: PostgreSQL 설정 파일 postgresql.conf 을 보관할 디렉토리 경로. 기본값은 data_dir 값과 동일하다.

  • postgresql.pg_hba: Patroni가 생성할 pg_hba.conf (PostgreSQL의 기본 호스트 기반 인증 설정) 파일에 작성할 아이템들을 입력한다. PostgreSQL 파라미터 hba_file 이 사용자 지정 값으로 설정되어 있으면 이 항목은 무시된다.

  • postgresql.parameters: PostgreSQL 데이터베이스 파라미터이다. 키-값 형태로 입력하며 postgresql.conf 파일을 생성할 때 이용된다.


사용법

patronictl은 별도의 구성 설정을 저장하지 않으며, 매 실행 시 클러스터의 정보를 가져오기 위해 DCS 접속 URL 혹은 Patroni 접속 URL이 주어져야 한다.

$ patronictl list
2024-10-29 17:20:56,603 - WARNING - Listing members: No cluster names were provided 

$ patronictl list opensql
Error: Can not find suitable configuration of distributed configuration store
Available implementations: etcd, etcd3, kubernetes

클러스터 구성에 사용한 Configuration .yml 파일을 인자로 주어 아래와 같이 사용한다.

$ patronictl -c /etc/patroni/patroni.yml list

+ Cluster: opensql (7364637789542980847) ----------+----+-----------+------------------------+ 
| Member      | Host        | Role    | State     | TL | Lag in MB | Tags                   | 
+-------------+-------------+---------+-----------+----+-----------+------------------------+ 
| postgresql0 | 192.1.1.218 | Replica | streaming | 16 |         0 |                        | 
+-------------+-------------+---------+-----------+----+-----------+------------------------+ 
| postgresql1 | 192.1.1.236 | Replica | streaming | 16 |         0 | failover_priority: 150 | 
|             |             |         |           |    |           | nofailover: false      | 
+-------------+-------------+---------+-----------+----+-----------+------------------------+ 
| postgresql2 | 192.1.1.238 | Leader  | running   | 16 |           |                        | 
+-------------+-------------+---------+-----------+----+-----------+------------------------+

아래와 같이 Linux alias로 등록해 사용할 수도 있다.

$ alias pctl='patronictl -c /etc/patroni/patroni.yml'

$ echo 'alias pctl="patronictl -c /etc/patroni/patroni.yml"' >> ~/.bashrc

$ pctl list
+ Cluster: opensql (7364637789542980847) ----------+----+-----------+------------------------+
| Member      | Host        | Role    | State     | TL | Lag in MB | Tags                   |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql0 | 192.1.1.218 | Replica | streaming | 16 |         0 |                        |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql1 | 192.1.1.236 | Replica | streaming | 16 |         0 | failover_priority: 150 |
|             |             |         |           |    |           | nofailover: false      |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql2 | 192.1.1.238 | Leader  | running   | 16 |           |                        |
+-------------+-------------+---------+-----------+----+-----------+------------------------+

클러스터 정보 확인

클러스터의 구성 노드와 각 노드의 접속 정보, 상태 정보를 출력한다.

$ patronictl list
2024-10-29 15:36:15,282 - WARNING - Listing members: No cluster names were provided

## 테이블 형태로 조회 (기본 옵션)
$ patronictl -c /etc/patroni/patroni.yml list
+ Cluster: opensql (7364637789542980847) ----------+----+-----------+------------------------+
| Member      | Host        | Role    | State     | TL | Lag in MB | Tags                   |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql0 | 192.1.1.218 | Replica | streaming | 16 |         0 |                        |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql1 | 192.1.1.236 | Replica | streaming | 16 |         0 | failover_priority: 150 |
|             |             |         |           |    |           | nofailover: false      |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql2 | 192.1.1.238 | Leader  | running   | 16 |           |                        |
+-------------+-------------+---------+-----------+----+-----------+------------------------+

## JSON 형태로 조회
$ patronictl -c /etc/patroni/patroni.yml list -f json
[{"Cluster": "opensql", "Member": "postgresql0", "Host": "192.1.1.218", "Role": "Leader", "State": "running", "TL": 17}, {"Cluster": "opensql", "Member": "postgresql1", "Host": "192.1.1.236", "Role": "Replica", "State": "streaming", "TL": 17, "Lag in MB": 0, "Tags": {"nofailover": false, "failover_priority": 150}}, {"Cluster": "opensql", "Member": "postgresql2", "Host": "192.1.1.238", "Role": "Replica", "State": "streaming", "TL": 17, "Lag in MB": 0}]

토폴로지 출력

$ patronictl -c /etc/patroni/patroni.yml topology
+ Cluster: opensql (7364637789542980847) +-----------+----+-----------+---------------------------------------------+
| Member        | Host        | Role    | State     | TL | Lag in MB | Tags                                        |
+---------------+-------------+---------+-----------+----+-----------+---------------------------------------------+
| postgresql2   | 192.1.1.238 | Leader  | running   | 21 |           |                                             |
| + postgresql0 | 192.1.1.218 | Replica | streaming | 21 |         0 |                                             |
| + postgresql1 | 192.1.1.236 | Replica | streaming | 21 |         0 | {failover_priority: 150, nofailover: false} |
+---------------+-------------+---------+-----------+----+-----------+---------------------------------------------+

DSN 출력

클러스터 노드의 DSN (Data Source Name) 을 출력한다. 별도 옵션이 주어지지 않으면 Leader 노드 접속 정보를 출력한다.

$ patronictl -c /etc/patroni/patroni.yml dsn
host=192.1.1.238 port=5432

특정 Role을 가진 멤버에 대한 접속 정보를 출력하거나 이름으로 특정 멤버에 대한 접속 정보를 출력할 수도 있다.

$ patronictl -c /etc/patroni/patroni.yml dsn -r replica
host=192.1.1.218 port=5432

$ patronictl -c /etc/patroni/patroni.yml dsn -m postgresql1
host=192.1.1.236 port=5432

재시작

클러스터의 멤버 노드 중 하나의 PostgreSQL 프로세스를 재시작한다. 클러스터 이름 (메타 정보에서 설정) 이 인자로 주어져야 하며 추가로 멤버 노드의 이름을 옵션으로 넣을 수 있다. 멤버 이름이 지정되지 않은 경우 모든 노드들이 한번씩 재시작된다.

$ pctl restart <cluster_name>

대화형 프롬프트를 통해 재시작 일시 (바로 재시작하는 옵션과 Timestamp를 지정하여 재시작을 스케쥴하는 기능을 제공) 를 입력하며 재시작할 PostgreSQL 서버의 버전을 확인하여 필터링하는 기능을 제공한다.

$ pctl restart opensql postgresql0
+ Cluster: opensql (7364637789542980847) ----------+----+-----------+------------------------+
| Member      | Host        | Role    | State     | TL | Lag in MB | Tags                   |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql0 | 192.1.1.218 | Replica | streaming | 16 |         0 |                        |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql1 | 192.1.1.236 | Replica | streaming | 16 |         0 | failover_priority: 150 |
|             |             |         |           |    |           | nofailover: false      |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql2 | 192.1.1.238 | Leader  | running   | 16 |           |                        |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
When should the restart take place (e.g. 2024-10-31T12:16)  [now]:
## now를 입력하면 바로 재시작

Are you sure you want to restart members postgresql0? [y/N]:

Restart if the PostgreSQL version is less than provided (e.g. 9.5.2)  []:

Success: restart on member postgresql0

리로딩

클러스터의 멤버 노드 중 하나의 PostgreSQL 서버를 재시작하지 않고 Configuration을 다시 불러오는 기능이다. 클러스터 이름이 인자로 주어져야 하며 추가로 멤버 노드의 이름을 옵션으로 넣을 수 있다.

$ pctl reload <cluster_name>

대화형 프롬프트를 통해 클러스터 멤버 리로딩을 스케쥴할 지 여부를 확인한다.

$ pctl reload opensql
+ Cluster: opensql (7364637789542980847) ----------+----+-----------+------------------------+
| Member      | Host        | Role    | State     | TL | Lag in MB | Tags                   |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql0 | 192.1.1.218 | Replica | streaming | 16 |         0 |                        |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql1 | 192.1.1.236 | Replica | streaming | 16 |         0 | failover_priority: 150 |
|             |             |         |           |    |           | nofailover: false      |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql2 | 192.1.1.238 | Leader  | running   | 16 |           |                        |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
Are you sure you want to reload members postgresql0, postgresql1, postgresql2? [y/N]:
Reload request received for member postgresql0 and will be processed within 10 seconds
Reload request received for member postgresql1 and will be processed within 10 seconds
Reload request received for member postgresql2 and will be processed within 10 seconds

Context 값이 internal, postmaster 인 PostgreSQL 변수 (GUC)는 Reloading 기능으로 변경할 수 없다. internal 변수는 서버 프로그램을 컴파일할 때 혹은 initdb 명령어로 데이터베이스를 초기화할 때 결정되는 변수로 데이터베이스 재설치 없이 변경할 수 없으며 postmaster 변수는 PostgreSQL 프로세스를 재시작해야 변경할 수 있다.

이력 조회

클러스터에서 발생한 Failover / Switchover 이력을 조회한다.

$ pctl history
+----+------------+------------------------------+----------------------------------+-------------+
| TL |        LSN | Reason                       | Timestamp                        | New Leader  |
+----+------------+------------------------------+----------------------------------+-------------+
|  1 |   26875256 | no recovery target specified | 2024-05-03T14:20:28.841738+09:00 | postgresql2 |
|  2 |  213072680 | no recovery target specified | 2024-05-03T14:45:37.945208+09:00 | postgresql1 |
|  3 |  213101064 | no recovery target specified | 2024-05-03T14:46:14.686504+09:00 | postgresql2 |
|  4 |  805306528 | no recovery target specified | 2024-05-28T17:44:02.808722+09:00 | postgresql1 |
|  5 |  855638176 | no recovery target specified | 2024-05-28T17:54:12.358175+09:00 | postgresql2 |
|  6 | 1879048352 | no recovery target specified | 2024-07-30T15:58:10.527327+09:00 | postgresql0 |
|  7 | 1895825568 | no recovery target specified | 2024-07-30T15:58:52.408275+09:00 | postgresql2 |
|  8 | 2013266080 | no recovery target specified | 2024-08-12T10:00:38.641449+09:00 | postgresql2 |
|  9 | 2030043296 | no recovery target specified | 2024-08-12T10:04:16.370771+09:00 | postgresql2 |
| 10 | 2046820512 | no recovery target specified | 2024-08-12T10:05:07.178679+09:00 | postgresql2 |
| 11 | 2063597728 | no recovery target specified | 2024-08-12T10:47:06.368795+09:00 | postgresql2 |
| 12 | 2080374944 | no recovery target specified | 2024-08-12T10:50:59.596850+09:00 | postgresql2 |
| 13 | 2332033184 | no recovery target specified | 2024-10-28T13:41:28.936064+09:00 | postgresql1 |
| 14 | 2348810400 | no recovery target specified | 2024-10-28T16:04:27.587725+09:00 | postgresql0 |
| 15 | 2365587616 | no recovery target specified | 2024-10-28T16:16:32.842442+09:00 | postgresql2 |
+----+------------+------------------------------+----------------------------------+-------------+

쿼리

특정 Role을 가진 PostgreSQL 노드에 데이터베이스 쿼리를 실행하여 결과값을 확인할 수 있다.

$ patronictl -c /etc/patroni/patroni.yml query -U postgres --password -c "SELECT VERSION();"
Password: 
version
PostgreSQL 14.13 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit

Failover

클러스터에 발생한 장애로 Leader 노드가 없는 경우 수동으로 Failover를 실행할 수 있다.

$ patronictl -c /etc/patroni/patroni.yml failover

Current cluster topology
+ Cluster: opensql (7364637789542980847) ----------+----+-----------+------------------------+
| Member      | Host        | Role    | State     | TL | Lag in MB | Tags                   |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql0 | 192.1.1.218 | Replica | streaming | 20 |         0 |                        |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql1 | 192.1.1.236 | Leader  | running   | 20 |           | failover_priority: 150 |
|             |             |         |           |    |           | nofailover: false      |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql2 | 192.1.1.238 | Replica | streaming | 20 |         0 |                        |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
Candidate ['postgresql0', 'postgresql2'] []: postgresql2
Are you sure you want to failover cluster opensql, demoting current leader postgresql1? [y/N]: y
2024-11-04 16:36:12.20700 Successfully failed over to "postgresql2"
+ Cluster: opensql (7364637789542980847) --------+----+-----------+------------------------+
| Member      | Host        | Role    | State   | TL | Lag in MB | Tags                   |
+-------------+-------------+---------+---------+----+-----------+------------------------+
| postgresql0 | 192.1.1.218 | Replica | running | 20 |         0 |                        |
+-------------+-------------+---------+---------+----+-----------+------------------------+
| postgresql1 | 192.1.1.236 | Replica | stopped |    |   unknown | failover_priority: 150 |
|             |             |         |         |    |           | nofailover: false      |
+-------------+-------------+---------+---------+----+-----------+------------------------+
| postgresql2 | 192.1.1.238 | Leader  | running | 20 |           |                        |
+-------------+-------------+---------+---------+----+-----------+------------------------+

정상 동작중인 클러스터에서도 patronictl failover 명령어로 수동 Failover를 수행할 수 있다. 다만 정상 동작중인 클러스터에서 Leader 인스턴스를 변경하고자 하는 경우 patronictl switchover 명령어를 사용하는 것이 권장된다.

Switchover

PostgreSQL / Patroni Leader 노드를 Replica로 전환하고, 다른 Replica 노드 중 하나를 Leader로 승격시키는 동작이다.

$ patronictl -c /etc/patroni/patroni.yml switchover

Current cluster topology
+ Cluster: opensql (7364637789542980847) ----------+----+-----------+------------------------+
| Member      | Host        | Role    | State     | TL | Lag in MB | Tags                   |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql0 | 192.1.1.218 | Leader  | running   | 19 |           |                        |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql1 | 192.1.1.236 | Replica | streaming | 19 |         0 | failover_priority: 150 |
|             |             |         |           |    |           | nofailover: false      |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql2 | 192.1.1.238 | Replica | streaming | 19 |         0 |                        |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
Primary [postgresql0]: postgresql0
Candidate ['postgresql1', 'postgresql2'] []: postgresql1
When should the switchover take place (e.g. 2024-11-04T17:34 )  [now]: now
Are you sure you want to switchover cluster opensql, demoting current leader postgresql0? [y/N]: y
2024-11-04 16:35:07.34291 Successfully switched over to "postgresql1"
+ Cluster: opensql (7364637789542980847) --------+----+-----------+------------------------+
| Member      | Host        | Role    | State   | TL | Lag in MB | Tags                   |
+-------------+-------------+---------+---------+----+-----------+------------------------+
| postgresql0 | 192.1.1.218 | Replica | stopped |    |   unknown |                        |
+-------------+-------------+---------+---------+----+-----------+------------------------+
| postgresql1 | 192.1.1.236 | Leader  | running | 19 |           | failover_priority: 150 |
|             |             |         |         |    |           | nofailover: false      |
+-------------+-------------+---------+---------+----+-----------+------------------------+
| postgresql2 | 192.1.1.238 | Replica | running | 19 |         0 |                        |
+-------------+-------------+---------+---------+----+-----------+------------------------+

Pause / Resume

Patroni 클러스터의 자동 Failover 기능을 중단시키고 유지보수 모드 (Maintenance Mode) 로 클러스터를 전환한다.

$ patronictl -c /etc/patroni/patroni.yml pause
Success: cluster management is paused

$ patronictl -c /etc/patroni/patroni.yml list
+ Cluster: opensql (7364637789542980847) ----------+----+-----------+------------------------+
| Member      | Host        | Role    | State     | TL | Lag in MB | Tags                   |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql0 | 192.1.1.218 | Replica | streaming | 25 |         0 |                        |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql1 | 192.1.1.236 | Replica | streaming | 25 |         0 | failover_priority: 150 |
|             |             |         |           |    |           | nofailover: false      |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
| postgresql2 | 192.1.1.238 | Leader  | running   | 25 |           |                        |
+-------------+-------------+---------+-----------+----+-----------+------------------------+
 Maintenance mode: on

Resume 명령어로 유지보수 모드를 종료하고 클러스터의 자동 Failover 기능을 다시 활성화한다.

$ patronictl -c /etc/patroni/patroni.yml resume
Success: cluster management is resumed

Config 확인

DCS를 조회하여 현재 Patroni 클러스터에 적용된 설정값들을 확인할 수 있다.

$ patronictl -c /etc/patroni/patroni.yml show-config
loop_wait: 10
maximum_lag_on_failover: 1048576
postgresql:
  parameters:
    archive_command: barman-wal-archive node4 pg %p
    archive_mode: 'true'
    authentication_timeout: '200'
    log_line_prefix: '%m [%r] [%u] [%a]'
    max_connections: '250'
    wal_level: replica
    wal_receiver_timeout: '30000'
  pg_hba:
  - local all all trust
  - host replication patroni_repl 192.1.1.218/26 trust
  - host replication patroni_repl 127.0.0.1/32 trust
  - host all all 0.0.0.0/0 md5
  - host all barman 192.1.1.218/26 trust
  - host replication streaming_barman 192.1.1.218/26 trust
  use_pg_rewind: true
  use_slots: true
retry_timeout: 10
slots:
  barman:
    type: physical
ttl: 30

Config 수정

DCS에 저장된 Patroni 클러스터의 동적 환경설정 (Dynamic Configuration) 값을 수정할 수 있다.

$ pctl edit-config

로컬 사용자의 EDITOR 환경변수로 지정된 텍스트 에디터 또는 vi 를 서브프로세스로 실행하여 TTY 형태로 Configuration을 수정 후 저장할 수 있다.

loop_wait: 10
maximum_lag_on_failover: 1048576
postgresql:
  parameters:
    archive_command: barman-wal-archive node4 pg %p
    archive_mode: 'false'
    authentication_timeout: '500'
    log_line_prefix: '%m [%r] [%u] [%a]'
    max_connections: 500
    wal_level: replica
    wal_receiver_timeout: '30000'
  pg_hba:
  - local all all trust
  - host replication patroni_repl 192.1.1.218/26 trust
  - host replication patroni_repl 127.0.0.1/32 trust
  - host all all 0.0.0.0/0 md5
  - host all barman 192.1.1.218/26 trust
  - host replication streaming_barman 192.1.1.218/26 trust
  use_pg_rewind: true
  use_slots: true
retry_timeout: 10
slots:
  barman:
    type: physical
ttl: 45
~
~
~
~
"/tmp/opensql-config-m332oce3.yaml" 25L, 683C

Last updated