当前位置:   article > 正文

进阶数据库系列(十八):PostgreSQL 基于 repmgr 高可用架构实践

postgres repmgr

点击下方名片,设为星标

回复“1024”获取2TB学习资源!

前面介绍了 PostgreSQL 存储过程索引分区分表事务与并发控制主从复制高可用方案基于 Patroni 高可用架构部署及故障切换等相关的知识点,今天我将详细的为大家介绍 PostgreSQL 基于 repmgr 高可用架构相关知识,希望大家能够从中收获多多!如有帮助,请点在看、转发支持一波!!!

PostgreSQL 的开源 HA 工具有很多种,前一章介绍了基于 Patroni 高可用架构的部署与配置,今天来学习另一个轻量级的高可用套件 repmgr。

repmgr 简介

repmgr是一个2ndQuadrant开发的一款复制的开源工具套件,用于管理PostgreSQL服务器集群中的复制和故障转移。最初,它主要是为了简化流副本的管理,后来发展成为一个完整的故障转移管理套件。它通过设置备用服务器,监视复制以及执行管理任务(如故障转移或手动切换操作)的工具,增强了PostgreSQL内置的热备份功能。Repmgr体系架构如下:e6243c53f313da08f63a502c81380fbd.pngrepmgr与声名远扬的ORACLE ADG逻辑复制工具非常类似。它的功能强大,安装和配置简单,有很强的可操控性。

repmgr特点

repmgr的特点是非常轻量,单功能全面

  • 故障检测和自动故障切换:repmgr 可以检测到主服务器故障并自动切换到备用服务器。

  • 自动故障恢复:repmgr 可以检测到从服务器故障并自动将其重新加入到复制拓扑中。

  • 多个备用服务器:repmgr 支持多个备用服务器,可以在主服务器故障时自动切换到最合适的备用服务器。

  • 灵活的复制拓扑:repmgr 支持各种复制拓扑,包括单主服务器和多主服务器。

  • 管理和监控:repmgr 提供了用于管理和监控PostgreSQL复制的各种工具和命令。

可以说 repmgr 是一个扩展模块,简化了 PostgreSQL 复制的管理和维护,提高系统的可靠性和可用性。它是一个非常有用的工具,特别是对于需要高可用性的生产环境。同时 repmgr 也是由 Postgresql 社区开发以及维护的。

它提供了两个主要工具:
  1. repmgr 
  2. #用于执行管理任务的命令行工具 设置备用服务器,将备用服务器提升为主服务器,切换主服务器和备用服务器,显示复制群集中服务器的状态
  3. repmgrd 
  4. #主动监视复制群集中的服务器的守护程序 监视和记录复制性能,通过检测主数据库和提升最合适的备用服务器,向用户定义的群集中事件提供有关事件的通知 可以执行任务的脚本,例如通过电子邮件发送警报
repmgr 版本对应支持的PostgreSQL版本
6cacfcc599a3469896a25b4bdc660812.png

更多关于大数据 PostgreSQL 系列的学习文章,请参阅:PostgreSQL 数据库,本系列持续更新中。

PostgreSql repmgr 高可用部署

服务器配置
三台服务器配置ip(添加至/etc/hosts文件)

192.168.100.110 master
192.168.100.111 slave1
192.168.100.112 slave2

三台服务器配置互信
  1. ssh-keygen -t rsa
  2. for i in 192.168.100.110 192.168.100.111 192.168.100.112;do ssh-copy-id -i $i;done

三台服务器分别安装PostgreSQL数据库

三台服务器设置环境变量

root 用户下增加环境变量设置

  1. export PGHOME=/usr/local/pgsql/
  2. export PGUSER=postgres
  3. export PGPORT=5432
  4. export PGDATA=/app/pgsql/data
  5. export PGLOG=/app/pgsql/log/postgresql.log
  6. export PATH=$PGHOME/bin:$PATH:$HOME/bin
  7. export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
三台服务器流复制参数配置
  1. cd /app/pgsql/data
  2. vi postgresql.conf
  3. listen_addresses = '*'
  4. max_wal_senders = 10
  5. max_replication_slots = 10  
  6. wal_level = replica 
  7. hot_standby = on
  8. wal_log_hints = on
  9. full_page_writes=on
  10. shared_preload_libraries = 'repmgr'
  11. wal_keep_segments=100
  12. archive_mode = on 
  13. archive_command = 'test ! -f /app/pgsql/archive/%f && cp %p /app/pgsql/archive/%f'
