postgres主从部署(repmgr故障自动切换)

基础信息:

节点角色pg版本节点IPos版本pg安装方式
node1primary14.1210.204.121.214Ubuntu2204apt
node2standby14.1210.204.121.214Ubuntu2204apt

安装pg(所有节点)

sudo apt update
sudo apt install postgresql-14 -y
sudo systemctl status postgresql@14-main.service
sudo -u postgres psql -c "SELECT version();"

安装repmgr(所有节点)

下面安装的是14匹配的repmgr,如果是其他版本把14换成其他版本即可

sudo apt-get install postgresql-14-repmgr -y

配置主库node1

修改配置文件 /etc/postgresql/14/main/postgresql.conf

sed -i '/cluster_name/s/14\/main/node1/' /etc/postgresql/14/main/postgresql.conf
cat <<EOF >> /etc/postgresql/14/main/postgresql.conf
listen_addresses = '*'
max_wal_senders = 10
max_replication_slots = 10
wal_level = 'hot_standby'
hot_standby = on
archive_mode = on   # repmgr 本身不需要 WAL 文件归档。
archive_command = '/bin/true'
shared_preload_libraries = 'repmgr'
wal_log_hints = 'on'
EOF
  1. 创建 repmgr 用户
su -l postgres -c "createuser -s repmgr"
su -l postgres -c "createdb repmgr -O repmgr"
  1. 配置 /etc/postgresql/14/main/pg_hba.conf

这个hba文件是为了配置repmgr用户无密码访问repmgr数据库的,所以上面创建用户的时候没有设置密码,trust就是这个意思,还有其他的配置项可以看配置文件里的解释

sed -i '/# DO NOT DISABLE!/i \
local   replication   repmgr                              trust \
host    replication   repmgr      127.0.0.1/32            trust \
host    replication   repmgr      0.0.0.0/0               trust \
local   repmgr        repmgr                              trust \
host    repmgr        repmgr      127.0.0.1/32            trust \
host    repmgr        repmgr      0.0.0.0/0               trust' /etc/postgresql/14/main/pg_hba.conf
  1. 配置/etc/postgresql/14/main/repmgr.conf,内容如下
cat <<EOF > /etc/postgresql/14/main/repmgr.conf
node_id=1
node_name='${PRIMARY}'
conninfo='host=${PRIMARY} user=repmgr dbname=repmgr connect_timeout=2'
data_directory='/var/lib/postgresql/14/main'
# 日志管理
log_level='INFO'
log_file='/tmp/repmgr.log'
# failover设置
failover='automatic'
promote_command='/usr/bin/repmgr standby promote -f /etc/postgresql/14/main/repmgr.conf --log-to-file'
follow_command='/usr/bin/repmgr standby follow -f /etc/postgresql/14/main/repmgr.conf --log-to-file --upstream-node-id=%n'
# 用于repmgr启停pg
service_start_command   = 'sudo pg_ctlcluster 14 main start'
service_stop_command    = 'sudo pg_ctlcluster 14 main stop'
service_restart_command = 'sudo pg_ctlcluster 14 main restart'
service_reload_command  = 'sudo pg_ctlcluster 14 main reload'
EOF

上面的变量按实际情况去修改

  1. 配置后重启数据库
sudo systemctl restart postgresql@14-main.service
  1. 注册主库
# 注册主节点
su -l postgres -c "repmgr -f /etc/postgresql/14/main/repmgr.conf primary register"
# 查看注册后情况,如下说明已注册成功
su -l postgres -c "repmgr -f /etc/postgresql/14/main/repmgr.conf cluster show"
 ID | Name  | Role    | Status    | Upstream | Location | Priority | Timeline | Connection string                                                                
----+-------+---------+-----------+----------+----------+----------+----------+-----------------------------------------------------------------------------------
 1  | node1 | primary | * running |          | default  | 100      | 1        | host=10.204.121.218 user=repmgr dbname=repmgr connect_timeout=2 password=test2024

配置从库node2

  1. 修改配置文件 /etc/postgresql/14/main/postgresql.conf
sed -i '/cluster_name/s/14\/main/node2/' /etc/postgresql/14/main/postgresql.conf
cat <<EOF >> /etc/postgresql/14/main/postgresql.conf
listen_addresses = '*'
max_wal_senders = 10
max_replication_slots = 10
wal_level = 'hot_standby'
hot_standby = on
archive_mode = on   # repmgr 本身不需要 WAL 文件归档。
archive_command = '/bin/true'
shared_preload_libraries = 'repmgr'
wal_log_hints = 'on'
EOF
  1. 配置/etc/postgresql/14/main/repmgr.conf,内容如下
