【PG】PostgreSQL高可用方案repmgr部署(非常详细)

目录

简介

1 概述

1.1 术语

1.2 组件

1.2.1 repmgr

1.2.2 repmgrd

1.3 Repmgr用户与元数据

2 安装部署

2.0 部署环境 

2.1 安装要求

2.1.1 操作系统

2.1.2 PostgreSQL 版本

2.1.3 操作系统用户

2.1.4 安装位置

2.1.5 版本要求

2.2 安装

2.2.1 软件包安装

2.2.2 源码编译安装

3 快速开始

3.2 PostgreSQL部署主库

3.3 创建用户与数据库

3.4 配置认证文件 pg_hba.conf 

3.5 设置免密登录 

3.6 repmgr配置文件

3.7 注册主节点

3.8 克隆 standby Server

3.9 验证主从同步正常

3.10 注册从节点

参考 

repmgr官网

5.3.3版本文档

最新文档(5.4版本)

github repmgr


简介

repmgr是一套开源工具,用于管理 PostgreSQL 服务器集群内的复制和故障转移。它通过设置备用服务器、监控复制以及执行故障转移或切换操作等管理任务的实用程序增强了 PostgreSQL 的内置复制功能。

1 概述

1.1 术语

整个repmgr文档 中使用了以下术语

  • replication cluster:在repmgr文档中,“replication cluster”是指通过流式复制连接的 PostgreSQL 服务器网络。
  • node :节点是复制集群中的单个 PostgreSQL 服务器。
  • upstream node:备用服务器连接到的节点,以便接收流式复制。这可以是主服务器,或者在级联复制的情况下是另一台备用服务器。
  • failover:这是在主服务器发生故障并且合适的备用服务器升​​级为新的主服务器时发生的操作。repmgrd守护程序支持自动故障转移以最大限度地减少停机时间 。
  • switchover:在某些情况下,例如硬件或操作系统维护,有必要使主服务器离线;在这种情况下,需要进行受控切换,从而提升合适的备用数据库,并以受控方式从复制集群中删除现有的主数据库。repmgr命令行客户端提供此功能 。
  • fencing:在故障转移情况下,在升级新的备用数据库后,之前的主数据库不会意外地重新上线,否则会导致脑裂情况。为了防止这种情况发生,发生故障的主数据库应与应用程序隔离,即“隔离”。
  • witness server:epmgr提供了设置所谓“witness server”的功能,以帮助在具有多个备用服务器的故障转移情况下确定新的主服务器。witness server本身不是复制集群的一部分,尽管它包含 repmgr 元数据架构的副本。

1.2 组件

repmgr是一套开源工具,用于管理 PostgreSQL 服务器集群内的复制和故障转移。它支持并增强 PostgreSQL 的内置流复制,该复制提供单个读/写主服务器和一个或多个包含主服务器数据库的近实时副本的只读备用服务器。它提供了两个主要工具:

1.2.1 repmgr

用于执行管理任务的命令行工具,例如:

  • 设置备用服务器
  • 将备用服务器升​​级为主服务器
  • 主备服务器切换
  • 显示复制集群中服务器的状态

1.2.2 repmgrd

主动监视复制集群中的服务器并执行以下任务的守护程序:

  • 监控和记录复制性能
  • 通过检测主服务器的故障并升级最合适的备用服务器来执行故障转移
  • 向用户定义的脚本提供有关集群中事件的通知,该脚本可以执行通过电子邮件发送警报等任务

1.3 Repmgr用户与元数据

为了有效地管理复制集群,repmgr需要将集群中服务器的信息存储在专用的数据库schema中。此架构由repmgr扩展自动创建,该扩展在初始化repmgr管理的集群 ( repmgr primary register)的第一步中安装,并包含以下对象:

table

  • repmgr.events:记录关注的事件
  • repmgr.nodes:复制集群中每个服务器的连接和状态信息
  • repmgr.monitoring_history: repmgrd写入的历史备用监控信息

view

  • repmgr.show_nodes:基于 table repmgr.nodes,另外显示服务器上游节点的名称
  • repmgr.replication_status:当启用repmgrd的监控时,显示每个备用数据库的当前监控状态。

repmgr元数据模式可以存储在现有数据库或其自己的专用数据库中。请注意,repmgr 元数据模式不能保存在不属于repmgr管理的复制集群的数据库服务器上。

