4.2 monitor 생성
monitor는 pgautofailover extension이 설치된 일반적인 PostgreSQL 인스턴스입니다. pg_autoctl create monitor는 initdb부터 extension 생성, HBA 규칙 설정, 인증서 생성까지 한 번에 처리합니다. 생성 직후 default formation이 자동으로 만들어지므로 데이터 노드를 바로 이 monitor에 등록할 수 있습니다.
생성 흐름
실습 compose의 monitor 서비스는 다음 명령을 사용합니다.
pg_autoctl create monitor \
--hostname monitor --pgport 5432 \
--ssl-self-signed --auth trust \
--run
--auth trust는 학습용입니다. 운영 환경에서는 scram-sha-256을 사용하고 autoctl_node의 비밀번호를 직접 설정해야 합니다. pg_autoctl은 비밀번호를 대신 생성하지 않습니다(8.3).
주요 옵션
| 옵션 | 의미 | 기본값 |
|---|---|---|
--pgdata | monitor PGDATA | 환경 변수 PGDATA |
--pgport | PostgreSQL 포트 | 5432 |
--hostname | 다른 노드가 monitor에 접속할 때 사용하는 이름이나 IP | 자동 계산 |
--auth | HBA 규칙에 사용할 인증 방식 | 없음(--skip-pg-hba가 없으면 필수) |
--skip-pg-hba | HBA 파일을 편집하지 않고 필요한 규칙을 로그로만 출력 | 꺼짐 |
--ssl-self-signed | self-signed 인증서 생성, 기본 --ssl-mode require | 꺼짐 |
--ssl-ca-file, --server-cert, --server-key | 직접 준비한 인증서 사용, 기본 --ssl-mode verify-full | 없음 |
--no-ssl | 암호화 없이 구성 (권장하지 않음) | 꺼짐 |
--run | 생성 후 바로 서비스 실행 | 꺼짐 |
--hostname을 생략하면 pg_autoctl이 값을 계산합니다. monitor는 8.8.8.8:53에 연결을 시도한 뒤 해당 연결에 사용된 로컬 IP를 구합니다. 이어서 역방향 DNS 조회와 정방향 조회 결과가 모두 일치하면 호스트명을 사용합니다. 일치하지 않으면 IP를 사용합니다. 외부 인터넷이 차단된 망에서는 이 계산이 예상과 다를 수 있습니다. 따라서 --hostname을 명시하는 편이 확실합니다.
monitor 데이터베이스와 사용자
생성이 끝나면 monitor에 pg_auto_failover 데이터베이스가 생성됩니다. 해당 데이터베이스의 pgautofailover 스키마에는 상태 테이블 세 개가 있습니다.
List of relations
Schema | Name | Type | Owner
----------------+-----------+-------+----------
pgautofailover | event | table | postgres
pgautofailover | formation | table | postgres
pgautofailover | node | table | postgres
| 사용자 | 용도 |
|---|---|
autoctl_node | 데이터 노드의 keeper가 monitor에 접속할 때 사용하는 사용자. node_active() 외에 failover, promotion, maintenance, 노드 삭제 함수 실행 권한도 받음 |
autoctl | monitor의 pg_autoctl 서비스(listener)가 로컬 PostgreSQL에 접속할 때 사용하는 사용자 |
v2.2 운영 문서는 보안상 autoctl_node로 maintenance 같은 관리 작업을 할 수 없다고 설명합니다. 하지만 실제 v2.2 extension SQL은 autoctl_node에 perform_failover, start_maintenance, remove_node 같은 함수의 실행 권한을 부여합니다. 따라서 이 설명은 현재 권한과 일치하지 않습니다(8.3). SQL로 formation을 직접 관리하려면 별도 사용자와 HBA 규칙을 만들거나 autoctl 규칙을 조정해야 합니다.
데이터 노드가 사용할 monitor URI는 pg_autoctl show uri로 확인합니다.
Type | Name | Connection String
-------------+---------+-------------------------------
monitor | monitor | postgres://autoctl_node@monitor:5432/pg_auto_failover?sslmode=require
monitor에 PGDATABASE를 주지 않는다
실습 첫 시도에서는 모든 서비스에 동일한 환경 변수(PGDATABASE=appdb)를 설정했습니다. 그 결과 monitor가 시작되지 못했습니다. monitor의 listener가 autoctl 사용자를 만들려고 로컬 PostgreSQL에 접속할 때 PGDATABASE 설정에 따라 appdb로 접속했기 때문입니다.
05:04:44 34 WARN Failed to connect to "postgres://@/var/run/postgresql:5432/appdb?", retrying until the server is ready
05:04:47 34 ERROR Connection to database failed: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: database "appdb" does not exist
05:04:47 34 ERROR Failed to create user "autoctl" on local postgres server
05:04:47 7 ERROR pg_autoctl service listener exited with exit status 12
05:04:47 7 INFO Restarting service listener
listener는 이 오류로 2~3초마다 재시작을 반복했고, pg_auto_failover 데이터베이스는 생성되지 않았습니다. monitor 서비스에서 PGDATABASE를 제거하고 볼륨을 삭제한 뒤 다시 만들었습니다. 그러자 1.0초 만에 pgautofailover.formation을 조회할 수 있었습니다.
monitor와 데이터 노드를 동일한 셸 프로필이나 compose 공통 블록으로 관리하면 환경 변수가 섞이기 쉽습니다. monitor에는 PGDATABASE를 설정하지 않습니다. 데이터 노드의 애플리케이션 DB는 환경 변수 대신 --dbname 옵션으로 지정하는 편이 안전합니다.
monitor GUC
failover를 판단하는 설정은 keeper가 아니라 monitor PostgreSQL의 GUC입니다. 실습 monitor에서 확인한 기본값은 다음과 같습니다.
select name, setting from pg_settings where name like 'pgautofailover.%' order by 1;
pgautofailover.enable_sync_wal_log_threshold|16777216
pgautofailover.health_check_max_retries|2
pgautofailover.health_check_period|5000
pgautofailover.health_check_retry_delay|2000
pgautofailover.health_check_timeout|5000
pgautofailover.node_considered_unhealthy_timeout|20000
pgautofailover.primary_demote_timeout|30000
pgautofailover.promote_wal_log_threshold|16777216
pgautofailover.startup_grace_period|10000
v2.2 설정 문서는 postgresql.conf나 ALTER DATABASE pg_auto_failover SET ...으로 값을 바꾼 뒤 reload하라고 안내합니다. 실습에서는 ALTER SYSTEM과 pg_reload_conf()로 값을 바꿨으며 재시작 없이 반영되었습니다. 각 값의 의미는 2.3에서 다룹니다. 값을 줄였을 때의 결과는 7.6에서 다룹니다.
monitor를 새로 만들면 GUC 조정 내용도 사라집니다. monitor 교체 절차(6.6)에는 GUC와 복제 설정을 다시 적용하는 단계를 포함해야 합니다.
정리
pg_autoctl create monitor는 PostgreSQL 인스턴스 하나에 pg_auto_failover 데이터베이스, extension, 사용자 두 개, HBA 규칙을 만듭니다. --auth(또는 --skip-pg-hba)와 SSL 선택(--ssl-self-signed, 인증서 파일 지정, --no-ssl 중 하나)은 반드시 지정해야 합니다. --hostname도 명시하는 편이 좋습니다. monitor에는 PGDATABASE를 설정하지 않습니다. failover 타이밍은 monitor의 pgautofailover.* GUC로 조정합니다.