PostgreSQL Repmgr集群
一、概述
repmgr是一套开源工具,用于管理PostgreSQL服务器群集内的复制和故障转移。它支持并增强了PostgreSQL的内置流复制,该复制流提供了一个读/写主服务器以及一个或多个只读备用数据库,其中包含主服务器数据库的近实时副本。可以设置热备份服务器、监控复制、执行管理任务(故障转移、手工切换等)。
Repmgr流复制管理系统有repmgr和repmgrd两个命令。其中repmgr命令实现对集群节点的管理,如注册主/备节点、Clone节点,Promote节点,Follow节点以及Switchover操作等;repmgrd命令用来启动repmgr系统的守护进程,用以对集群节点的监控。二、Repmgr术语
replication cluster复制集群:“复制群集”是指通过流复制连接的PostgreSQL服务器网络。
Node :节点是复制群集中的单个PostgreSQL服务器。
upstream node :上游节点备用服务器连接到的节点,以接收流复制。这是主服务器,或者在级联复制的情况下,是另一个备用服务器。
Failover:故障转移如果主服务器发生故障并且合适的备用服务器被提升为新的主服务器,则会发生此操作。该repmgrd守护进程支持自动切换到停机时间最小化。
Switchover:切换在某些情况下,例如硬件或操作系统维护,有必要使主服务器脱机;在这种情况下,有必要进行受控的切换,从而促进适当的备用数据库并以受控的方式从复制群集中删除现有的主数据库。所述repmgr命令行客户机提供此功能。
Fencing :击剑 在故障转移情况下,升级新的备用数据库后,至关重要的是,先前的主数据库不要意外地恢复联机,这会导致脑裂的情况。为了防止这种情况,应该将发生故障的主数据库与应用程序隔离开,即“隔离”。
witness server :见证服务器 repmgr提供了设置所谓的“见证服务器”的功能,以协助在故障转移情况下确定具有多个备用服务器的新主服务器。见证服务器本身不是复制群集的一部分,尽管它确实包含repmgr元数据架构的副本。
见证服务器的目的是提供“强制投票”,将复制群集中的服务器拆分到多个位置。如果位置之间的连接丢失,则见证服务器的存在或不存在将决定该位置的服务器是否升级为主服务器。这是为了防止出现“裂脑”的情况,在这种情况下,隔离的位置会将网络中断解释为(远程)主节点的故障并升级为(本地)备用节点。仅当 使用repmgrd时,才需要创建见证服务器。
三、Repmgr
用于执行管理任务的命令行工具,例如:
1 设置备用服务器
2 将备用服务器升级为主服务器
3 切换主备服务器
4 显示复制群集中服务器的状态
四、 repmgrd
守护程序,该守护程序主动监视复制群集中的服务器并执行以下任务:
1监视和记录复制性能
2通过检测主服务器的故障并升级最合适的备用服务器来执行故障转移
3将有关集群中事件的通知提供给用户定义的脚本,该脚本可以执行任务,例如通过电子邮件发送警报
五、 命令简介
命令简介
repmgr primary register 安装pg的repmgr扩展并注册为主节点
repmgr primary unregister 注销不活动的主节点
repmgr standby clone 从其他节点复制数据到从节点
repmgr standby register 注册从节点(添加从的信息到repmgr元数据)
repmgr standby unregister repmgr元数据中移除从的信息
repmgr standby promote 将从提升为主
repmgr standby follow 将从跟随新主
repmgr standby switchover 将从提升为主并将主降级为从
repmgr witness register 注册一个观察节点
repmgr witness unregister 移除一个观察节点
repmgr node status 显示节点的基本信息和复制状态
repmgr node check 从复制的角度对节点进行健康监测
repmgr node rejoin 重新加入一个失效节点到集群
repmgr cluster show 显示所有集群中注册的节点信息
repmgr cluster matrix 在所有节点运行show并汇总
repmgr cluster crosscheck 在节点间两两交叉监测连接
repmgr cluster event 输出时间记录
repmgr cluster cleanup 清理监控历史
六、 Repmgr 快速入门指南
我们假设node1使用IP地址 主服务器192.168.1.11,而node2 使用IP地址备用服务器192.168.1.12.
1 repmgr设置基本复制群集的先决条件
两台服务器需要相同的PostgreSQL和repmgr版本,使用5432端口,还需要在两台服务器之间建立无密码的SSH连接,并且 应该安装rsync。
2 主服务器上面实例参数
max_wal_senders = 10 同时允许几个流复制协议的连接,根据实际需求设定 ,可以设置一个默认值例如64
max_replication_slots = 10 replication slot作用是保证wal没有同步到standby之前不能从pg_xlog移走公式: max_replication_slots=max_wal_senders 。
wal_level = 'hot_standby' 在云星宇是wal_level = logical
hot_standby = on 说明这台机器不仅仅是用于数据归档,也用于数据查询
archive_mode = on 开启归档
archive_command ='cp %p /usr/local/pgsql/pg_archive/%f'
3 Create the repmgr user and database
createuser -s repmgr
createdb repmgr -O repmgr
4. Configuring authentication in pg_hba.conf
5 备机准备
也没有创建一个PostgreSQL实例(即不执行initdb的或由该软件包提供的数据库创建脚本)
do not create a PostgreSQL instance (i.e. do not execute initdb or any database creation scripts provided by packages)
standby using psql:
psql 'host=192.168.1.11 user=repmgr dbname=repmgr connect_timeout=2'
6 repmgr configuration file
Create a repmgr.conf file on the primary server. The file must contain at least the following parameters:
7 注册主服务器
To enable repmgr to support a replication cluster, the primary node must be registered with repmgr. This installs the repmgr extension and metadata objects, and adds a metadata record for the primary server:
要使repmgr支持复制集群,必须向repmgr注册主节点。这将安装repmgr 扩展和元数据对象,并添加主服务器的元数据记录:
repmgr -f /etc/repmgr.conf primary register
验证集群状态
[postgres@pg11m ~]$ repmgr -f /etc/repmgr.conf cluster show
8 克隆主实例到备实例
repmgr.conf在备用服务器上 创建一个文件。它必须至少包含相同的参数作为主要的repmgr.conf,但与强制值node,node_name,conninfo
Use the --dry-run option to check the standby can be cloned:
检查备库是否可以克隆
repmgr -h node1(192.168.1.11) -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --dry-run
如果没有报错,开始克隆
repmgr -h node1 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone
遇见特殊情况可以加强制--force
repmgr -h 192.168.247.133 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --force
9 验证流复制是否正常
Connect to the primary server and execute:
repmgr=# SELECT * FROM pg_stat_replication;
检查备库状态
repmgr=# SELECT * FROM pg_stat_replication;
10 注册备库
$ repmgr -f /etc/repmgr.conf standby register
$ repmgr -f /etc/repmgr.conf cluster show
出现特殊情况,可以加--force
repmgr -f /etc/repmgr.conf standby register –force
11 生产环境还需要pgpass
.pgpass 是 连接 postgresql 时使用的密码文件,通常位置为 ~/.pgpass。
在使用某些组件时还真的必须使用。具体的格式为:
hostname:port:database:username:password
~/.pgpass 上的权限必须不允许所有人或组内访问,可以用命令chmod 0600 ~/.pgpass 实现。如果权限没有这么严格,该文件将被忽略。
[postgres@pg11s ~]$ chmod 0600 ~/.pgpass
[postgres@pg11s ~]$ cat .pgpass
192.168.198.129:5432:repmgr:repmgr:repmgr
192.168.198.130:5432:repmgr:repmgr:repmgr
七 repmgr管理
1克隆主实例到备实例standby clone
repmgr.conf在备用服务器上 创建一个文件。它必须至少包含相同的参数作为主要的repmgr.conf,但与强制值node,node_name,conninfo
Use the --dry-run option to check the standby can be cloned:
检查备库是否可以克隆
repmgr -h node1(192.168.1.11) -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --dry-run
如果没有报错,开始克隆
repmgr -h node1 -U repmgr -d repmgr -f /etc/repmgr.conf
standby clone
如果出现特殊的情况可以使用强制选项
repmgr -h 192.168.247.133 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --force
2 升级备用服务器为主服务器 standby promote
如果主服务器出现故障或需要从复制群集中删除,则必须指定新的主服务器,以确保群集继续正常运行。这可以通过repmgr standby promote完成。
repmgr -f /etc/repmgr.conf cluster show
在备用服务器上面执行
repmgr -f /etc/repmgr.conf standby promote
但是,唯一剩余的备用数据库(node3)仍在尝试从发生故障的主数据库复制;现在必须执行repmgr Standby Follow
3 备用服务器重新找主服务器Standby Follow
在复制群集的现有主服务器发生故障或删除之后,repmgr Standby Follow可用于使“孤立”备用数据库跟随新的主数据库并追赶其当前状态。
repmgr -f /etc/repmgr.conf standby follow
repmgr -f /etc/repmgr.conf cluster show
4备用服务器repmgr Standby switchover执行切换
复制的典型用例是主服务器和备用服务器的组合,备用服务器用作备份,在主服务器出现问题时可以轻松激活备用服务器。通常,可以通过提升备用数据库来处理这种计划外的故障转移,然后必须采取适当的措施来恢复旧的主数据库。
1 切换前测试
repmgr standby switchover -f /etc/repmgr.conf --siblings-follow --dry-run
2 切换standby switchover
repmgr -f /etc/repmgr.conf cluster show
repmgr standby switchover -f /etc/repmgr.conf --siblings-follow
repmgr -f /etc/repmgr.conf cluster show
5 将失效的节点加入集群repmgr node rejoin
repmgr node rejoin -d '$conninfo'
su - postgres -c "/usr/local/pgsql/bin/repmgr node rejoin -d 'host=192.168.91.133 user=repmgr dbname=repmgr connect_timeout=2' --force-rewind --config-files=postgresql.conf"
--config-files前pg_rewind将用源节点中的文件覆盖本地节点的配置文件,因此建议使用此选项以确保保留它们。
10.4 repmgrd管理
10.4.1 repmgrd概述
repmgrd(“ replication manager daemon”)是一个管理和监视守护程序,它在复制群集中的每个节点上运行。它可以自动执行诸如故障转移和更新备用数据库等操作以遵循新的主要数据库,并提供有关每个备用数据库状态的监视信息。
八repmgrd示范
1主机 在主状态
repmgr -f /etc/repmgr.conf cluster show
2 主机关闭,后查看状态
/usr/local/pgsql/bin/pg_ctl stop -D /usr/local/pgsql/data -m immediate
repmgr -f /etc/repmgr.conf cluster show
九监控语句
1 查看repmgr状态
repmgr -f /etc/repmgr.conf cluster show
2 监控复制槽状态
SELECT node_id, upstream_node_id, active, node_name, type, priority, slot_name FROM repmgr.nodes ORDER BY node_id;
SELECT slot_name, slot_type, active, active_pid FROM pg_replication_slots ;
3 查看比对主从lsn的信息是否相同
进入从库检查,检查第一个数值是不是为0.
select case when pg_last_wal_receive_lsn() = pg_last_wal_replay_lsn() then 0 else EXTRACT(EPOCH FROM now() - pg_last_xact_replay_timestamp()) end as replication_lag,pg_last_wal_receive_lsn(),pg_last_wal_replay_lsn() ,now(),pg_last_xact_replay_timestamp();
4 查看日志
日志位置 log_file='/usr/local/pgsql/data/repmgrd.log'#全路径+文件名最好在data目录下 涉及权限问题 全路径
tail -f /usr/local/pgsql/data/repmgrd.log
5 查看事件
SELECT * from repmgr.events ;
十实际问题
把主的PG数据库关闭在开启以后,就会出现,两边都是主的现象。
1 双主处理(克隆数据笨方法)
1 主机死机,强制辅助变主
repmgr -f /etc/repmgr.conf cluster show
2 停止辅助数据与repmgr服务
停止辅助数据库
/usr/local/pgsql/bin/pg_ctl stop -D /usr/local/pgsql/data
[postgres@pg11m ~]$ ps -ef | grep rep
postgres 9188 1 0 3月21 ? 00:00:00 repmgrd -f /etc/repmgr.conf --pid-file /tmp/repmgrd.pid --daemonize
postgres 10670 10269 0 00:01 pts/0 00:00:00 grep --color=auto rep
[postgres@pg11m ~]$ kill -9 9188
3 辅助机器强制克隆主数据库变辅助 加--force
[postgres@pg11s ~]$ repmgr -h 192.168.247.133 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --force
提示
HINT: for example: pg_ctl -D /usr/local/pgsql/data start
HINT: after starting the server, you need to re-register this standby with "repmgr standby register --force" to update the existing node record
4 初始化数据库与强制注册备库
pg_ctl -D /usr/local/pgsql/data start
[postgres@pg11s ~]$ repmgr -f /etc/repmgr.conf standby register --force
INFO: connecting to local node "192.168.247.134" (ID: 2)
INFO: connecting to primary database
INFO: standby registration complete
NOTICE: standby node "192.168.247.134" (ID: 2) successfully registered
5 repmgrd正常
2 双主处理(简便方法)
1 主机死机,强制辅助变主
repmgr -f /etc/repmgr.conf cluster show
2 停止辅助130变主,强制加入129
ERROR: this node's timeline is ahead of the rejoin target node's timeline
DETAIL: this node's timeline is 2, rejoin target node's timeline is 1
报时间线问题
[postgres@pg11s ~]$ pg_ctl stop -D /usr/local/pgsql/data
waiting for server to shut down.... done
server stopped
[postgres@pg11s ~]$ ps -ef | grep rep
postgres 13207 1 0 22:29 ? 00:00:00 repmgrd -f /etc/repmgr.conf --pid-file /tmp/repmgrd.pid --daemonize
postgres 19322 19043 0 22:41 pts/0 00:00:00 grep --color=auto rep
[postgres@pg11s ~]$ kill -9 13207
[postgres@pg11s ~]$ repmgr node rejoin -d 'host=192.168.198.129 user=repmgr dbname=repmgr connect_timeout=2' --force-rewind --config-files=postgresql.conf
ERROR: this node's timeline is ahead of the rejoin target node's timeline
DETAIL: this node's timeline is 2, rejoin target node's timeline is 1
3 强制129变副加入130
[postgres@pg11m ~]$ /usr/local/pgsql/bin/pg_ctl stop -D /usr/local/pgsql/data
waiting for server to shut down.... done
server stopped
[postgres@pg11m ~]$ ps -ef | grep rep
postgres 12836 1 0 22:36 ? 00:00:00 repmgrd -f /etc/repmgr.conf --pid-file /tmp/repmgrd.pid --daemonize
postgres 19245 18924 0 22:48 pts/0 00:00:00 grep --color=auto rep
[postgres@pg11m ~]$ kill -9 12836
[postgres@pg11m ~]$ ps -ef | grep rep
postgres 19253 18924 0 22:48 pts/0 00:00:00 grep --color=auto rep
[postgres@pg11m ~]$ repmgr node rejoin -d 'host=192.168.198.130 user=repmgr dbname=repmgr connect_timeout=2' --force-rewind --config-files=postgresql.conf
NOTICE: pg_rewind execution required for this node to attach to rejoin target node 2
DETAIL: rejoin target server's timeline 2 forked off current database system timeline 1 before current recovery point 2/A000028
NOTICE: executing pg_rewind
DETAIL: pg_rewind command is "/usr/local/pgsql/bin/pg_rewind -D '/usr/local/pgsql/data' --source-server='host=192.168.198.130 user=repmgr dbname=repmgr connect_timeout=2'"
NOTICE: 1 files copied to /usr/local/pgsql/data
NOTICE: setting node 1's upstream to node 2
WARNING: unable to ping "host=192.168.198.129 user=repmgr dbname=repmgr connect_timeout=2"
DETAIL: PQping() returned "PQPING_NO_RESPONSE"
NOTICE: starting server using "/usr/local/pgsql/bin/pg_ctl -w -D '/usr/local/pgsql/data' start"
NOTICE: NODE REJOIN successful
DETAIL: node 1 is now attached to node 2
[postgres@pg11m ~]$ repmgr -f /etc/repmgr.conf cluster show
ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string
----+-----------------+---------+-----------+-----------------+----------+----------+----------+------------------------------------------------------------------
1 | 192.168.198.129 | standby | running | 192.168.198.130 | default | 100 | 1 | host=192.168.198.129 user=repmgr dbname=repmgr connect_timeout=2
2 | 192.168.198.130 | primary | * running | | default | 100 | 2 | host=192.168.198.130 user=repmgr dbname=repmgr connect_timeout=2
4 切换129变主
repmgr standby switchover -f /etc/repmgr.conf --siblings-follow
PostgreSQL Repmgr集群的更多相关文章
- initdb - 创建一个新的 PostgreSQL数据库集群
SYNOPSIS initdb [ option...] --pgdata | -D directory DESCRIPTION 描述 initdb 创建一个新的 PostgreSQL 数据库集群. ...
- 云原生分布式 PostgreSQL+Citus 集群在 Sentry 后端的实践
优化一个分布式系统的吞吐能力,除了应用本身代码外,很大程度上是在优化它所依赖的中间件集群处理能力.如:kafka/redis/rabbitmq/postgresql/分布式存储(CephFS,Juic ...
- Centos7 源码安装PostgreSQL Citus集群 (转载)
citus的分布式集群目前在苏宁大规模应用,苏宁陈华军也做了很多技术分享和博客介绍.目前所有的教程都是rpm和pg一起安装,个人不喜欢,毕竟citus定位是个插件,我想在我已安装的pg上源码装一个ci ...
- postgresql分布式集群之citus简介(转载)
一.Citus是什么 citus是PG的一个sharding插件,可以把PG变成一个分布式数据库.目前在苏宁有大量的生产应用跑在citus+pg的环境中.大家可以看it大咖视频. citus是一款基于 ...
- pg_dumpall - 抽出一个 PostgreSQL 数据库集群到脚本文件中
SYNOPSIS pg_dumpall [ option...] DESCRIPTION 描述 pg_dumpall 是一个用于写出("转储")一个数据库集群里的所有 Postgr ...
- pg_resetxlog - 重置一个 PostgreSQL 数据库集群的预写日志以及其它控制内容
SYNOPSIS pg_resetxlog [ -f ] [ -n ] [ -o oid] [ -x xid] [ -l fileid,seg] datadir DESCRIPTION 描述 pg_r ...
- PostgreSQL集群方案相关索引页
磨砺技术珠矶,践行数据之道,追求卓越价值 返回顶级页:PostgreSQL索引页 本页记录所有本人所写的PostgreSQL的集群方案相关文摘和文章的链接: pgpool-II: 1 pgpool-I ...
- 命令行部署repmgr管理集群+switchover+切换测试
本次部署未使用securecmd/kbha工具.无需普通用户到root用户的互信. 建立系统数据库安装用户组及用户,在所有的节点执行 root用户登陆服务器,创建用户组及用户并且设置密码 [root@ ...
- gitlab HA集群
https://docs.gitlab.com/ee/administration/high_availability/gitlab.html https://about.gitlab.com/hig ...
- KingbaseES V8R6集群维护案例之--单实例数据迁移到集群案例
案例说明: 生产环境是单实例,测试环境是集群,现需要将生产环境的数据迁移到集群中运行,本文档详细介绍了从单实例环境恢复数据到集群环境的操作步骤,可以作为生产环境迁移数据的参考. 适用版本: Kingb ...
随机推荐
- mysql基础问题三问(底层逻辑;正在执行;日志观察)
背景:经常面试会遇到且实际工作中也会应用到的三个场景: 目录: 一.mysql查询时的底层原理是什么? 二.如何查看正在执行的mysql语句? 三.如何观察mysql运行过程中的日志信息? - - - ...
- openpyxl写数据
import osimport openpyxlos.chdir(r'D:/openpyxl') wb = openpyxl.Workbook() sht = wb.create_sheet('dat ...
- 纷繁复杂见真章,华为云产品需求管理利器CodeArts Req解读
摘要:到底什么是需求?又该如何做好需求管理? 本文分享自华为云社区<纷繁复杂见真章,华为云产品需求管理利器 CodeArts Req 解读>,作者:华为云头条 . 2022 年 8 月,某 ...
- mooc上的简单题,(疑惑已解决)
如题,在简单不过的题,在自己编译器上试了很多例子,输出结果都对.但是oj不给过:(已解决) 统计指定单词出现的次数(10分) 题目内容:输入6个单词,查找第6个单词在前5个单词中出现的次数. 输入:6 ...
- python @property的介绍与使用
python @property的介绍与使用 python的@property是python的一种装饰器,是用来修饰方法的. 作用: 我们可以使用@property装饰器来创建只读属性,@proper ...
- Qt栅格布局、ScrollArea和用户选择界面
用户选择界面 就我们在实际开发的时候可能需要面对这样一个界面 做个demo试试看 其实我们可以分解一下这个界面 就是除了控制相关的内容,最主要的就是这个界面之上,有一个个动态的输入的控件,上面都是学生 ...
- LeetCode HOT 100:组合总和
题目:39. 组合总和 题目描述: 给你一个没有重复元素的数组,和一个target目标值,返回数组中可以使数字和为目标数target的所有不同组合.什么叫组合?组合就是数组中任意数字组成的集合,不需要 ...
- Chaos 测试下的若干 NebulaGraph Raft 问题分析
Raft 是一种广泛使用的分布式共识算法.NebulaGraph 底层采用 Raft 算法实现 metad 和 storaged 的分布式功能.Raft 算法使 NebulaGraph 中的 meta ...
- LFU 的设计与实现
LFU 的设计与实现 作者:Grey 原文地址: 博客园:LFU 的设计与实现 CSDN:LFU 的设计与实现 题目描述 LFU(least frequently used).即最不经常使用页置换算法 ...
- 在windows上构建OpenCascade
基于作者QuaoarsWorkshop的视频Open Cascade Lessons,讲的非常详细,观看需要魔法 什么是OCCT?. 首先,Open CASCADE Technology SDK 是一 ...