ThingWorx 高可用性 > Pgpool-II を使用した PostgreSQL HA の展開例 > 4. ストリーミングレプリケーション用データベースの node0 としての設定
4. ストリーミングレプリケーション用データベースの node0 としての設定
最良事例として、レプリケーションアクセスのための専用ユーザーを作成します。
ステップ 1
ユーザー replicator を作成します。
psql
CREATE USER replicator REPLICATION LOGIN CONNECTION LIMIT 5
PASSWORD 'trUf6yuz2?_Gub';
\q
ステップ 2
レプリケーションをサポートするために postgresql.conf を修正します。
vim /db/postgres/postgresql.conf
次のパラメータを設定します。
node0 の postgres.conf パラメータ
パラメータ
ノード
1
listen_addresses
*
* ではすべてのアドレスが含まれます。セキュリティの要件に基づいてこの値を修正します。
2
port
5432
3
max_connections
150
4
archive_mode
on
5
wal_level
hot_standy
6
Fsync
on
7
synchronous_commit
on
8
wal_sync_method
open_sync
9
max_wal_sendors
10
10
synchronous_standby_names
'node1,node2'
11
hot_standby
on
12
hot_standby_feedback
on
13
wal_keep_segments
500
14
max_replication_slots
10
15
archive_command
'cp -f %p /db/node0archive/%f</dev/null'
16
checkpoint_segments
3
ステップ 3
start_replication スクリプトを実行します。
start_replication.sh スクリプトを使用して、node0 でレプリケーションプロセスを開始します。
vim /db/bin/start_replication.sh
コンテンツは以下のようにします。
cp /db/postgres/postgresql.conf /db/bin/postgresql.conf
cp /db/postgres/pg_hba.conf /db/bin/pg_hba.conf
rm -rf /db/postgres
rm -rf /db/thingworx/*
pg_basebackup -h $1 -D /db/postgres -U replicator --xlog-method=stream
-v -P
cp /db/bin/postgresql.conf /db/postgres/postgresql.conf
cp /db/bin/pg_hba.conf /db/postgres/pg_hba.conf
ステップ 4
node0 に対して retargetMaster.sh スクリプトを実行します。
script retargetMaster.sh を作成します。
vim /db/bin/retargetMaster.sh
コンテンツは以下のようにします。
#!/bin/bash -x
NEW_MASTER_HOSTNAME=$1
NEW_ARCHIVE_FOLDER=$2
if grep -q "$1" /db/postgres/recovery.conf
then
echo $1 is current target,server does not need a restart, done!
else
#service postgresql stop
/usr/pgsql-10.x/bin/pg_ctl -D /db/postgres stop -m fast
#/db/bin/start_replication.sh $NEW_MASTER_HOSTNAME
# Write out new recovery.conf file
rm /db/postgres/recovery.conf
echo "standby_mode = 'on'" >> /db/postgres/recovery.conf
echo "primary_conninfo = 'port=5432 host=$NEW_MASTER_HOSTNAME user=replicator password=trUf6yuz2?_Gub sslmode=disable sslcompression=1 application_name=node0'" >> /db/postgres/recovery.conf
echo "trigger_file='/db/trigger'" >> /db/postgres/recovery.conf
echo "restore_command = 'cp -f $NEW_ARCHIVE_FOLDER/%f %p>/db/postgres/recovery.conf
echo "archive_cleanup_command = '/usr/bin/pg_archivecleanup $NEW_ARCHIVE_FOLDER %r>/db/postgres/recovery.conf
echo "recovery_target_timeline = 'latest'" >> /db/postgres/recovery.conf
/usr/pgsql-10.x/bin/pg_ctl -D /db/postgres -l /db/postgres.log start
echo target is $1 now
fi
次に、スクリプトを実行可能にします。
chmod a+x /db/bin/start_replication.sh
chmod a+x /db/bin/reargetMaster.sh
ステップ 5
pg_hba の設定を更新します。
vim /db/postgres/pg_hba.conf
以下の行を更新します。
host all all 10.10.0.0/16 trust
host replicator replicator 10.10.0.0/16 trust
上記の host の設定はセキュリティの要件によって異なる場合があります。たとえば、trust の代わりに md5 を認証に使用できます。