• 【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 软件包安装

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

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

    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

    安装依赖

    1. yum check-update
    2. yum groupinstall "Development Tools" -y
    3. yum install -y yum-utils openjade docbook-dtds docbook-style-dsssl docbook-style-xsl
    4. yum-builddep postgresql96// 执行不了 没有postgresql96yum源
    5. 用以下命令替代 
    6. yum  install -y  cmake make gcc zlib gcc-c++ perl readline readline-devel zlib // 自己安装pg13安装的依赖
    7. 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 创建用户与数据库

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

    3.4 配置认证文件 pg_hba.conf 

    1. # 允许用户 repmgr 通过local,127.0.0.1,10.79.21.网段连接到replication
    2. local replication repmgr trust
    3. host replication repmgr 127.0.0.1/32 trust
    4. host replication repmgr 10.79.21.0/24 trust
    5. # 允许用户 repmgr 通过local,127.0.0.1,10.79.21. 连接到repmgr schema
    6. local repmgr repmgr trust
    7. host repmgr repmgr 127.0.0.1/32 trust
    8. host repmgr repmgr 10.79.21.0/24 trust

    3.5 设置免密登录 

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

    1. 使用postgres用户 ,在两台服务器上
    2. 在node1上 
    3. ssh-keygen -t rsa
    4. ssh-copy-id postgres@10.79.21.29
    5. ssh postgres@10.79.21.29 date
    6. 在node2上 
    7. ssh-keygen -t rsa
    8. ssh-copy-id postgres@10.79.21.30
    9. ssh postgres@10.79.21.30 date
    10. // 数据库免密登录 IP:port:schema?:user:passwd
    11. vim .pgpass
    12. 10.79.21.30:5432:repmgr:repmgr:repmgr
    13. 10.79.21.29:5432:repmgr:repmgr:repmgr
    14. 修改权限,只允许postgres用户读写
    15. chmod 600 .pgpass

    3.6 repmgr配置文件

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

    vim repmgr.conf

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

    3.7 注册主节点

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

    repmgr -f /etc/repmgr.conf primary register

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

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

    repmgr -f /etc/repmgr.conf cluster show

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

    元数据表中的记录:

    1. repmgr=# select * from nodes;
    2. -[ RECORD 1 ]----+-----------------------------------------------------------------------
    3. node_id | 1
    4. upstream_node_id |
    5. active | t
    6. node_name | node1
    7. type | primary
    8. location | default
    9. priority | 100
    10. conninfo | host=10.79.21.30 port=5432 user=repmgr dbname=repmgr connect_timeout=2
    11. repluser | repmgr
    12. slot_name |
    13. config_file | /etc/repmgr.conf

    3.8 克隆 standby Server

    在从节点上编辑配置文件
    vim repmgr.conf
    1. node_id=2
    2. node_name='node2'
    3. conninfo='host=10.79.21.29 port=5432 user=repmgr dbname=repmgr connect_timeout=2'
    4. 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
    1. $repmgr -h 10.79.21.30 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --dry-run
    2. NOTICE: destination directory "/home/storage/pgsql/data" provided
    3. INFO: connecting to source node
    4. DETAIL: connection string is: host=10.79.21.30 user=repmgr dbname=repmgr
    5. DETAIL: current installation size is 116 MB
    6. INFO: "repmgr" extension is installed in database "repmgr"
    7. INFO: replication slot usage not requested; no replication slot will be set up for this standby
    8. INFO: parameter "max_wal_senders" set to 10
    9. NOTICE: checking for available walsenders on the source node (2 required)
    10. INFO: sufficient walsenders available on the source node
    11. DETAIL: 2 required, 10 available
    12. NOTICE: checking replication connections can be made to the source server (2 required)
    13. INFO: required number of replication connections could be made to the source server
    14. DETAIL: 2 replication connections required
    15. WARNING: data checksums are not enabled and "wal_log_hints" is "off"
    16. DETAIL: pg_rewind requires "wal_log_hints" to be enabled
    17. NOTICE: standby will attach to upstream node 1
    18. HINT: consider using the -c/--fast-checkpoint option
    19. INFO: would execute:
    20. pg_basebackup -l "repmgr base backup" -D /home/storage/pgsql/data -h 10.79.21.30 -p 5432 -U repmgr -X stream
    21. 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
    
    1. $repmgr -h 10.79.21.30 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone
    2. NOTICE: destination directory "/home/storage/pgsql/data" provided
    3. INFO: connecting to source node
    4. DETAIL: connection string is: host=10.79.21.30 user=repmgr dbname=repmgr
    5. DETAIL: current installation size is 116 MB
    6. INFO: replication slot usage not requested; no replication slot will be set up for this standby
    7. NOTICE: checking for available walsenders on the source node (2 required)
    8. NOTICE: checking replication connections can be made to the source server (2 required)
    9. INFO: checking and correcting permissions on existing directory "/home/storage/pgsql/data"
    10. NOTICE: starting backup (using pg_basebackup)...
    11. HINT: this may take some time; consider using the -c/--fast-checkpoint option
    12. INFO: executing:
    13. pg_basebackup -l "repmgr base backup" -D /home/storage/pgsql/data -h 10.79.21.30 -p 5432 -U repmgr -X stream
    14. NOTICE: standby clone (using pg_basebackup) complete
    15. NOTICE: you can now start your PostgreSQL server
    16. HINT: for example: pg_ctl -D /home/storage/pgsql/data start
    17. 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
    
    1. $repmgr -f /etc/repmgr.conf standby register
    2. INFO: connecting to local node "node2" (ID: 2)
    3. INFO: connecting to primary database
    4. WARNING: --upstream-node-id not supplied, assuming upstream node is primary (node ID: 1)
    5. INFO: standby registration complete
    6. NOTICE: standby node "node2" (ID: 2) successfully registered
    验证注册成功
    
    repmgr -f /etc/repmgr.conf cluster show
    1. $repmgr -f /etc/repmgr.conf cluster show
    2. ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string
    3. ----+-------+---------+-----------+----------+----------+----------+----------+------------------------------------------------------------------------
    4. 1 | node1 | primary | * running | | default | 100 | 1 | host=10.79.21.30 port=5432 user=repmgr dbname=repmgr connect_timeout=2
    5. 2 | 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高可用部署 - 墨天轮

  • 相关阅读:
    kafka ack确认机制
    深度学习1:神经网络原理与算法详解
    java毕业设计教学平台(附源码、数据库)
    PIMPL技巧
    Activiti7-基础(SpringBoot 2.6版)
    UACANet: Uncertainty Augmented Context Attention for Polyp Segmentation
    【RocketMQ 十二】RocketMQ工作原理之消息的消费
    机器学习基本知识(2)
    如何让ESP8266板子像APP开发一样方便
    如何做好工作汇报?这些要点要注意
  • 原文地址:https://blog.csdn.net/weixin_48154829/article/details/134383378