본문으로 건너뛰기
10.5 백업 도구 통합

10.5 백업 도구 통합

Patroni는 백업을 만들지도, WAL archive를 관리하지도 않는다. leader lock과 replication 관리가 Patroni의 관심사이고, 백업 스케줄이나 archive 실패 대응은 문서화된 동작에 없다. archive_command가 계속 실패해도 Patroni가 경고해 주리라 기대하면 안 되며, 그 감시는 백업 도구와 별도 모니터링 체계의 몫이다. 대신 Patroni는 백업 도구를 연결할 지점을 세 곳 제공한다.

통합 지점 세 곳

  1. create_replica_methods: 새 replica를 pg_basebackup 대신 백업 저장소에서 복원해 생성한다. primary에 복제본 생성 부하를 주지 않으려는 대용량 클러스터에서 흔한 선택이다.
  2. custom bootstrap: 클러스터 자체를 백업에서 초기화한다. recovery_conf 블록으로 PITR 대상을 지정하는 구성이 여기에 속한다. 공식 문서는 bootstrap method가 replica 생성과 달리 체이닝이나 fallback 없이 단일 method로만 동작한다고 주의를 준다.
  3. permanent slot: failover가 일어나도 백업 도구 같은 외부 소비자의 replication slot이 유지되도록 slots 섹션에 정의한다.

pgBackRest

공식 문서의 replica 생성 예시다.

postgresql:
    create_replica_methods:
        - pgbackrest
        - basebackup
    pgbackrest:
        command: /usr/bin/pgbackrest --stanza=<scope> --delta restore
        keep_data: True
        no_params: True
    basebackup:
        max-rate: '100M'

옵션의 짝이 중요하다. --delta restore는 기존 파일을 checksum 비교로 재활용하는 복원이므로, Patroni가 복원 전에 PGDATA를 비우지 않도록 keep_data: True를 함께 준다. 명령에 필요한 인자가 이미 다 들어 있으므로 no_params: True로 Patroni의 인자 전달(--scope, --datadir 등)을 막는다. pgbackrest가 실패하면 다음 방법인 basebackup으로 넘어간다.

WAL archive 연동은 pgBackRest 쪽 명령을 Patroni 설정에 배치하는 방식이 일반적이다. archive_command는 dynamic configuration의 postgresql.parameters에, 복원용 restore_command는 custom bootstrap의 recovery_conf에 넣는다.

postgresql:
    parameters:
        archive_mode: "on"
        archive_command: pgbackrest --stanza=demo archive-push %p

WAL-E와 WAL-G

WAL-E는 공식 문서에 replica 생성 예시가 있다.

postgresql:
    create_replica_methods:
        - wal_e
        - basebackup
    wal_e:
        command: patroni_wale_restore
        no_leader: 1
        envdir: {{WALE_ENV_DIR}}
        use_iam: 1

no_leader: 1은 leader가 없어도 이 method를 실행하도록 허용하는 옵션이다. 클러스터 전체가 사라진 상태에서 객체 저장소의 백업만으로 노드를 복원하는 시나리오가 이 옵션의 용도이고, 이때 --connstring 인자는 빈 값으로 전달된다.

WAL-G는 공식 문서에 예시가 없다. Spilo 이미지가 wal-g backup-fetch를 호출하는 wrapper 스크립트를 create_replica_methods에 등록하는 패턴을 쓰지만, 공식 문서가 보증하는 구성은 아니다. 검증된 YAML 예시가 없으므로 여기에도 싣지 않는다.

Barman

공식 문서의 tools integration 페이지가 다루는 유일한 도구가 Barman이다. patroni_barman 애플리케이션이 Barman의 pg-backup-api와 통신하는 구조로, Barman 3.10 이상과 pg-backup-api 구성이 요건이다.

patroni_barman recover는 custom bootstrap과 custom replica creation method 양쪽에 쓸 수 있다.

postgresql:
    create_replica_methods:
        - barman
        - basebackup
    barman:
        command: patroni_barman --api-url https://barman-host:7480 recover
        barman-server: my_server
        ssh-command: ssh postgres@patroni-host

patroni_barman config-switch는 failover로 primary가 바뀌었을 때 Barman 쪽 설정을 전환하는 명령으로, on_role_change callback에 걸어 promote 시점에 자동 실행되게 한다.

postgresql:
    callbacks:
        on_role_change: >
            patroni_barman --api-url YOUR_API_URL config-switch
                --barman-server YOUR_BARMAN_SERVER_NAME
                --barman-model YOUR_BARMAN_MODEL_NAME
                --switch-when promoted
callback은 비동기로 실행되어 promote를 블로킹하지 않는다. config-switch가 완료되기 전에 새 primary가 쓰기를 받기 시작하는 구간이 존재하므로, 전환 직후의 백업 공백 가능성을 감안해 운영 절차를 설계한다.

custom bootstrap과 PITR

백업에서 클러스터를 초기화하는 custom bootstrap의 일반 구조다.

bootstrap:
    method: <name>
    <name>:
        command: <script> [args]
        keep_existing_recovery_conf: False
        no_params: False
        recovery_conf:
            recovery_target_action: promote
            recovery_target_timeline: latest
            restore_command: <method_specific_restore_command>

recovery_conf 블록이 정의되어 있으면 Patroni가 인스턴스 시작 전에 recovery 설정을 만든다(PostgreSQL 12 이상은 recovery.signal 방식). pgBackRest처럼 복원 도구가 recovery 설정을 직접 생성하는 경우에는 keep_existing_recovery_conf: True로 기존 recovery.signal과 standby.signal을 보존한다.

permanent slot

slots:
  backup_tool_slot:
    type: physical
  cdc_slot:
    type: logical
    database: my_db
    plugin: test_decoding

permanent slot은 switchover와 failover에서 보존되고, 없으면 Patroni가 생성한다. physical slot에는 cluster_type 필드(primary 또는 standby)로 특정 클러스터 유형에서만 생성되게 제한하는 옵션도 있다. 백업 도구와 CDC 소비자의 slot 보존, 그리고 standby cluster가 원본에서 쓸 slot의 보존(10.4 standby cluster)이 대표 용례다. 소비자가 오래 멈추면 slot이 pg_wal을 계속 잡아 두므로 slot lag 모니터링이 함께 필요하다.

정리

  • Patroni의 백업 통합은 내장 기능이 아니라 접점이다. create_replica_methods(복원 기반 replica), custom bootstrap(PITR 초기화), permanent slot(소비자 보존) 세 곳이 전부다.
  • 공식 문서에 예시가 있는 도구는 pgBackRest, WAL-E, Barman이고, 전용 통합(patroni_barman)이 문서화된 것은 Barman뿐이다. WAL-G는 공식 예시가 없다.
  • 복원 명령의 성격에 맞춰 keep_data, no_params, no_leader 옵션을 짝지어야 의도대로 동작한다.