三台服务器 repmgr 配置
三个节点均安装 repmgr
  1. --下载并解压
  2. wget -c https://repmgr.org/download/repmgr-5.2.1.tar.gz
  3. tar -zxvf repmgr-5.2.1.tar.gz -C /usr/local/pgsql/contrib
  4. cd /usr/local/pgsql/contrib
  5. --编译安装
  6. mv repmgr-5.2.1 repmgr
  7. cd repmgr
  8. yum install flex
  9. ./configure && make install
参数文件配置
master 节点
  1. vi /etc/repmgr.conf
  2. #repmgr基本配置信息
  3. node_id=1
  4. node_name='master'
  5. conninfo='host=192.168.100.110 user=repmgr dbname=repmgr connect_timeout=2'
  6. data_directory='/app/pgsql/data'
  7. #repmgr日志配置
  8. log_level=INFO                          
  9. log_facility=STDERR                     
  10. log_file='/app/pgsql/log/repmgr.log'
  11. log_status_interval=10
  12. #可执行文件配置
  13. pg_bindir='/usr/local/pgsql/bin'
  14. #集群faibver设置
  15. failover='automatic'
  16. promote_command='/usr/local/pgsql/bin/repmgr standby promote -f /etc/repmgr.conf --log-to-file'
  17. follow_command='/usr/local/pgsql/bin/repmgr standby follow -f /etc/repmgr.conf --log-to-file --upstream-node-id=%n'
  18. log_file='/app/pgsql/log/repmgr.log'

更多关于大数据 PostgreSQL 系列的学习文章,请参阅:PostgreSQL 数据库,本系列持续更新中。

slave1 节点
  1. vi /etc/repmgr.conf
  2. #repmgr基本配置信息
  3. node_id=2
  4. node_name='slave1'
  5. conninfo='host=192.168.100.111 user=repmgr dbname=repmgr connect_timeout=2'
  6. data_directory='/app/pgsql/data'
  7. #repmgr日志配置
  8. log_level=INFO                          
  9. log_facility=STDERR                     
  10. log_file='/app/pgsql/log/repmgr.log'
  11. log_status_interval=10
  12. #可执行文件配置
  13. pg_bindir='/usr/local/pgsql/bin'
  14. #集群faibver设置
  15. failover='automatic'
  16. promote_command='/usr/local/pgsql/bin/repmgr standby promote -f /etc/repmgr.conf --log-to-file'
  17. follow_command='/usr/local/pgsql/bin/repmgr standby follow -f /etc/repmgr.conf --log-to-file --upstream-node-id=%n'
slave2 节点
  1. #repmgr基本配置信息
  2. node_id=3
  3. node_name='slave2'
  4. conninfo='host=192.168.100.112 user=repmgr dbname=repmgr connect_timeout=2'
  5. data_directory='/app/pgsql/data'
  6. #repmgr日志配置
  7. log_level=INFO                          
  8. log_facility=STDERR                     
  9. log_file='/app/pgsql/log/repmgr.log'
  10. log_status_interval=10
  11. #可执行文件配置
  12. pg_bindir='/usr/local/pgsql/bin'
  13. #集群faibver设置
  14. failover='automatic'
  15. promote_command='/usr/local/pgsql/bin/repmgr standby promote -f /etc/repmgr.conf --log-to-file'
  16. follow_command='/usr/local/pgsql/bin/repmgr standby follow -f /etc/repmgr.conf --log-to-file --upstream-node-id=%n'
master 节点配置数据库环境
创建repmgr数据库及用户
  1. create database repmgr;
  2. create user repmgr with password 'repmgr' superuser login;
  3. alter database repmgr owner to repmgr;
配置pg_hba.conf
  1. cd /app/pgsql/data
  2. vi pg_hba.conf
  3. # TYPE  DATABASE        USER            ADDRESS                 METHOD
  4. "local" is for Unix domain socket connections only
  5. local   all           all                                     trust
  6. # IPv4 local connections:
  7. host       all        all             127.0.0.1/32            trust
  8. local   repmgr     repmgr                                     trust
  9. host    repmgr     repmgr             127.0.0.1/32            trust
  10. host    repmgr     repmgr             192.168.100.0/24        trust
  11. host       all        all             0.0.0.0/0               md5
  12. # IPv6 local connections:
  13. host       all        all             ::1/128                 trust
  14. # Allow replication connections from localhost, by a user with the
  15. # replication privilege.
  16. local   replication     all                                     trust
  17. host    replication     all             127.0.0.1/32            trust
  18. host    replication     all             ::1/128                 trust
  19. local   replication     repmgr                                     trust
  20. host    replication     repmgr             127.0.0.1/32            trust
  21. host    replication     repmgr             192.168.220.0/24        trust