数据库用户必须可供repmgr访问该数据库并修改的权限。该用户不需要是超级用户,但是某些操作(例如repmgr扩展的初始安装)将需要超级用户连接(可以在需要时使用命令行选项指定 --superuser)。

2 安装部署

2.0 部署环境 

PostgreSQL:13.12

repmgr 5.3.3

CentOS Linux release 7.2.1511 (Core)

repmgr可以从操作系统的打包系统提供的二进制包或源代码安装。一般来说,我们建议使用二进制包,除非您的操作系统不可用。

2.1 安装要求

2.1.1 操作系统

repmgr 是在 Linux 和 OS X 上开发和测试的,但应该可以在 PostgreSQL 本身支持的任何类 UNIX 系统上运行。不支持 Microsoft Windows。

2.1.2 PostgreSQL 版本

复制集群中的所有服务器必须运行相同的 PostgreSQL 主要版本,我们建议它们也运行相同的次要版本。

2.1.3 操作系统用户

不需要repmgr 专用系统用户;由于许多repmgr和 repmgrd操作需要直接访问 PostgreSQL 数据目录,因此这些命令应由postgres用户执行。

2.1.4 安装位置

repmgr必须安装在复制集群中的每台服务器上。如果从软件包安装 repmgr,软件包版本必须与 PostgreSQL 版本匹配。如果从源代码安装,repmgr必须针对相同的主要版本进行编译。

2.1.5 版本要求

下表概述了repmgr版本支持的 PostgreSQL 版本。

版本兼容矩阵
repmgr 版本

Supported?

是否维护

Latest release

最新版本

Supported PostgreSQL versions

支持的PostgreSQL版本

Notes 

注意

repmgr 5.4(dev)5.4.1 (2023-07-04)9.4, 9.5, 9.6, 10, 11, 12, 13, 15
repmgr 5.3YES5.4.1 (2023-07-04)9.4, 9.5, 9.6, 10, 11, 12, 13, 14, 15PostgreSQL 15 supported from repmgr 5.3.3
repmgr 5.2NO5.2.1 (2020-12-07)9.4, 9.5, 9.6, 10, 11, 12, 13
repmgr 5.1NO5.1.0 (2020-04-13)9.3, 9.4, 9.5, 9.6, 10, 11, 12
repmgr 5.0NO5.0 (2019-10-15)9.3, 9.4, 9.5, 9.6, 10, 11, 12
repmgr 4.xNO4.4 (2019-06-27)9.3, 9.4, 9.5, 9.6, 10, 11
repmgr 3.xNO3.3.2 (2017-05-30)9.3, 9.4, 9.5, 9.6
repmgr 2.xNO2.0.3 (2015-04-16)9.0, 9.1, 9.2, 9.3, 9.4

2.2 安装

2.2.1 软件包安装

这个命令的含义是下载一个文件然后执行 这个需要外网 ,生产数据库服务器一般没有外网 ,

curl https://dl.enterprisedb.com/default/release/get/13/rpm | sudo bash
yum list repmgr13yum install  repmgr13 -y 

2.2.2 源码编译安装

如果服务器没有外网 只能使用源码安装 

下载源码的地址 下面两个地址都可以 一个是官网上的下载地址,一个是gtihub的下载地址

wget https://github.com/EnterpriseDB/repmgr/archive/refs/tags/v5.3.3.tar.gz

// 我使用的这个

wget https://www.repmgr.org/download/repmgr-5.3.3.tar.gz

安装依赖

yum check-update
yum groupinstall "Development Tools" -y
yum install -y yum-utils openjade docbook-dtds docbook-style-dsssl docbook-style-xsl
yum-builddep postgresql96// 执行不了 没有postgresql96yum源用以下命令替代 
yum  install -y  cmake make gcc zlib gcc-c++ perl readline readline-devel zlib // 自己安装pg13安装的依赖yum -y install yum-builddep flex libselinux-devel libxml2-devel libxslt-devel openssl-devel pam-devel readline-devel // 博客上的

下载源码包

wget https://www.repmgr.org/download/repmgr-5.3.3.tar.gz

解压安装repmgr(主备库都安装)

tar -zxvf repmgr-5.3.3.tar.gz

使用postgres用户安装

chown -R postgres:postgres /home/packet/repmgr-5.3.3

su - postgres

cd  /home/packet/repmgr-5.3.3

 ./configure

make install

输出一下内容说明二进制命令安装到了目录'/usr/local/pgsql/bin/下 

/bin/install -c -m 755 repmgr repmgrd '/usr/local/pgsql/bin/'

