赞
踩
点击下方名片,设为星标!
回复“1024”获取2TB学习资源!
前面介绍了 PostgreSQL 存储过程、索引、分区分表、事务与并发控制、主从复制、高可用方案、基于 Patroni 高可用架构部署及故障切换等相关的知识点,今天我将详细的为大家介绍 PostgreSQL 基于 repmgr 高可用架构相关知识,希望大家能够从中收获多多!如有帮助,请点在看、转发支持一波!!!
PostgreSQL 的开源 HA 工具有很多种,前一章介绍了基于 Patroni 高可用架构的部署与配置,今天来学习另一个轻量级的高可用套件 repmgr。
repmgr是一个2ndQuadrant开发的一款复制的开源工具套件,用于管理PostgreSQL服务器集群中的复制和故障转移。最初,它主要是为了简化流副本的管理,后来发展成为一个完整的故障转移管理套件。它通过设置备用服务器,监视复制以及执行管理任务(如故障转移或手动切换操作)的工具,增强了PostgreSQL内置的热备份功能。Repmgr体系架构如下:repmgr与声名远扬的ORACLE ADG逻辑复制工具非常类似。它的功能强大,安装和配置简单,有很强的可操控性。
repmgr的特点是非常轻量,单功能全面
故障检测和自动故障切换:repmgr 可以检测到主服务器故障并自动切换到备用服务器。
自动故障恢复:repmgr 可以检测到从服务器故障并自动将其重新加入到复制拓扑中。
多个备用服务器:repmgr 支持多个备用服务器,可以在主服务器故障时自动切换到最合适的备用服务器。
灵活的复制拓扑:repmgr 支持各种复制拓扑,包括单主服务器和多主服务器。
管理和监控:repmgr 提供了用于管理和监控PostgreSQL复制的各种工具和命令。
可以说 repmgr 是一个扩展模块,简化了 PostgreSQL 复制的管理和维护,提高系统的可靠性和可用性。它是一个非常有用的工具,特别是对于需要高可用性的生产环境。同时 repmgr 也是由 Postgresql 社区开发以及维护的。
- repmgr
- #用于执行管理任务的命令行工具 设置备用服务器,将备用服务器提升为主服务器,切换主服务器和备用服务器,显示复制群集中服务器的状态
- repmgrd
- #主动监视复制群集中的服务器的守护程序 监视和记录复制性能,通过检测主数据库和提升最合适的备用服务器,向用户定义的群集中事件提供有关事件的通知 可以执行任务的脚本,例如通过电子邮件发送警报
更多关于大数据 PostgreSQL 系列的学习文章,请参阅:PostgreSQL 数据库,本系列持续更新中。
192.168.100.110 master
192.168.100.111 slave1
192.168.100.112 slave2
- ssh-keygen -t rsa
- for i in 192.168.100.110 192.168.100.111 192.168.100.112;do ssh-copy-id -i $i;done
三台服务器分别安装PostgreSQL数据库:
root 用户下增加环境变量设置
- export PGHOME=/usr/local/pgsql/
- export PGUSER=postgres
- export PGPORT=5432
- export PGDATA=/app/pgsql/data
- export PGLOG=/app/pgsql/log/postgresql.log
- export PATH=$PGHOME/bin:$PATH:$HOME/bin
- export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
- cd /app/pgsql/data
- vi postgresql.conf
-
- listen_addresses = '*'
- max_wal_senders = 10
- max_replication_slots = 10
- wal_level = replica
- hot_standby = on
- wal_log_hints = on
- full_page_writes=on
- shared_preload_libraries = 'repmgr'
- wal_keep_segments=100
- archive_mode = on
- archive_command = 'test ! -f /app/pgsql/archive/%f && cp %p /app/pgsql/archive/%f'
- --下载并解压
- wget -c https://repmgr.org/download/repmgr-5.2.1.tar.gz
- tar -zxvf repmgr-5.2.1.tar.gz -C /usr/local/pgsql/contrib
- cd /usr/local/pgsql/contrib
-
- --编译安装
- mv repmgr-5.2.1 repmgr
- cd repmgr
- yum install flex
- ./configure && make install
- vi /etc/repmgr.conf
-
- #repmgr基本配置信息
- node_id=1
- node_name='master'
- conninfo='host=192.168.100.110 user=repmgr dbname=repmgr connect_timeout=2'
- data_directory='/app/pgsql/data'
-
- #repmgr日志配置
- log_level=INFO
- log_facility=STDERR
- log_file='/app/pgsql/log/repmgr.log'
- log_status_interval=10
-
- #可执行文件配置
- pg_bindir='/usr/local/pgsql/bin'
-
- #集群faibver设置
- failover='automatic'
- promote_command='/usr/local/pgsql/bin/repmgr standby promote -f /etc/repmgr.conf --log-to-file'
- follow_command='/usr/local/pgsql/bin/repmgr standby follow -f /etc/repmgr.conf --log-to-file --upstream-node-id=%n'
- log_file='/app/pgsql/log/repmgr.log'
更多关于大数据 PostgreSQL 系列的学习文章,请参阅:PostgreSQL 数据库,本系列持续更新中。
- vi /etc/repmgr.conf
-
- #repmgr基本配置信息
- node_id=2
- node_name='slave1'
- conninfo='host=192.168.100.111 user=repmgr dbname=repmgr connect_timeout=2'
- data_directory='/app/pgsql/data'
-
- #repmgr日志配置
- log_level=INFO
- log_facility=STDERR
- log_file='/app/pgsql/log/repmgr.log'
- log_status_interval=10
-
- #可执行文件配置
- pg_bindir='/usr/local/pgsql/bin'
-
- #集群faibver设置
- failover='automatic'
- promote_command='/usr/local/pgsql/bin/repmgr standby promote -f /etc/repmgr.conf --log-to-file'
- follow_command='/usr/local/pgsql/bin/repmgr standby follow -f /etc/repmgr.conf --log-to-file --upstream-node-id=%n'
- #repmgr基本配置信息
- node_id=3
- node_name='slave2'
- conninfo='host=192.168.100.112 user=repmgr dbname=repmgr connect_timeout=2'
- data_directory='/app/pgsql/data'
-
- #repmgr日志配置
- log_level=INFO
- log_facility=STDERR
- log_file='/app/pgsql/log/repmgr.log'
- log_status_interval=10
-
- #可执行文件配置
- pg_bindir='/usr/local/pgsql/bin'
-
- #集群faibver设置
- failover='automatic'
- promote_command='/usr/local/pgsql/bin/repmgr standby promote -f /etc/repmgr.conf --log-to-file'
- follow_command='/usr/local/pgsql/bin/repmgr standby follow -f /etc/repmgr.conf --log-to-file --upstream-node-id=%n'
- create database repmgr;
- create user repmgr with password 'repmgr' superuser login;
- alter database repmgr owner to repmgr;
- cd /app/pgsql/data
- vi pg_hba.conf
-
- # TYPE DATABASE USER ADDRESS METHOD
-
- # "local" is for Unix domain socket connections only
- local all all trust
- # IPv4 local connections:
- host all all 127.0.0.1/32 trust
- local repmgr repmgr trust
- host repmgr repmgr 127.0.0.1/32 trust
- host repmgr repmgr 192.168.100.0/24 trust
- host all all 0.0.0.0/0 md5
- # IPv6 local connections:
- host all all ::1/128 trust
- # Allow replication connections from localhost, by a user with the
- # replication privilege.
- local replication all trust
- host replication all 127.0.0.1/32 trust
- host replication all ::1/128 trust
-
- local replication repmgr trust
- host replication repmgr 127.0.0.1/32 trust
- host replication repmgr 192.168.220.0/24 trust
更多关于大数据 PostgreSQL 系列的学习文章,请参阅:PostgreSQL 数据库,本系列持续更新中。
- --启动master节点数据库
- su - postgres
- pg_ctl start -l $PGLOG
- --master节点,将master数据库注册至集群,并查看状态
- su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf primary register"
- su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf cluster show"
- --slave1节点,测试连通性并克隆master数据库数据
- 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"
- rm -rf /app/pgsql/data/*
- su - postgres -c "/usr/local/pgsql/bin/repmgr -h 192.168.100.110 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone"
- --启动slave1节点数据库
- su - postgres
- pg_ctl start -l $PGLOG
- --slave1节点,将slave1数据库注册到集群,并查看状态
- su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf standby register"
- su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf cluster show"
- --slave2节点,测试连通性并克隆master数据库数据
- 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"
- rm -rf /app/pgsql/data/*
- su - postgres -c "/usr/local/pgsql/bin/repmgr -h 192.168.100.110 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone"
- --启动slave2节点数据库
- su - postgres
- pg_ctl start -l $PGLOG
- --slave2节点,将slave2数据库注册到集群,并查看状态
- su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf standby register"
- su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf cluster show"
- --开启守护进程(故障自动转移)
- su - postgres -c "/usr/local/pgsql/bin/repmgrd -f /etc/repmgr.conf -d -p /tmp/repmgrd.pid"
-
- --停止守护进程
- REPMGRD_PID=`ps -ef | grep repmgrd|grep -v grep |awk '{print $2}'`
- kill -9 $REPMGRD_PID
- --主备切换并查看
- su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf standby switchover --siblings-follow -U repmgr --verbose"
- su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf cluster show"
- --从库重新跟随新主库
- su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf standby follow"
- su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf cluster show"
-
- --驱逐备库节点
- su - postgres -c "/usr/local/pgsql/bin/repmgr standby unregister -f /etc/repmgr.conf"
-
- --注销不活动的主节点
- su - postgres -c "/usr/local/pgsql/bin/repmgr primary unregister -f /etc/repmgr.conf"
-
- --主节点故障时,手动升级备库为主节点
- su - postgres -c "/usr/local/pgsql/bin/repmgr standby promote -f /etc/repmgr.conf --siblings-follow"
- su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf standby follow"
- su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf cluster show"
-
- --故障节点修复后,重新加入集群
- su - postgres -c "/usr/local/pgsql/bin/repmgr node rejoin -d 'host=slave2 user=repmgr dbname=repmgr' --force-rewind --verbose -f /etc/repmgr.conf"
- su - postgres -c "/usr/local/pgsql/bin/repmgr -f /etc/repmgr.conf cluster show"
-
- --强制重新注册
- 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
读者专属技术群
构建高质量的技术交流社群,欢迎从事后端开发、运维技术进群(备注岗位,已在技术交流群的请勿重复添加)。主要以技术交流、内推、行业探讨为主,请文明发言。广告人士勿入,切勿轻信私聊,防止被骗。
扫码加我好友,拉你进群
推荐阅读 点击标题可跳转
PS:因为公众号平台更改了推送规则,如果不想错过内容,记得读完点一下“在看”,加个“星标”,这样每次新文章推送才会第一时间出现在你的订阅列表里。点“在看”支持我们吧!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。