更多关于大数据 PostgreSQL 系列的学习文章,请参阅:PostgreSQL 数据库,本系列持续更新中。

repmgr 集群构建

master 节点加入集群
  1. --启动master节点数据库
  2. su - postgres
  3. pg_ctl start -l $PGLOG
  4. --master节点,将master数据库注册至集群,并查看状态
  5. su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf primary register"
  6. su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf cluster show"
slave1 节点加入集群
  1. --slave1节点,测试连通性并克隆master数据库数据
  2. su - postgres -c "/usr/local/pgsql/bin/repmgr -h 192.168.100.110 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --dry-run"
  3. rm -rf /app/pgsql/data/*
  4. su - postgres -c "/usr/local/pgsql/bin/repmgr -h 192.168.100.110 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone"
  5. --启动slave1节点数据库
  6. su - postgres
  7. pg_ctl start -l $PGLOG
  8. --slave1节点,将slave1数据库注册到集群,并查看状态
  9. su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf standby register"
  10. su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf cluster show"
slave2 节点加入集群
  1. --slave2节点,测试连通性并克隆master数据库数据
  2. su - postgres -c "/usr/local/pgsql/bin/repmgr -h 192.168.100.110 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --dry-run"
  3. rm -rf /app/pgsql/data/*
  4. su - postgres -c "/usr/local/pgsql/bin/repmgr -h 192.168.100.110 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone"
  5. --启动slave2节点数据库
  6. su - postgres
  7. pg_ctl start -l $PGLOG
  8. --slave2节点,将slave2数据库注册到集群,并查看状态
  9. su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf standby register"
  10. su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf cluster show"

开启守护进程

  1. --开启守护进程(故障自动转移)
  2. su - postgres -c "/usr/local/pgsql/bin/repmgrd -f /etc/repmgr.conf -d  -p /tmp/repmgrd.pid"
  3. --停止守护进程
  4. REPMGRD_PID=`ps -ef | grep repmgrd|grep   -v grep |awk '{print  $2}'`
  5. kill -9 $REPMGRD_PID

其他 repmgr 管理命令

  1. --主备切换并查看
  2. su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf standby switchover --siblings-follow -U repmgr  --verbose"
  3. su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf cluster show"
  4. --从库重新跟随新主库
  5. su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf standby follow"
  6. su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf cluster show"
  7. --驱逐备库节点
  8. su - postgres -c "/usr/local/pgsql/bin/repmgr standby unregister -f /etc/repmgr.conf"
  9. --注销不活动的主节点
  10. su - postgres -c "/usr/local/pgsql/bin/repmgr primary unregister -f /etc/repmgr.conf"
  11. --主节点故障时,手动升级备库为主节点
  12. su - postgres -c "/usr/local/pgsql/bin/repmgr standby promote -f /etc/repmgr.conf --siblings-follow"
  13. su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf standby follow"
  14. su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf cluster show"
  15. --故障节点修复后,重新加入集群
  16. su - postgres -c "/usr/local/pgsql/bin/repmgr node rejoin -d 'host=slave2 user=repmgr dbname=repmgr' --force-rewind --verbose -f /etc/repmgr.conf"
  17. su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf cluster show"
  18. --强制重新注册
  19. su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf primary register --force"

更多关于大数据 PostgreSQL 系列的学习文章,请参阅:PostgreSQL 数据库,本系列持续更新中。

来源:https://xiaosonggong.blog.csdn.net/article/details

/120059875

读者专属技术群

构建高质量的技术交流社群,欢迎从事后端开发、运维技术进群(备注岗位,已在技术交流群的请勿重复添加)。主要以技术交流、内推、行业探讨为主,请文明发言。广告人士勿入,切勿轻信私聊,防止被骗。

扫码加我好友,拉你进群

8e43596675ecf8a0ee29eb70f32f5232.jpeg

推荐阅读 点击标题可跳转

阿里再次大改革,江湖再无 P8 了。。。

这款轻量级文件传输工具真心强大!支持网页版

良心推荐!这 5 款免费开源软件一年为你节省上万元

最受欢迎Web服务器!来看看它比Nginx牛逼在哪?

面试官:如何快速修改 Docker 镜像默认存储位置?

发现一款吊炸天的跨平台远程管理工具,有点牛逼!

一名鹅厂员工悲观发文,活着究竟是为了什么?

b245b3ba7e45faad85b7f37426458f85.png

PS:因为公众号平台更改了推送规则,如果不想错过内容,记得读完点一下在看,加个星标,这样每次新文章推送才会第一时间出现在你的订阅列表里。点在看支持我们吧!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/449838
推荐阅读
相关标签
  

闽ICP备14008679号