cat <<EOF > /etc/postgresql/14/main/repmgr.conf
node_id=2
node_name='${STANDBY}'
conninfo='host=${STANDBY} user=repmgr dbname=repmgr connect_timeout=2'
data_directory='/var/lib/postgresql/14/main'
# 日志管理
log_level='INFO'
log_file='/tmp/repmgr.log'
# failover设置
failover='automatic'
promote_command='/usr/bin/repmgr standby promote -f /etc/postgresql/14/main/repmgr.conf --log-to-file'
follow_command='/usr/bin/repmgr standby follow -f /etc/postgresql/14/main/repmgr.conf --log-to-file --upstream-node-id=%n'
# 用于repmgr启停pg
service_start_command   = 'sudo pg_ctlcluster 14 main start'
service_stop_command    = 'sudo pg_ctlcluster 14 main stop'
service_restart_command = 'sudo pg_ctlcluster 14 main restart'
service_reload_command  = 'sudo pg_ctlcluster 14 main reload'
EOF
  1. 配置 /etc/postgresql/14/main/pg_hba.conf
# 添加repmgr无密码登录pg
sed -i '/# DO NOT DISABLE!/i \
local   replication   repmgr                              trust \
host    replication   repmgr      127.0.0.1/32            trust \
host    replication   repmgr      0.0.0.0/0               trust \
local   repmgr        repmgr                              trust \
host    repmgr        repmgr      127.0.0.1/32            trust \
host    repmgr        repmgr      0.0.0.0/0               trust' /etc/postgresql/14/main/pg_hba.conf
  1. 停止pg 删除从库数据