进入到目录中查看/usr/local/pgsql/bin/ 多了以下两个命令 

 可以通过repmgr  --help 验证安装是否成功

3 快速开始

3.2 PostgreSQL部署主库

参考我的博客只部署主库即可 【PG】PostgreSQL13主从流复制部署(详细可用)-CSDN博客

3.3 创建用户与数据库

// 创建用户 需要超级用户权限
create user repmgr with superuser password 'repmgr'  connection limit 10;// 创建元数据库create database repmgr owner  repmgr;// repmgr会创建repmgr schema 来报错repmgr的元数据表,函数,视图等,建议设置用户repmgr的搜索路径如下
ALTER USER repmgr SET search_path TO repmgr, "$user", public;

3.4 配置认证文件 pg_hba.conf 

# 允许用户 repmgr 通过local,127.0.0.1,10.79.21.网段连接到replication 
local   replication   repmgr                              trust
host    replication   repmgr      127.0.0.1/32            trust
host    replication   repmgr      10.79.21.0/24           trust# 允许用户 repmgr  通过local,127.0.0.1,10.79.21. 连接到repmgr schema
local   repmgr        repmgr                              trust
host    repmgr        repmgr      127.0.0.1/32            trust
host    repmgr        repmgr      10.79.21.0/24           trust

3.5 设置免密登录 

如果之前postgres 没有密码 ,需要先设置一个密码 ,否则下面ssh拷贝公钥时会提示输入密码

使用postgres用户 ,在两台服务器上在node1上 
ssh-keygen -t rsa
ssh-copy-id postgres@10.79.21.29
ssh postgres@10.79.21.29 date在node2上 
ssh-keygen -t rsa
ssh-copy-id postgres@10.79.21.30
ssh postgres@10.79.21.30 date// 数据库免密登录 IP:port:schema?:user:passwd
vim .pgpass
10.79.21.30:5432:repmgr:repmgr:repmgr
10.79.21.29:5432:repmgr:repmgr:repmgr修改权限,只允许postgres用户读写
chmod  600 .pgpass

3.6 repmgr配置文件

repmgr.conf不应存储在 PostgreSQL 数据目录中,因为在设置或重新初始化 PostgreSQL 服务器时它可能会被覆盖

vim repmgr.conf

node_id=1
node_name='node1'
conninfo='host=10.79.21.30 port=5432 user=repmgr dbname=repmgr connect_timeout=2'
data_directory='/home/storage/pgsql/data'

3.7 注册主节点

要使repmgr支持复制集群,主节点必须向repmgr注册。这将安装repmgr 扩展和元数据对象,并为主服务器添加元数据记录

repmgr -f /etc/repmgr.conf primary register

$repmgr -f /etc/repmgr.conf primary register
INFO: connecting to primary database...
NOTICE: attempting to install extension "repmgr"
NOTICE: "repmgr" extension successfully installed
NOTICE: primary node record (ID: 1) registered

验证集群的状态,如下所示:

repmgr -f /etc/repmgr.conf cluster show

$repmgr -f /etc/repmgr.conf cluster showID | Name  | Role    | Status    | Upstream | Location | Priority | Timeline | Connection string
----+-------+---------+-----------+----------+----------+----------+----------+------------------------------------------------------------------------1  | node1 | primary | * running |          | default  | 100      | 1        | host=10.79.21.30 port=5432 user=repmgr dbname=repmgr connect_timeout=2

元数据表中的记录:

repmgr=# select *  from  nodes;
-[ RECORD 1 ]----+-----------------------------------------------------------------------
node_id          | 1
upstream_node_id |
active           | t
node_name        | node1
type             | primary
location         | default
priority         | 100
conninfo         | host=10.79.21.30 port=5432 user=repmgr dbname=repmgr connect_timeout=2
repluser         | repmgr
slot_name        |
config_file      | /etc/repmgr.conf

3.8 克隆 standby Server

在从节点上编辑配置文件
vim repmgr.conf
node_id=2
node_name='node2'
conninfo='host=10.79.21.29 port=5432 user=repmgr dbname=repmgr connect_timeout=2'
data_directory='/home/storage/pgsql/data'

 使用参数--dry-run 检查是否可以克隆从库

repmgr -h 10.79.21.30 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --dry-run
  • 检查目录
  • 检查参数 max_wal_senders 是否大于2 
  • 检查参数 wal_log_hints
  • 检查通过会执行备份命令   pg_basebackup -l "repmgr base backup"  -D /home/storage/pgsql/data -h 10.79.21.30 -p 5432 -U repmgr -X stream
