PostgreSQL DB 冗長化

データベースの冗長化は難しいイメージがありましたが、非常に簡単に構築できるようになってきました。
マスタースレーブ型でスレーブに書き込みできない等の制限がありますが、レプリケーションクラスタというのを試してみました。

PostgreSQLのインストール

異なるバージョンの共存できないので、Postgresqlのアンインストールが必要です。すでに稼働中の場合はDBのバックアップを取ってからアンインストールします。

# yum -y remove postgresql-server postgresql postgresql-libs
 Removed:
 postgresql.x86_64 0:9.2.23-3.el7_4                                            
 postgresql-libs.x86_64 0:9.2.23-3.el7_4                                       
 postgresql-server.x86_64 0:9.2.23-3.el7_4                                     

Complete!

postgresql 9.5 のインストール

PostgreSQLコミュニティサイトにRPMが用意されているのでこれをインストールします。

# curl -L -O https://yum.postgresql.org/9.5/redhat/rhel-7-x86_ 64/postgresql95-9.5.11-1PGDG.rhel7.x86_64.rpm
# curl -L -O https://yum.postgresql.org/9.5/redhat/rhel-7-x86_64/postgresql95-libs-9.5.11-1PGDG.rhel7.x86_64.rpm
# curl -L -O https://yum.postgresql.org/9.5/redhat/rhel-7-x86_64/postgresql95-server-9.5.11-1PGDG.rhel7.x86_64.rpm

# rpm -Uvh postgresql95-9.5.11-1PGDG.rhel7.x86_64.rpm postgresql95-libs-9.5.11-1PGDG.rhel7.x86_64.rpm postgresql95-server-9.5.11-1PGDG.rhel7.x86_64.rpm

クラスタツールのインストール

# curl -L -O https://yum.postgresql.org/9.5/redhat/rhel-7-x86_64/repmgr95-4.0.2-1.rhel7.x86_64.rpm
# rpm -Uvh repmgr95-4.0.2-1.rhel7.x86_64.rpm

repmgrのセットアップ(マスタのみで実施)

repmgrの設定ファイルを修正します(修正箇所抜粋)

# vi /etc/repmgr/9.5/repmgr.conf
 node_id=1
 node_name='DB1'
 conninfo='host=DB1 user=repmgr dbname=repmgr'
 data_directory='/var/lib/pgsql/9.5/data'
 replication_user='repmgr'

PostgreSQLの起動(マスタのみで実施)

最低限必要な設定として、以下のような設定を行いました

 # vi /var/lib/pgsql/9.5/data/postgresql.conf
 listen_addresses = 'localhost,DB1'
 port = 5432
 wal_level = 'hot_standby'
 archive_mode = on
 archive_command = '/bin/true'
 max_wal_senders = 10
 wal_keep_segments = 5000
 hot_standby = on

修正できたらPostgresを起動します。

# su - postgres -c "/usr/pgsql-9.5/bin/pg_ctl -D /var/lib/pgsql/9.5/data -l logfile start"

repmgrがクラスタを管理するためのユーザとDBを作成します

(マスタのみで実施)

$ createuser -s repmgr
$ createdb repmgr -O repmgr 

pg_hba.confを修正します

# /var/lib/pgsql/9.5/data/pg_hba.conf
 # replication privilege.
 local   replication     repmgr                                trust
 host    replication     repmgr        127.0.0.1/32            trust
 host    replication     repmgr        ::1/128                 trust
 host    replication     repmgr        192.168.4.0/24          trust
 
 local   repmgr     repmgr                                trust
 host    repmgr     repmgr        127.0.0.1/32            trust
 host    repmgr     repmgr        ::1/128                 trust
 host    repmgr     repmgr        192.168.4.0/24          trust

repmgrユーザのsearch_pathを設定します

 $ psql repmgr -c 'ALTER ROLE repmgr SET search_path TO repmgr_db, "$user", public' -U repmgr

PostgreSQLを再起動

 $ /usr/pgsql-9.5/bin/pg_ctl -D /var/lib/pgsql/9.5/data -l logfile restart

repmgrにマスタノードを登録します(マスタのみで実施)

$ /usr/pgsql-9.5/bin/repmgr -f /etc/repmgr/9.5/repmgr.conf master register

登録された情報は以下のコマンドで確認することができます

$ psql repmgr -c 'SELECT * FROM nodes' -U repmgr -x
 -[ RECORD 1 ]----+--------------------------------------
 node_id          | 1
 upstream_node_id |
 active           | t
 node_name        | DB1
 type             | primary
 location         | default
 priority         | 100
 conninfo         | host=WEBDB2 user=repmgr dbname=repmgr
 repluser         | repmgr
 slot_name        |
 config_file      | /etc/repmgr/9.5/repmgr.conf

スタンバイ側のDBを作成します

まだPostgresは起動しません、既に起動した場合は停止して、「/var/lib/pgsql/9.5/data/」配下を削除します

$ /usr/pgsql-9.5/bin/repmgr -h WEBDB2 -U repmgr -d repmgr -D /var/lib/pgsql/9.5/data -f /etc/repmgr/9.5/repmgr.conf standby clone

スタンバイノードにはrecover.confが作られます

PostgreSQLを起動

「postgresql.conf」のリッスンアドレスを修正してPostgreSQLを起動します。

$ vi /var/lib/pgsql/9.5/data/postgresql.conf
listen_addresses = 'localhost,DB2'

$ /usr/pgsql-9.5/bin/pg_ctl -D /var/lib/pgsql/9.5/data -l logfile start

マスタの統計情報ビューを確認

$ psql repmgr -c 'SELECT * FROM nodes' -U repmgr -x
-[ RECORD 1 ]----+--------------------------------------
node_id          | 2
upstream_node_id |
active           | t
node_name        | DB1
type             | primary
location         | default
priority         | 100
conninfo         | host=DB1 user=repmgr dbname=repmgr
repluser         | repmgr
slot_name        |
config_file      | /etc/repmgr/9.5/repmgr.conf
-[ RECORD 2 ]----+--------------------------------------
node_id          | 3
upstream_node_id | 2
active           | t
node_name        | DB2
type             | standby
location         | default
priority         | 100
conninfo         | host=DB2 user=repmgr dbname=repmgr
repluser         | repmgr
slot_name        |
config_file      | /etc/repmgr/9.5/repmgr.conf

起動したスタンバイをrepmgrに登録

スタンバイサーバで以下のコマンドを実行します

$ /usr/pgsql-9.5/bin/repmgr -f /etc/repmgr/9.5/repmgr.conf standby register
 NOTICE: standby node "DB2" (id: 2) successfully registered

登録されたことを確認してみます

$ psql repmgr -c 'SELECT * FROM nodes' -U repmgr
 node_id | upstream_node_id | active | node_name |  type   | location | priority |               conninfo                | repluser | slot_name |         config_file
---------+------------------+--------+-----------+---------+----------+----------+---------------------------------------+----------+-----------+-----------------------------
       2 |                  | t      | DB1       | primary | default  |      100 | host=DB1 user=repmgr dbname=repmgr    | repmgr   |           | /etc/repmgr/9.5/repmgr.conf
       3 |                2 | t      | DB2       | standby | default  |      100 | host=DB2 user=repmgr dbname=repmgr    | repmgr   |           | /etc/repmgr/9.5/repmgr.conf
(2 rows)