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

相关文章

通过Docker容器部署Python环境的全流程

《通过Docker容器部署Python环境的全流程》在现代化开发流程中,Docker因其轻量化、环境隔离和跨平台一致性的特性,已成为部署Python应用的标准工具,本文将详细演示如何通过Docker容... 目录引言一、docker与python的协同优势二、核心步骤详解三、进阶配置技巧四、生产环境最佳实践

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

redis-sentinel基础概念及部署流程

《redis-sentinel基础概念及部署流程》RedisSentinel是Redis的高可用解决方案,通过监控主从节点、自动故障转移、通知机制及配置提供,实现集群故障恢复与服务持续可用,核心组件包... 目录一. 引言二. 核心功能三. 核心组件四. 故障转移流程五. 服务部署六. sentinel部署

PostgreSQL简介及实战应用

《PostgreSQL简介及实战应用》PostgreSQL是一种功能强大的开源关系型数据库管理系统,以其稳定性、高性能、扩展性和复杂查询能力在众多项目中得到广泛应用,本文将从基础概念讲起,逐步深入到高... 目录前言1. PostgreSQL基础1.1 PostgreSQL简介1.2 基础语法1.3 数据库

Linux部署中的文件大小写问题的解决方案

《Linux部署中的文件大小写问题的解决方案》在本地开发环境(Windows/macOS)一切正常,但部署到Linux服务器后出现模块加载错误,核心原因是Linux文件系统严格区分大小写,所以本文给大... 目录问题背景解决方案配置要求问题背景在本地开发环境(Windows/MACOS)一切正常,但部署到

Oracle迁移PostgreSQL隐式类型转换配置指南

《Oracle迁移PostgreSQL隐式类型转换配置指南》Oracle迁移PostgreSQL时因类型差异易引发错误,需通过显式/隐式类型转换、转换关系管理及冲突处理解决,并配合验证测试确保数据一致... 目录一、问题背景二、解决方案1. 显式类型转换2. 隐式转换配置三、维护操作1. 转换关系管理2.

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数

MySQL 主从复制部署及验证(示例详解)

《MySQL主从复制部署及验证(示例详解)》本文介绍MySQL主从复制部署步骤及学校管理数据库创建脚本,包含表结构设计、示例数据插入和查询语句,用于验证主从同步功能,感兴趣的朋友一起看看吧... 目录mysql 主从复制部署指南部署步骤1.环境准备2. 主服务器配置3. 创建复制用户4. 获取主服务器状态5

PostgreSQL中rank()窗口函数实用指南与示例

《PostgreSQL中rank()窗口函数实用指南与示例》在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名... 目录一、rank()函数简介二、基础示例:部门内员工薪资排名示例数据排名查询三、高级应用示例1. 每