repmgr是一个开源工具套件,用于管理PostgreSQL服务器集群中的复制和故障转移。它通过设置备用服务器、监控复制和执行管理任务(例如故障转移或手动切换操作)的工具增强了PostgreSQL的内置热备用功能。
PostgreSQL在9.0后引入了流复制架构,并且支持hot standby特性,并且在往后的几个版本中不断完善和增强流复制架构,repmgr为PostgreSQL的流复制机制提供了高级支持,因为它们是在9.0中引入的。当前的repmgr系列,repmgr 5,支持从PostgreSQL 9.3引入的复制功能的最新发展,例如级联复制、时间线切换和通过复制协议进行的基本备份。
repmgr作为一个开源工具,旨在用于灵活、便捷地管理PostgreSQL集群。
我们从https://dl.2ndquadrant.com/中找到对应PostgreSQL版本的RPM存储库,下载并安装,这里我们以PostgreSQL 11为例。
curl https://dl.2ndquadrant.com/default/release/get/11/rpm | sudo bash
验证存储库是否已安装sudo yum repolist,输出应该包含以下两个条目。
2ndquadrant-dl-default-release-pg11/7/x86_64 2ndQuadrant packages (PG11) for 7 - x86_64 18
2ndquadrant-dl-default-release-pg11-debug/7/x86_64 2ndQuadrant packages (PG11) for 7 - x86_64 - Debug 8
使用yum安装对应PostgreSQL版本的repmgr版本。
sudo yum install repmgr11
当然地,我们需要在两台服务器上都安装PostgreSQL软件和repmgr软件,都完成安装后,开始配置PostgreSQL。
配置postgresql.conf文件,添加或修改以下选项。
max_wal_senders = 10
max_replication_slots = 10
wal_level = 'hot_standby'
hot_standby = on
archive_mode = on
archive_command = '/bin/true'
启动数据库,在数据库中创建repmgr用户和repmgr数据库,并进入该数据库创建repmgr模式,将模式添加到search path中。
ALTER USER repmgr SET search_path TO repmgr, "$user", public;
配置ph_hba.conf白名单文件,允许repmgr有连接访问和复制的权限。
local replication repmgr trust
host replication repmgr 127.0.0.1/32 trust
host replication repmgr 192.168.22.129/24 trust
local repmgr repmgr trust
host repmgr repmgr 127.0.0.1/32 trust
host repmgr repmgr 192.168.22.129/24 trust
测试一下从节点端能否连通主节点。
psql 'host=192.168.22.128 user=repmgr dbname=repmgr connect_timeout=2'
接下来配置repmgr,在/etc/目录下编辑repmgr.conf,添加以下。
node_id=1
node_name='node1'
conninfo='host=192.168.22.128 user=repmgr dbname=repmgr password=repmgr connect_timeout=2'
data_directory='/pgdata/data/' #对应的PG数据目录
注册主节点服务。
$ repmgr -f /etc/repmgr.conf primary register
INFO: connecting to primary database...
NOTICE: attempting to install extension "repmgr"
NOTICE: "repmgr" extension successfully installed
NOTICE: primary node record (id: 1) registered
注册好后,我们来查看一下集群状态。
$ repmgr -f /etc/repmgr.conf cluster show
ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string
----+-------+---------+-----------+----------+----------+----------+----------+---------------------------------------------------------------------------------
1 | node1 | primary | * running | | default | 100 | 1 | host=192.168.22.128 user=repmgr dbname=repmgr password=repmgr connect_timeout=2
可以看到已经有主节点注册进来了,紧接着在备端服务器上注册从节点服务。在备端/etc/目录下编辑repmgr.conf,添加以下。
vi /etc/repmgr.conf
node_id=2
node_name='node2'
conninfo='host=192.168.22.129 user=repmgr dbname=repmgr password=repmgr connect_timeout=2'
data_directory='/pgdata/dataano' #选择一个空目录,否则会覆盖原有数据目录
使用–dry-run参数尝试克隆备用服务器。
repmgr -h 192.168.22.128 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --dry-run
NOTICE: destination directory "/pgdata/dataano" provided
INFO: connecting to source node
DETAIL: connection string is: host=192.168.22.128 user=repmgr dbname=repmgr
DETAIL: current installation size is 31 MB
INFO: "repmgr" extension is installed in database "repmgr"
INFO: replication slot usage not requested; no replication slot will be set up for this standby
INFO: parameter "max_wal_senders" set to 10
NOTICE: checking for available walsenders on the source node (2 required)
INFO: sufficient walsenders available on the source node
DETAIL: 2 required, 10 available
NOTICE: checking replication connections can be made to the source server (2 required)
INFO: required number of replication connections could be made to the source server
DETAIL: 2 replication connections required
NOTICE: standby will attach to upstream node 1
HINT: consider using the -c/--fast-checkpoint option
INFO: all prerequisites for "standby clone" are met
没有问题,去掉–dry-run参数再执行命令。
repmgr -h 192.168.22.128 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone
好了,备服务器成功搭建,我们直接启动服务。
$ pg_ctl -D $PGDATA start
服务启动以后,我们去主节点查看是否建立了流复制模式,可以看到主备已经处于同步状态。
我们可以手动的使用repmgr进行主备切换,我们在备节点执行,使得备节点切换成为主节点。
$ repmgr standby switchover -f /etc/repmgr.conf --siblings-follow
…
NOTICE: executing STANDBY FOLLOW on 1 of 1 siblings
INFO: STANDBY FOLLOW successfully executed on all reachable sibling nodes
NOTICE: switchover was successful
DETAIL: node "192.168.22.129" is now primary and node "192.168.22.128" is attached as standby
NOTICE: STANDBY SWITCHOVER has completed successfully
查看集群切换状态。
$ repmgr -f /etc/repmgr.conf cluster show
ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string
----+-------+---------+-----------+----------+----------+----------+----------+---------------------------------------------------------------------------------
1 | node1 | standby | * running | | default | 100 | 1 | host=192.168.22.128 user=repmgr dbname=repmgr password=repmgr connect_timeout=2
2 | node2 | primary | * running | | default | 100 | 1 | host=192.168.22.128 user=repmgr dbname=repmgr password=repmgr connect_timeout=2
PostgreSQL的高可用方案,基本上没有原生的,大多依靠第三方的工具来实现,repmgr来自第二象限公司,其产品的稳定性和支持度也是不错的,非常适合被考虑用来做生产上的高可用方案。
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/database/237322.html