os:ubunbu 16.04
postgresql:9.6.8
repmgr:4.1.1
192.168.56.101 node1
192.168.56.102 node2
安装 postgresql 9.6 软件
node1、node2都需要使用 apt install 安装 postgreql9.6.8,最后一位代表的是补丁号
# apt install postgresql-plpython-9.6 postgresql-plperl-9.6 postgresql-pltcl-9.6 postgresql-doc-9.6 postgresql-contrib-9.6 postgresql-client-9.6 postgresql-client-common postgresql-common postgresql-9.6 postgresql-server-dev-9.6 libpq-dev libpq5
使用 apt 或者 yum 的好处就是依赖包都会被安装上,比较省心些。
安装好后都会自动initdb初始化数据库集群,需要主要的是node2上的PG_DATA下的文件需要删除,因为数据需要从node1同步。
更多详细过程略。
安装 repmgr
node1、node2都需要安装2ndQuadrant public apt repository for Debian/Ubuntu
# curl https://dl.2ndquadrant.com/default/release/get/deb | sudo bash
# apt install postgresql-9.6-repmgr repmgr-common
创建user、database
node1 上操作
$ createuser -s repmgr
$ createdb repmgr -O repmgr
$ psql -c "alter user repmgr with password 'repmgrrepmgr'"
$ psql -c "alter user repmgr set search_path to repmgr, "$user", public";
修改 pg_hba.conf
node1、node2都需要操作,其实stream replication环境的各个节点的该配置文件应该相同,方便切换.
$ vi /etc/postgresql/9.6/main/pg_hba.conf
local repmgr repmgr trust
host repmgr repmgr 127.0.0.1/32 trust
host repmgr repmgr 192.168.56.0/24 trust
local replication repmgr trust
host replication repmgr 127.0.0.1/32 trust
host replication repmgr 192.168.56.0/24 trust
修改postgresql.conf
node1、node2都需要操作,其实stream replication环境的各个节点的该配置文件应该相同,方便切换.
需要设置成 stream replicator 建议的参数,有个参数需要注意修改下,免得后面修改后又要重启实例。
$ vi /etc/postgresql/9.6/main/postgresql.conf
shared_preload_libraries = 'repmgr,pg_stat_statements'
/etc/repmgr.conf
node1 节点的文件内容如下:
$ vi /etc/repmgr.conf
node_id=1
node_name=node1
conninfo='host=192.168.56.101 user=repmgr dbname=repmgr connect_timeout=2'
data_directory='/var/lib/postgresql/9.6/main'
pg_bindir=/usr/lib/postgresql/9.6/bin
node2 节点的文件内容如下:
$ vi /etc/repmgr.conf
node_id=2
node_name=node2
conninfo='host=192.168.56.102 user=repmgr dbname=repmgr connect_timeout=2'
data_directory='/var/lib/postgresql/9.6/main'
pg_bindir=/usr/lib/postgresql/9.6/bin
Register the primary server
node1 节点上操作
$ repmgr -f /etc/repmgr.conf primary register
$ repmgr -f /etc/repmgr.conf cluster show
$ psql -U repmgr
repmgr=# SELECT * FROM repmgr.nodes;
Clone the standby server
node2 节点上操作
$ repmgr -h 192.168.56.101 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --dry-run
$ repmgr -h 192.168.56.101 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone
–dry-run 意思就是
--dry-run show what would happen for action, but don't execute it
Verify replication is functioning
node1 节点上查看
$ psql -U repmgr
repmgr=# SELECT * FROM pg_stat_replication;
node2 节点上查看
$ psql -U repmgr
repmgr=# select * from pg_stat_wal_receiver;
Register the standby
$ repmgr -f /etc/repmgr.conf standby register
$ repmgr -f /etc/repmgr.conf cluster show
ID | Name | Role | Status | Upstream | Location | Connection string
----+-------+---------+-----------+----------+----------+-----------------------------------------------------------------
1 | node1 | primary | * running | | default | host=192.168.56.101 user=repmgr dbname=repmgr connect_timeout=2
2 | node2 | standby | running | node1 | default | host=192.168.56.102 user=repmgr dbname=repmgr connect_timeout=2
可以看到 cluster show 的结果符合预期。
下一篇blog 介绍手动 repmgr的 switchover
参考:
https://repmgr.org/docs/4.1/index.html