$repmgr -h 10.79.21.30 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --dry-run
NOTICE: destination directory "/home/storage/pgsql/data" provided
INFO: connecting to source node
DETAIL: connection string is: host=10.79.21.30 user=repmgr dbname=repmgr
DETAIL: current installation size is 116 MB
INFO: "repmgr" extension is installed in database "repmgr"
INFO: replication slot usage not requested;  no replication slot will be set up for this standby
INFO: parameter "max_wal_senders" set to 10
NOTICE: checking for available walsenders on the source node (2 required)
INFO: sufficient walsenders available on the source node
DETAIL: 2 required, 10 available
NOTICE: checking replication connections can be made to the source server (2 required)
INFO: required number of replication connections could be made to the source server
DETAIL: 2 replication connections required
WARNING: data checksums are not enabled and "wal_log_hints" is "off"
DETAIL: pg_rewind requires "wal_log_hints" to be enabled
NOTICE: standby will attach to upstream node 1
HINT: consider using the -c/--fast-checkpoint option
INFO: would execute:pg_basebackup -l "repmgr base backup"  -D /home/storage/pgsql/data -h 10.79.21.30 -p 5432 -U repmgr -X stream
INFO: all prerequisites for "standby clone" are met

可以看到个warning  ,将参数 wal_log_hints 设置为 on ,再次检查 

如果检查没有问题 ,就可以进行克隆

repmgr -h 10.79.21.30 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone
$repmgr -h 10.79.21.30 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone
NOTICE: destination directory "/home/storage/pgsql/data" provided
INFO: connecting to source node
DETAIL: connection string is: host=10.79.21.30 user=repmgr dbname=repmgr
DETAIL: current installation size is 116 MB
INFO: replication slot usage not requested;  no replication slot will be set up for this standby
NOTICE: checking for available walsenders on the source node (2 required)
NOTICE: checking replication connections can be made to the source server (2 required)
INFO: checking and correcting permissions on existing directory "/home/storage/pgsql/data"
NOTICE: starting backup (using pg_basebackup)...
HINT: this may take some time; consider using the -c/--fast-checkpoint option
INFO: executing:pg_basebackup -l "repmgr base backup"  -D /home/storage/pgsql/data -h 10.79.21.30 -p 5432 -U repmgr -X stream
NOTICE: standby clone (using pg_basebackup) complete
NOTICE: you can now start your PostgreSQL server
HINT: for example: pg_ctl -D /home/storage/pgsql/data start
HINT: after starting the server, you need to register this standby with "repmgr standby register"

3.9 验证主从同步正常

在主库上查看 

 SELECT * FROM pg_stat_replication;

可以看到 

standy 上查看 

 SELECT * FROM pg_stat_wal_receiver;

3.10 注册从节点

使用以下命令注册从节点

repmgr -f /etc/repmgr.conf standby register
$repmgr -f /etc/repmgr.conf standby register
INFO: connecting to local node "node2" (ID: 2)
INFO: connecting to primary database
WARNING: --upstream-node-id not supplied, assuming upstream node is primary (node ID: 1)
INFO: standby registration complete
NOTICE: standby node "node2" (ID: 2) successfully registered
验证注册成功repmgr -f /etc/repmgr.conf cluster show
$repmgr -f /etc/repmgr.conf cluster showID | Name  | Role    | Status    | Upstream | Location | Priority | Timeline | Connection string
----+-------+---------+-----------+----------+----------+----------+----------+------------------------------------------------------------------------1  | node1 | primary | * running |          | default  | 100      | 1        | host=10.79.21.30 port=5432 user=repmgr dbname=repmgr connect_timeout=22  | node2 | standby |   running | node1    | default  | 100      | 1        | host=10.79.21.29 port=5432 user=repmgr dbname=repmgr connect_timeout=2

参考 

repmgr官网

repmgr - Replication Manager for PostgreSQL clusters

5.3.3版本文档

https://www.repmgr.org/docs/5.3/index.html

最新文档(5.4版本)

repmrg文档

github repmgr

GitHub - EnterpriseDB/repmgr: A lightweight replication manager for PostgreSQL (Postgres)

参考

进阶数据库系列(十八):PostgreSQL 基于 repmgr 高可用架构实践_mingongge的博客-CSDN博客

