Patroni

개요

OpenSQL 클러스터 관리자인 Patroni의 환경 설정 및 실행에 대해 설명한다. Patroni는 PostgreSQL를 실행/정지/재시작 등을 직접 수행해야 함으로 PostgreSQL 관련 파라미터 설정을 Patroni구성 설정의 하위 항목으로 설정해야 한다.

환경 설정

Patroni는 3가지의 Configuration 유형이 있다.

Global Dynamic Configuration

  • 해당 옵션은 ETCD 에 저장되고 모든 클러스터 노드에 적용된다.

  • 동적 구성은 patronictl edit-config tool 또는 rest api 사용하여 설정 가능하다.

  • 동적 구성 변경 사항은 모든 노드에 비동기적으로 반영된다.

Local Configuration File (patroni.yml)

  • patroni.yml 수정 후 patroni 재기동 없이 동적 reload 가능

    • Patroni 프로세스에 SIGHUP 보내면 local config 파일 다시 읽음

    • POST /reload REST-API

    • patronictl reload 명령어 사용

환경 변수

일부 local configuration parameter를 환경 변수로 설정/재정의 할 수 있다.

본 매뉴얼에서는 3가지 방식 중 Local Configuration File으로 설정 및 실행을 설명한다.

Local Configuration File

Patroni 프로세스 실행 시, 매개변수로 입력 받는 경로에 위치한 yaml 파일로부터 읽어오는 설정 값이다. 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: opensql
#namespace: /service/
name: postgresql0
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:
    - 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'

Configuration File 예제

각 노드별 patroni.yml 파일을 다음과 같이 작성한다.

REST API 부분의 connect_address 값과 etcd3의 hosts 설정을 노드별 ip-address 주소로 변경한다.

노드 주소가 아래와 같고 patroni 클러스터 구성을 하는 경우

  • node1 172.176.0.2

  • node2 172.176.0.3

  • node3 172.176.0.4

# /etc/patroni/patroni.yml
scope: opensql
name: postgresql0
restapi:
  listen: 0.0.0.0:8008
  connect_address: 178.176.0.2:8008
etcd3:
  protocol: http
  hosts:
  - 178.176.0.2:2379
  - 178.176.0.3:2379
  - 178.176.0.4:2379
  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
    postgresql:
      use_pg_rewind: 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
  
  postgresql:
  listen: 0.0.0.0:5432
  proxy_address: 127.0.0.1:6432  # The address of connection pool (e.g.,openproxy) running next to Patroni/Postgres. Only for service discovery.
  data_dir: /home/postgres/data # 
#  config_dir:
  pgpass: /tmp/pgpass0
  authentication:
    replication:
      username: patroni_repl
      password: patroni_repl
    superuser:
      username: postgres
      password: postgres
    rewind:  # Has no effect on postgres 10 and lower
      username: patroni_rewind
      password: patroni_rewind
  pg_hba:
    - local all all trust
    - host replication replicator all md5
    - host all all all md5
  parameters:
    log_line_prefix: '%m [%r] [%u] [%a]'

tags:
    noloadbalance: false
    clonefrom: false
    nostream: false

Patroni 실행

patroni.yml 작성 후 아래와 같이 각각의 노드에서 아래 명령어를 통해 patroni 프로세스를 실행한다. 인자로 유효한 Configuration .yml 파일의 경로가 입력되어야 한다.

$ patroni /etc/patroni/patroni.yml

Cluster 실행 확인

patronictl 명령어를 사용하여 cluster 정상 동작을 확인한다.

아래 예시처럼 Leader는 running, Replica는 streaming 으로 표기되는 것이 cluster 정상 부팅 상태이다.

postgres@opensql3:~$ patronictl -c /etc/patroni/patroni.yml list
+ Cluster: cluster (7488929455988801618) ------+----+-----------+
| Member   | Host        | Role    | State     | TL | Lag in MB |
+----------+-------------+---------+-----------+----+-----------+
| patroni1 | 178.176.0.4 | Replica | streaming |  1 |         0 |
| patroni2 | 178.176.0.2 | Replica | streaming |  1 |         0 |
| patroni3 | 178.176.0.3 | Leader  | running   |  1 |           |
+----------+-------------+---------+-----------+----+-----------+

Last updated