2&>1

AWSとかGCPとかGolangとかとか

Postgresql12とrepmgerでレプリケーション構成を組んだ

前提

構築予定図

f:id:piyojir0:20200402174131j:plain

OS: CentOS 8

DB: Postgresql 12

tool: repmager

2台構成でマスタースレーブのみ。

name IPアドレス role apps
PG-Node1 192.168.26.126 Primary PostgreSQL 12 and repmgr
PG-Node2 192.168.26.127 Standby PostgreSQL 12 and repmgr

英語版参考資料

How to Automate PostgreSQL 12 Replication and Failover with repmgr - Part 1 - 2ndQuadrant | PostgreSQL

構築

プライマリーサーバの設定

各サーバに対してpostgresql12はインストール済みとする。

ただしPG-node1のみ /usr/pgsql-12/bin/postgresql-12-setup initdb が実行された状態 (PG-node2はインストールしただけ。すでに行った場合はrm -rf /var/lib/pgsql/12/data/*)

PG-node1の「postgresql.conf」に以下を追記

listen_addresses = '*' 
max_wal_senders = 10
max_replication_slots = 10
wal_level = 'replica'
hot_standby = on
archive_mode = on
archive_command = '/bin/true'

追記後postgresqlの再起動アンド自動起動設定

systemctl restart postgresql-12.service
systemctl enable postgresql-12.service

repmgrのインストール

PG-node1とPG-node2で以下を実行

wget https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
dnf -y install repmgr12 

PG-node01のみで以下実行

su - postgres
createuser --superuser repmgr
createdb --owner=repmgr repmgr
psql -c "ALTER USER repmgr SET search_path TO repmgr, public;"

PG-node01の「postgres.conf」に以下追記

hared_preload_libraries = 'repmgr'

repmgrのコンフィグ設定

対象ファイル:/etc/repmgr/12/repmgr.conf

PG-node1に以下追記

node_id=1
node_name='PG-Node1'
conninfo='host=192.168.26.126 user=repmgr dbname=repmgr 
connect_timeout=2'
data_directory='/var/lib/pgsql/12/data'

PG-node2に以下追記

node_id=2
node_name='PG-Node2'
conninfo='host=192.168.26.127 user=repmgr dbname=repmgr 
connect_timeout=2'
data_directory='/var/lib/pgsql/12/data'

host=IP は環境によって変更すること

pg_hda.confの変更

PG-node1で以下追記

local   replication     repmgr                              trust
host    replication     repmgr      127.0.0.1/32            trust
host    replication     repmgr      192.168.26.126/32             trust
host    replication     repmgr      192.168.26.127/32             trust

local   repmgr          repmgr                              trust
host    repmgr          repmgr      127.0.0.1/32            trust
host    repmgr          repmgr      192.168.26.126/32             trust
host    repmgr          repmgr      192.168.26.127/32             trust

postgresqlの再起動

 systemctl restart postgresql-12.service

PG-node2から以下を実行して疎通できると問題なし

psql 'host=192.168.26.126 user=repmgr dbname=repmgr connect_timeout=2'

これが出るとOK repmgr>

repmgrへのプライマリーノード登録

PG-node1で以下実行

su - postgres
/usr/pgsql-12/bin/repmgr -f /etc/repmgr/12/repmgr.conf master register
/usr/pgsql-12/bin/repmgr -f /etc/repmgr/12/repmgr.conf cluster show

最後のコマンドで以下が出るとOK

 ID | Name     | Role    | Status    | Upstream | Connection string
----+----------+---------+-----------+----------+--------------------
 1  | PG-Node1 | primary | * running |          | host=PG-Node1 dbname=repmgr user=repmgr connect_timeout=2

repmgrでのレプリケーション設定

PG-node2で以下実行

su - postgres
/usr/pgsql-12/bin/repmgr -h 16.0.1.156 -U repmgr -d repmgr -f /etc/repmgr/12/repmgr.conf standby clone --dry-run

これが出力されたら問題なし

INFO: all prerequisites for "standby clone" are met

PG-node2のPostgresqlを起動する

systemctl start postgresql-12.service
systemctl enable postgresql-12.service

PG-node2で以下を実行しスタンバイノード登録をする

su - postgres
/usr/pgsql-12/bin/repmgr -f /etc/repmgr/12/repmgr.conf standby register

こういうのが出れば多分成功

INFO: connecting to local node "PG-Node2" (ID: 2)
INFO: connecting to primary database
WARNING: --upstream-node-id not supplied, assuming upstream node is primary (node ID 1)
INFO: standby registration complete
NOTICE: standby node "PG-Node2" (ID: 2) successfully registered

PG-node2から実際登録されているかを以下コマンドで確認する

/usr/pgsql-12/bin/repmgr -f /etc/repmgr/12/repmgr.conf cluster show --compact

こうでれば成功

ID | Name     | Role    | Status    | Upstream | Location | Prio. | TLI
----+----------+---------+-----------+----------+----------+-------+-----
 1  | PG-Node1 | primary | * running |          | default  | 100   | 1
 2  | PG-Node2 | standby |   running |          | default  | 100   | 1

ここまででマスタースレーブ構成のレプリケーション完成

次は自動フェイルオーバー設定を行う

次の記事

Postgresql12とrepmgerで自動フェイルオーバー構成を組んだ - 2&>1