systemctl stop postgresql@14-main.service
rm -rf /var/lib/postgresql/14/main/*
  1. 注册并启动从库
# 测试克隆数据,-h 是主节点的IP地址
su -l postgres -c "repmgr -h $primaryIP -U repmgr -d repmgr -f /etc/postgresql/14/main/repmgr.conf standby clone --dry-run"
# 正常的话,执行clone
su -l postgres -c "repmgr -h $primaryIP -U repmgr -d repmgr -f /etc/postgresql/14/main/repmgr.conf standby clone"
# 启动pg
systemctl start postgresql@14-main.service
# 注册从库
su -l postgres -c "repmgr -f /etc/postgresql/14/main/repmgr.conf standby register"
# 查看注册好的从库,目前主从都建立完成
su -l postgres -c "repmgr -f /etc/postgresql/14/main/repmgr.conf cluster show"
 ID | Name  | Role    | Status    | Upstream | Location | Priority | Timeline | Connection string                                                                
----+-------+---------+-----------+----------+----------+----------+----------+-----------------------------------------------------------------------------------
 1  | node1 | primary | * running |          | default  | 100      | 1        | host=10.204.121.218 user=repmgr dbname=repmgr connect_timeout=2 password=test2024
 2  | node2 | standby |   running | node1    | default  | 100      | 1        | host=10.204.121.219 user=repmgr dbname=repmgr connect_timeout=2 password=test2024

到此已经完成了pg的主从搭建,目前也可以故障转移,但是需要手动操作,后面接着部署repmgr自动故障切换

创建repmgrd(所有节点)

repmgrd 作为运行在集群中每个节点上的一个管理和监控的守护程序,可以自动进行故障转移和维护复制关系,并提供有关每个节点状态的监控信息。

  1. 修改repmgrd service 配置文件,新的,如下
cat <<EOF > /etc/default/repmgrd
REPMGRD_ENABLED=yes
REPMGRD_CONF="/etc/postgresql/14/main/repmgr.conf"
REPMGRD_OPTS="--daemonize=false"
EOF
  1. 重启repmgrd
# 启动
systemctl restart repmgrd
  1. 查看repmgrd在集群的状态,显示running说明正常。到这里就可以自动故障转移了
su - postgres -c 'repmgr -f /etc/postgresql/14/main/repmgr.conf service status'
 ID | Name  | Role    | Status    | Upstream | repmgrd     | PID     | Paused? | Upstream last seen
----+-------+---------+-----------+----------+-------------+---------+---------+--------------------
 1  | node1 | primary | * running |          | running     | 1541134 | no      | n/a                
 2  | node2 | standby |   running | node1    | running     | n/a     | n/a     | n/a    

repmgr缺点:主节点宕机恢复后,需要人工手动加入到集群

主节点宕机恢复

重新加入之前的主节点为当前的从节点,可以使用repmgr node rejoin 命令,官方链接: repmgr-node-rejoin但是测试没有成功,所以用重新clone的方式恢复

systemctl stop postgresql@14-main.service
rm -rf /var/lib/postgresql/14/main/*
# $primaryIP 替换成当前主节点IP
su -l postgres -c "repmgr -h $primaryIP -U repmgr -d repmgr -f /etc/postgresql/14/main/repmgr.conf standby clone --dry-run"
# 上一步正常执行后,执行clone
su -l postgres -c "repmgr -h 10.204.121.219 -U repmgr -d repmgr -f /etc/postgresql/14/main/repmgr.conf standby clone"
systemctl start postgresql@14-main.service
su -l postgres -c "repmgr -f /etc/postgresql/14/main/repmgr.conf standby register --force"
# 查看集群状态是否已同步
su -l postgres -c "repmgr -f /etc/postgresql/14/main/repmgr.conf cluster show"

上面是一步一步的部署测试方法,本人又写了个自动部署脚本,用来自动安装,链接如下:
https://gitee.com/znblog/db-ha/tree/master/postgres

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/757832.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

1-爬虫基础知识(6节课学会爬虫)

1-爬虫基础知识&#xff08;6节课学会爬虫&#xff09; 1.什么是爬虫2.爬取的数据去哪了3.需要的软件和环境4.浏览器的请求&#xff08;1&#xff09;Url&#xff08;2&#xff09;浏览器请求url地址&#xff08;3&#xff09;url地址对应的响应 5.认识HTTP/HTTPS5.1 http协议之…

餐饮火锅加盟网站pbootcms模板源码

模板介绍 如果您正在创建火锅店、餐饮店或加盟网站&#xff0c;小编推荐您下载这款餐饮火锅加盟网站pbootcms模板源码&#xff0c;整站源码响应式自适应的设计&#xff0c;可以让您自由编辑且适应任何终端浏览器&#xff0c;节约您的建站时间成本。 模板截图 源码下载 餐饮火…

生成式人工智能和机器人技术是否即将取得最后的突破?

了解生成式人工智能与机器人技术的融合如何彻底改变从医疗保健到娱乐等行业 想象一下这样一个世界&#xff0c;机器人可以谱写交响乐、画出杰作、写出小说。这种创造力与自动化的迷人融合&#xff0c;由 生成式人工智能&#xff0c;不再是梦想&#xff1b;它正在以重大方式重塑…

前后端分离的后台管理系统开发模板(带你从零开发一套自己的若依框架)上

前言&#xff1a; 目前&#xff0c;前后端分离开发已经成为当前web开发的主流。目前最流行的技术选型是前端vue3后端的spring boot3&#xff0c;本次。就基于这两个市面上主流的框架来开发出一套基本的后台管理系统的模板&#xff0c;以便于我们今后的开发。 前端使用vue3ele…

go Channel 原理 (一)

Channel 设计原理 不要通过共享内存的方式进行通信&#xff0c;而是应该通过通信的方式共享内存。 在主流编程语言中&#xff0c;多个线程传递数据的方式一般都是共享内存。 Go 可以使用共享内存加互斥锁进行通信&#xff0c;同时也提供了一种不同的并发模型&#xff0c;即通…

Postman设置请求间自动保存返回参数,方便后续请求调用,减少复制粘贴

postman中常常出现&#xff1a;有两个请求&#xff0c;一个请求首先获取验证码或者token&#xff0c;再由得到的验证码或token编写body发送另一个请求。如何设置两个请求间自动关联相关数据呢&#xff1f; 通过环境存储全局变量 现在有两个请求如下图&#xff0c;生成验证码是…

代理IP如何助力旅游信息聚合?

在数字化时代&#xff0c;旅游信息聚合对于提升服务质量、优化用户体验起着至关重要的作用。随着在线旅游预订的普及&#xff0c;旅游信息的采集、整合和呈现成为了一个复杂而关键的过程。在这个过程中&#xff0c;代理IP技术以其独特的优势&#xff0c;为旅游信息聚合提供了强…

服务器硬件以及RAID配置

目录 一、RAID磁盘阵列原理&#xff08;嘎嘎重要&#xff09; 1、RAID的概述 2、常用的RAID 2.1、RAID 0 2.2、RAID 1 2.3、RAID 5 2.5、RAID 10 3、阵列卡介绍 二、建立软件RAID磁盘阵列 1、添加硬盘 2、使用fdisk分区&#xff0c;类型为fd 3、mdata命令使用参数 …

CXL:拯救NVMe SSD缓存不足设计难题-2

LMB提出了基于CXL协议的内存扩展框架和内核模块。该方案利用CXL内存扩展器作为物理DRAM源&#xff0c;旨在提供一个统一的内存分配接口&#xff0c;使PCIe和CXL设备都能方便地访问扩展的内存资源。通过这个接口&#xff0c;NVMe驱动和CUDA的统一内存内核驱动可以直接高效地访问…

探索人工智能和LLM对未来就业的影响

近年来&#xff0c;人工智能&#xff08;AI&#xff09;迅猛发展&#xff0c;引发了人们的兴奋&#xff0c;同时也引发了人们对就业未来的担忧。大型语言模型&#xff08;LLM&#xff09;就是最新的例子。这些强大的人工智能子集经过大量文本数据的训练&#xff0c;以理解和生成…

【贡献法】2262. 字符串的总引力

本文涉及知识点 贡献法 LeetCode2262. 字符串的总引力 字符串的 引力 定义为&#xff1a;字符串中 不同 字符的数量。 例如&#xff0c;“abbca” 的引力为 3 &#xff0c;因为其中有 3 个不同字符 ‘a’、‘b’ 和 ‘c’ 。 给你一个字符串 s &#xff0c;返回 其所有子字符…

【Arduino】实验使用ESP32控制可编程继电器制作跑马灯(图文)

今天小飞鱼实验使用ESP控制继电器&#xff0c;为了更好的掌握继电器的使用方法这里实验做了一个跑马灯的效果。 这里用到的可编程继电器&#xff0c;起始原理并不复杂&#xff0c;同样需要ESP32控制针脚输出高电平或低电平给到继电器&#xff0c;继电器使用这个信号控制一个电…

Linux 网络:网卡 promiscuous 模式疑云

文章目录 1. 前言2. 问题场景3. 问题定位和分析4. 参考资料 1. 前言 限于作者能力水平&#xff0c;本文可能存在谬误&#xff0c;因此而给读者带来的损失&#xff0c;作者不做任何承诺。 2. 问题场景 调试 Marvell 88E6320 时&#xff0c;发现 eth0 出人意料的进入了 promis…

【吊打面试官系列-MyBatis面试题】MyBatis 与 Hibernate 有哪些不同?

大家好&#xff0c;我是锋哥。今天分享关于 【MyBatis 与 Hibernate 有哪些不同&#xff1f;】面试题&#xff0c;希望对大家有帮助&#xff1b; MyBatis 与 Hibernate 有哪些不同&#xff1f; 1、Mybatis 和 hibernate 不同&#xff0c;它不完全是一个 ORM 框架&#xff0c;因…

grpc学习golang版( 四、多服务示例 )

系列文章目录 第一章 grpc基本概念与安装 第二章 grpc入门示例 第三章 proto文件数据类型 第四章 多服务示例 第五章 多proto文件示例 第六章 服务器流式传输 第七章 客户端流式传输 第八章 双向流示例 文章目录 一、前言二、定义proto文件三、编写server服务端四、编写Client客…

盘点全球Top10大云计算平台最热门技能证书

小李哥花了一年半时间终于考下全球10大云的77张认证&#xff0c;今天盘点下各个云的热门证书&#xff0c;希望能帮到非CS专业转IT和刚刚入行云计算的小伙伴。 排名取自23年Yahoo云计算市场份额排名报告&#xff0c;我会从云平台、证书价格、证书热门程度做推荐。 1️⃣亚马逊云…

MathType7.6永久破解激活码注册码 包含安装包下载

MathType是一款强大的数学公式编辑器&#xff0c;它能够帮助用户轻松编辑各种复杂的数学公式和符号。无论是学生、教师还是科研人员&#xff0c;MathType都能提供专业、精确的数学公式编辑服务。 在学习和工作中&#xff0c;我们常常会遇到需要编写数学公式的情况。然而&#x…

Python 算法交易实验74 QTV200第二步(改): 数据清洗并写入Mongo

说明 之前第二步是打算进入Clickhouse的&#xff0c;实测下来有一些bug 可以看到有一些分钟数据重复了。简单分析原因&#xff1a; 1 起异步任务时&#xff0c;还是会有两个任务重复的问题&#xff0c;这个在同步情况下是不会出现的2 数据库没有upsert模式。clickhouse是最近…

除了重塑千行百业,生成式AI还能改善运动健康

飞速发展的生成式AI与大模型技术&#xff0c;不但正在重塑千行百业&#xff0c;而且还能有效改善人们的运动健康。 生成式AI技术应用的挑战 随着生活品质的不断提升&#xff0c;人们对于健康问题也越来越重视。作为一家以“AI重塑健康与美”为使命的AI数字健康解决方案提供商&a…

langchain学习总结

大模型开发遇到的问题及langchain框架学习 背景&#xff1a; 1、微场景间跳转问题&#xff0c;无法实现微场景随意穿插 2、大模型幻读&#xff08;推荐不存在的产品、自己发挥&#xff09; 3、知识库检索&#xff0c;语义匹配效果较差&#xff0c;匹配出的结果和客户表述的…