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

相关文章

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

postgresql数据库基本操作及命令详解

《postgresql数据库基本操作及命令详解》本文介绍了PostgreSQL数据库的基础操作,包括连接、创建、查看数据库,表的增删改查、索引管理、备份恢复及退出命令,适用于数据库管理和开发实践,感兴... 目录1. 连接 PostgreSQL 数据库2. 创建数据库3. 查看当前数据库4. 查看所有数据库

PostgreSQL数据库密码被遗忘时的操作步骤

《PostgreSQL数据库密码被遗忘时的操作步骤》密码遗忘是常见的用户问题,因此提供一种安全的遗忘密码找回机制是十分必要的,:本文主要介绍PostgreSQL数据库密码被遗忘时的操作步骤的相关资... 目录前言一、背景知识二、Windows环境下的解决步骤1. 找到PostgreSQL安装目录2. 修改p

PostgreSQL 默认隔离级别的设置

《PostgreSQL默认隔离级别的设置》PostgreSQL的默认事务隔离级别是读已提交,这是其事务处理系统的基础行为模式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一 默认隔离级别概述1.1 默认设置1.2 各版本一致性二 读已提交的特性2.1 行为特征2.2

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg

Web技术与Nginx网站环境部署教程

《Web技术与Nginx网站环境部署教程》:本文主要介绍Web技术与Nginx网站环境部署教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Web基础1.域名系统DNS2.Hosts文件3.DNS4.域名注册二.网页与html1.网页概述2.HTML概述3.

一文详解PostgreSQL复制参数

《一文详解PostgreSQL复制参数》PostgreSQL作为一款功能强大的开源关系型数据库,其复制功能对于构建高可用性系统至关重要,本文给大家详细介绍了PostgreSQL的复制参数,需要的朋友可... 目录一、复制参数基础概念二、核心复制参数深度解析1. max_wal_seChina编程nders:WAL

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

ubuntu如何部署Dify以及安装Docker? Dify安装部署指南

《ubuntu如何部署Dify以及安装Docker?Dify安装部署指南》Dify是一个开源的大模型应用开发平台,允许用户快速构建和部署基于大语言模型的应用,ubuntu如何部署Dify呢?详细请... Dify是个不错的开源LLM应用开发平台,提供从 Agent 构建到 AI workflow 编排、RA

ubuntu16.04如何部署dify? 在Linux上安装部署Dify的技巧

《ubuntu16.04如何部署dify?在Linux上安装部署Dify的技巧》随着云计算和容器技术的快速发展,Docker已经成为现代软件开发和部署的重要工具之一,Dify作为一款优秀的云原生应用... Dify 是一个基于 docker 的工作流管理工具,旨在简化机器学习和数据科学领域的多步骤工作流。它