暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL集群管理(一)——repmgr


作者:徐浩


近10年PostgreSQL/MySQL/Oracle/数据库乙方运维经验;美创科技运维负责人、资深DBA、资深架构师;中国开源软件推进联盟中国PG分会认证讲师;美创科技PGCA/PGCE/PGCM认证培训讲师;Oracle OCM、Oracle OCP、MySQL OCP、认证培训讲师;书籍《DBA攻坚指南:左手Oracle,右手MySQL》作者。


repmgr是一个开源工具套件,用于管理 PostgreSQL 服务器集群中的复制和故障转移。它通过设置备用服务器、监控复制和执行管理任务(例如故障转移或手动切换操作)的工具增强了 PostgreSQL 的内置热备用功能。


PostgreSQL在9.0后引入了流复制架构,并且支持hot standby特性,并且在往后的几个版本中不断完善和增强流复制架构,repmgr为 PostgreSQL 的流复制机制提供了高级支持,因为它们是在 9.0 中引入的。当前的repmgr系列, repmgr 5,支持从 PostgreSQL 9.3 引入的复制功能的最新发展,例如级联复制、时间线切换和通过复制协议进行的基本备份


repmgr作为一个开源工具,旨在用于灵活、便捷地管理PostgreSQL集群


老样子,我们先来安装repmgr软件


我们从https://dl.2ndquadrant.com/中找到对应PostgreSQL版本的RPM存储库,下载并安装,这里我们以PostgreSQL11为例


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,添加以下


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来自第二象限公司,其产品的稳定性和支持度也是不错的,非常适合被考虑用来做生产上的高可用方案。



预告 | 2021 PG亚洲大会12月与您相约
PG ACE计划的正式发布
三期PostgreSQL国际线上沙龙活动的举办
六期PostgreSQL国内线上沙龙活动的举办

中国PostgreSQL分会与腾讯云战略合作协议签订

中国PostgreSQL分会与美创科技战略合作协议签订
中国PostgreSQL分会与中软国际战略合作协议签订
中国PostgreSQL分会“走进”北京大学
中国PostgreSQL分会“走进”深圳大学
PGFans社区核心用户点亮计划

PostgreSQL 14.0 正式发布

深度报告:开源协议那些事儿

从“非主流”到“潮流”,开源早已值得拥有

Oracle中国正在进行新一轮裁员,传 N+6 补偿

PostgreSQL与MySQL版权比较

新闻|Babelfish使PostgreSQL直接兼容SQL Server应用程序

四年三冠,PostgreSQL再度荣获“年度数据库”

中国PostgreSQL分会入选工信部重点领域人才能力评价机构


更多新闻资讯行业动态技术热点,请关注中国PostgreSQL分会官方网站

https://www.postgresqlchina.com

中国PostgreSQL分会生态产品

https://www.pgfans.cn

中国PostgreSQL分会资源下载站

https://www.postgreshub.cn


点赞在看分享收藏

文章转载自开源软件联盟PostgreSQL分会,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论