PostgreSQL+repmgr高可用部署 - 墨天轮

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/172096.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据结构—数组栈的实现

前言:各位小伙伴们我们前面已经学习了带头双向循环链表,数据结构中还有一些特殊的线性表,如栈和队列,那么我们今天就来实现数组栈。 目录: 一、 栈的概念 二、 栈的实现 三、 代码测试 栈的概念: 栈的概念…

通过 Elasticsearch 和 Go 使用混合搜索进行地鼠狩猎

作者:CARLY RICHMOND,LAURENT SAINT-FLIX 就像动物和编程语言一样,搜索也经历了不同实践的演变,很难在其中做出选择。 在本系列的最后一篇博客中,Carly Richmond 和 Laurent Saint-Flix 将关键字搜索和向量搜索结合起…

idea中搭建Spring boot项目(借助Spring Initializer)

创建新项目 启动端口 在项目配置文件application.properties中写入 #启动端口server.port8088编写测试方法 创建控制类文件夹–>便于规范我们新建一个controller包–>建一个HelloWorld.class package com.example.hellospringboot.controller;import org.springframew…

什么是安全平行切面

安全平行切面的定义 通过嵌入在端—管—云内部的各层次切点,使得安全管控与业务逻辑解耦,并通过标准化的接口为安全业务提供内视和干预能力的安全基础设施。安全平行切面是一种创新的安全体系思想,是实现“原生安全”的一条可行路径。 为什…

深度学习 YOLO 实现车牌识别算法 计算机竞赛

文章目录 0 前言1 课题介绍2 算法简介2.1网络架构 3 数据准备4 模型训练5 实现效果5.1 图片识别效果5.2视频识别效果 6 部分关键代码7 最后 0 前言 🔥 优质竞赛项目系列,今天要分享的是 🚩 基于yolov5的深度学习车牌识别系统实现 该项目较…

数据结构(超详细讲解!!)第二十三节 树型结构

1.定义 树型结构是一类重要的非线性数据结构,是以分支关系定义的层次结构。是一种一对多的逻辑关系。 树型结构是结点之间有分支,并且具有层次关系的结构,它非常类似于自然界中的树。树结构在客观世界中是大量存在的,例如家谱、…

空间数据结构笔记:层次包围盒树(Bounding Volume Hierarchy Based On Tree)

1 总览 层次包围盒树(BVH树)是一棵多叉树,用来存储包围盒形状。它的根节点代表一个最大的包围盒,其多个子节点则代表多个子包围盒。为了统一化层次包围盒树的形状,它只能存储同一种包围盒形状 2 AABB包围盒树&#x…

MyBatis解析全局配置文件

MyBatis解析全局配置文件 MyBaits基础应用: 文档:MyBatis 链接:http://note.youdao.com/noteshare?id5d41fd41d970f1af9185ea2ec0647b64 传统JDBC和Mybatis相比的弊病 传统JDBC ​ Connection conn null; PreparedStatement pstmt …

[单片机课程设计报告汇总] 单片机设计报告常用硬件元器件描述

[单片机课程设计必看] 单片机设计报告常用描述 硬件设计 AT89C51最小系统 AT89C51是美国ATMEL公司生产的低电压,高性能CMOS16位单片机,片内含4k bytes的可反复擦写的只读程序存储器和128 bytes的随机存取数据存储器,期间采用ATMEL公司的高…

使用select实现定时任务

selectOutOfTime.c里边的代码如下&#xff1a; #include<stdio.h> #include<sys/time.h> #include<sys/types.h> #include<unistd.h> #include <string.h>#define BUF_SIZE 100int main(void){fd_set reads;struct timeval tv;int errorNum;cha…

linux openlab搭建web网站

网站需求&#xff1a; 1.基于域名 www.openlab.com 可以访问网站内容为 welcome to openlab!!! 2.给该公司创建三个子界面分别显示学生信息&#xff0c;教学资料和缴费网站&#xff0c; 1、基于 www.openlab.com/student 网站访问学生信息&#xff0c; 2、基于 www.openlab…

车间安灯呼叫看板的功能与优势介绍

现在的工厂车间的管理变得越来越复杂&#xff0c;生产过程中可能会出现各种问题&#xff0c;如设备故障、物料短缺、工人伤病等。为了提高生产效率、优化生产管理&#xff0c;许多工厂引入了车间安灯呼叫看板系统。本文将介绍车间安灯呼叫看板的功能与优势。 一、功能介绍 1. 实…