PostgreSql repmgr 高可用部署

2024-01-22 12:48

本文主要是介绍PostgreSql repmgr 高可用部署,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 服务器配置

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

192.168.100.110 master
192.168.100.111 slave1
192.168.100.112 slave2

1.2 三台服务器配置互信

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

2. 三台服务器分别源码安装pg12.4

https://blog.csdn.net/songyundong1993/article/details/120053276

3. 三台服务器设置环境变量

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

4. 三台服务器流复制参数配置

cd /app/pgsql/data
vi postgresql.conflisten_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'

5. 三台服务器 repmgr 配置

5.1 三个节点均安装 repmgr

--下载并解压
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

5.2 参数文件配置

5.2.1 master 节点
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'
5.2.2 slave1 节点
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'
5.2.3 slave2 节点
#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'

6. master 节点配置数据库环境

6.1 创建repmgr数据库及用户

create database repmgr;
create user repmgr with password 'repmgr' superuser login;
alter database repmgr owner to repmgr;

6.2 配置pg_hba.conf

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                 trustlocal   replication     repmgr                                     trust
host    replication     repmgr             127.0.0.1/32            trust
host    replication     repmgr             192.168.220.0/24        trust

7. repmgr 集群构建

7.1 master 节点加入集群

--启动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"

7.2 slave1 节点加入集群

--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"

7.3 slave2 节点加入集群

--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"

8. 开启守护进程

--开启守护进程(故障自动转移)
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

9. 其他 repmgr 管理命令

--主备切换并查看
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 repmgr 高可用部署的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/633025

相关文章

SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程

《SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程》本文详细介绍了如何在虚拟机和宝塔面板中安装RabbitMQ,并使用Java代码实现消息的发送和接收,通过异步通讯,可以优化... 目录一、RabbitMQ安装二、启动RabbitMQ三、javascript编写Java代码1、引入

ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法

《ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法》本文介绍了Elasticsearch的基本概念,包括文档和字段、索引和映射,还详细描述了如何通过Docker... 目录1、ElasticSearch概念2、ElasticSearch、Kibana和IK分词器部署

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

Linux流媒体服务器部署流程

《Linux流媒体服务器部署流程》文章详细介绍了流媒体服务器的部署步骤,包括更新系统、安装依赖组件、编译安装Nginx和RTMP模块、配置Nginx和FFmpeg,以及测试流媒体服务器的搭建... 目录流媒体服务器部署部署安装1.更新系统2.安装依赖组件3.解压4.编译安装(添加RTMP和openssl模块

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应

redis群集简单部署过程

《redis群集简单部署过程》文章介绍了Redis,一个高性能的键值存储系统,其支持多种数据结构和命令,它还讨论了Redis的服务器端架构、数据存储和获取、协议和命令、高可用性方案、缓存机制以及监控和... 目录Redis介绍1. 基本概念2. 服务器端3. 存储和获取数据4. 协议和命令5. 高可用性6.

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

nginx部署https网站的实现步骤(亲测)

《nginx部署https网站的实现步骤(亲测)》本文详细介绍了使用Nginx在保持与http服务兼容的情况下部署HTTPS,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录步骤 1:安装 Nginx步骤 2:获取 SSL 证书步骤 3:手动配置 Nginx步骤 4:测

Tomcat高效部署与性能优化方式

《Tomcat高效部署与性能优化方式》本文介绍了如何高效部署Tomcat并进行性能优化,以确保Web应用的稳定运行和高效响应,高效部署包括环境准备、安装Tomcat、配置Tomcat、部署应用和启动T... 目录Tomcat高效部署与性能优化一、引言二、Tomcat高效部署三、Tomcat性能优化总结Tom

如何在本地部署 DeepSeek Janus Pro 文生图大模型

《如何在本地部署DeepSeekJanusPro文生图大模型》DeepSeekJanusPro模型在本地成功部署,支持图片理解和文生图功能,通过Gradio界面进行交互,展示了其强大的多模态处... 目录什么是 Janus Pro1. 安装 conda2. 创建 python 虚拟环境3. 克隆 janus