首页 » SEO优化 » phppgpool技巧_PostgreSQL高可用套件repmgrpgpool

phppgpool技巧_PostgreSQL高可用套件repmgrpgpool

duote123 2024-11-21 0

扫一扫用手机浏览

文章目录 [+]

一、背景概述

关于PosgreSQL高可用管理套件,PG生态圈供应许多人气比较高的开源高可用和故障转移工具,比如Patroni、Repmgr、PAF、Stolon等。
下面重点给大家推举关于repmgr这款精大略单纯用的流复制工具。
个人以为,repmgr与声名远扬的ORACLE ADG逻辑复制工具非常类似。
它的功能强大,安装和配置大略,有很强的可操控性。
下面也会先容两种异构数据库不用的复制办法的工具。

phppgpool技巧_PostgreSQL高可用套件repmgrpgpool

repmgr是一个2ndQuadrant开拓的一款复制的开源工具套件,用于管理PostgreSQL做事器集群中的复制和故障转移。
最初,它紧张是为了简化流副本的管理,后来发展成为一个完全的故障转移管理套件。
它通过设置备用做事器,监视复制以及实行管理任务(如故障转移或手动切换操作)的工具,增强了PostgreSQL内置的热备份功能。
Repmgr体系架构如下:

phppgpool技巧_PostgreSQL高可用套件repmgrpgpool
(图片来自网络侵删)

在入门repmgr之前务必需要理解的组件功能模块:

1.1 Repmgrd 守护进程

它主动监视复制集群中的做事器并实行以下任务:

监控和记录集群复制性能通过检测主理事器故障并提升最得当的备用做事器来实行故障转移将有关群集中事宜的关照供应给用户定义的脚本,该脚本可以实行诸如通过电子邮件发送警报等任务repmgrd 根据本地数据库角色不同,其功能也不同:

主库:repmgrd仅监控本地数据库,卖力自动规复、同异步切换

备库:repmgrd监控本地数据库和主数据库,卖力自动切换、复制槽删除

1.2 Repmgr命令管理

用于实行管理任务的命令行工具,紧张有以下方面浸染:

设置备用做事器将备用做事器升级为主理事器切换主理事器和备用做事器显示复制群集中的做事器状态

1.3 用户和元数据

为了有效地管理复制集群,repmgr供应专用数据库存储和管理有关repmgr集群做事的干系信息。
此模式在支配repmgr做事时,由repmgr扩展自动创建,该扩展在初始化repmgr -administered集群(repmgr主寄存器)的第一步中安装,包含以下工具:

1、表

repmgr.events:记录感兴趣的事宜

repmgr.nodes:复制群集中每个做事器的连接和状态信息

repmgr.monitoring_history:repmgrd写入的历史备用监视信息

2、视图

repmgr.show_nodes:基于表repmgr.nodes,其余显示做事器上游节点的名称

repmgr.replication_status:启用repmgrd的监视时,显示每个备用数据库确当前监视状态。

repmgr元数据模式可以存储在现有的数据库或在自己的专用数据库。

请把稳,repmgr元数据模式不能驻留在不属于repmgr管理的复制集群的数据库做事器上。

数据库用户必须可供repmgr访问此数据库并实行必要的变动。

此用户不须要是超级用户,但是某些操作(如初始安装repmgr扩展)将须要超级用户连接(可以利用命令行选项--superuser在须要时指定 )。

二、安装支配

2.1 环境准备

安装环境须要把稳以下哀求:

做事主机名主机IP操作系统支配版本主节点lightdb1192.168.127.135CentOS 8.4pg13.3 repmgr-5.2.1备节点1lightdb2192.168.127.136CentOS 8.4pg13.3 repmgr-5.2.1备节点2lightdb3192.168.127.137CentOS 8.4pg13.3 repmgr-5.2.1见证节点lightdb4192.168.127.138CentOS 8.4pg13.3 repmgr-5.2.1连接池1lightdb5192.168.127.126CentOS 8.4pgpool-II-4.2.2连接池2lightdb6192.168.127.127CentOS 8.4pgpool-II-4.2.2

版本哀求:repmgr必须安装在复制群集中的每台做事器上。
如果从软件包安装repmgr,则软件包版本必须与PostgreSQL版本匹配。
如果从源安装,则必须针对相同的主版本编译repmgr。

环境依赖:repmgr有一些必要的依赖项,以使其能够最佳地运行,并且该当为系统供应某些最低限度的功能。
本案例是在Centos7.X操作系统利用repmgr最新5.1的版本。
在生产环境,可以选择根据实际情形选择操作系统和对应的repmgr和PostgreSQL版本。

如果是Rehat的操作系统系统可通过安装相应的RPM来添加PostgreSQL存储库:https://yum.postgresql.org/repopackages.php,也可以通过源码来进行安装。

通信配置:建议repmgr通过配置SSH免密在节点之间进行通信,方便运维操作。
但如果您企业对主机互信有特定的安全哀求和约束,可以利用配置管理来管理做事器密钥,比如Ansible、Puppet或Salt等。

2.2 安装步骤

步骤1:配置主机hosts文件vim /etc/hosts,添加主机ip和name

192.168.127.135 lightdb1192.168.127.136 lightdb2192.168.127.137 lightdb3192.168.127.138 lightdb4 192.168.127.126 lightdb5192.168.127.127 lightdb6 [root@localhost ~]# hostnamectl set-hostname lightdb1[root@localhost ~]# hostnamelightdb1[root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# systemctl disable firewalld.service Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.[root@localhost ~]# cat >> /etc/hosts << EOF> 192.168.127.135 lightdb1> 192.168.127.136 lightdb2> 192.168.127.137 lightdb3> 192.168.127.138 lightdb4>192.168.127.126 lightdb5>192.168.127.127 lightdb6> EOF[root@localhost ~]# systemctl stop firewalld[root@localhost ~]# systemctl disable firewalld[root@localhost ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config[root@localhost ~]# setenforce 0[root@localhost ~]# dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpmCentOS Linux 8 - AppStream 688 kB/s | 8.1 MB 00:12 CentOS Linux 8 - BaseOS 674 kB/s | 3.6 MB 00:05 CentOS Linux 8 - Extras 16 kB/s | 9.8 kB 00:00 pgdg-redhat-repo-latest.noarch.rpm 3.0 kB/s | 12 kB 00:03 Dependencies resolved.=========================================================================================================================================================================== Package Architecture Version Repository Size===========================================================================================================================================================================Installing: pgdg-redhat-repo noarch 42.0-18 @commandline 12 k?Transaction Summary===========================================================================================================================================================================Install 1 Package?Total size: 12 kInstalled size: 12 kDownloading Packages:Running transaction checkTransaction check succeeded.Running transaction testTransaction test succeeded.Running transaction Preparing : 1/1 Installing : pgdg-redhat-repo-42.0-18.noarch 1/1 Verifying : pgdg-redhat-repo-42.0-18.noarch 1/1 Installed products updated.?Installed: pgdg-redhat-repo-42.0-18.noarch ?Complete!

步骤2:在lightdb1主机用postgres用户创建mkdir ~/.ssh目录并实行以下命令,通过SSH访问postgres用户到所有节点,rmha2和rmha3重复实行。

ssh-keygenssh-copy-id -i /home/postgres/.ssh/id_rsa.pub postgres@192.168.127.135ssh-copy-id -i /home/postgres/.ssh/id_rsa.pub postgres@192.168.127.136ssh-copy-id -i /home/postgres/.ssh/id_rsa.pub postgres@192.168.127.137ssh-copy-id -i /home/postgres/.ssh/id_rsa.pub postgres@192.168.127.138

2.3 repmgr源代码安装

要安装repmgr,必须安装编译PostgreSQL的先决条件。
大多数主流Linux发行版和其他UNIX变体都供应了从包中安装先决条件的大略方法。
为您的目标repmgr版本选择适当的PostgreSQL版本。

Rhel或CentOSx或7.X:从yum.postgresql.org为你的系统安装适当的存储库RPM。
然后安装构建PostgreSQL的先决条件:

sudo yum check-updatesudo yum groupinstall 'Development Tools'sudo yum install yum-utils openjade docbook-dtds docbook-style-dsssl docbook-style-xslsudo yum-builddep postgresql96

可通过两种办法获取repmgr源代码:利用git或者下载发布版本的压缩包。
示例利用git下载源码包。
每个repmgr版本都有相应的标签。
repmgr的源文件下载地址在:https://github.com/2ndQuadrant/repmgr

git clone https://github.com/2ndQuadrant/repmgr

在利用tar -xvzf解压源代码归档文件之后,安装过程与从git克隆文件安装相同。
从源代码安装repmgr,利用postgres用户实行编译:

tar -zxvf repmgr-5.2.1.tar.gzsudo mv repmgr-5.2.1 /home/postgres/postgresql-13.3/contrib/repmgrcd /home/postgres/postgresql-13.3/contrib/repmgr./configuresudo make && sudo make install

源码编译之后会自动在PGHOME的extension/天生repmgr.control以及对应的repmgrd做事。

2.4 配置做事

1. 配置主库

我们将须要一台名为lightdb1的做事器。
/data/postgresql/data是本案例默认的PGDATA。
配置遵照以下步骤:

1)连接到PostgreSQL并实行以下命令来创建一个用于管理repmgr的用户和数据库

create user repmgr with password 'repmgr' superuser replication;create database repmgr owner repmgr;

2)修正postgresql.conf文件并设置以下参数:

listen_addresses = ''shared_preload_libraries = 'repmgr'wal_log_hints = 'on'wal_level = replicahot_standby = on

2. 修正pg_hba.conf文件并添加以下行:

# TYPE DATABASE USER ADDRESS METHOD# 'local' is for Unix domain socket connections onlylocal all all trustlocal repmgr repmgr trust# IPv4 local connections:host all all 127.0.0.1/32 trusthost all all 0.0.0.0/0 trusthost repmgr repmgr 192.168.127.135/0 trusthost repmgr repmgr 192.168.127.136/0 trusthost repmgr repmgr 192.168.127.137/0 trusthost repmgr repmgr 192.168.127.138/0 trust# IPv6 local connections:host all all ::1/128 trust# Allow replication connections from localhost, by a user with the# replication privilege.local replication all trusthost replication all 127.0.0.1/32 trusthost replication all ::1/128 trustlocal replication repmgr trusthost replication repmgr 192.168.127.135/0 trusthost replication repmgr 192.168.127.136/0 trusthost replication repmgr 192.168.127.137/0 trusthost replication repmgr 192.168.127.138/0 trust

3. 重新加载PostgreSQL做事

4. 创建一个名为/data/postgresql/etc/repmgr.conf的文件,包含以下内容:

sudo mkdir -pv /data/postgresql/etc/sudo chown -R postgres:postgres /data/postgresql/touch /data/postgresql/etc/repmgr.confcat >> /data/postgresql/etc/repmgr.conf << EOF/repmgr基本配置/node_id=1node_name='lightdb1'conninfo='host=192.168.127.135 port=5432 dbname=repmgr user=repmgr connect_timeout=2'data_directory ='/data/postgresql/data'pg_bindir='/data/postgresql/bin'config_directory='/data/postgresql/data'/设置日志参数/log_level=INFOlog_facility=STDERRlog_file='/data/postgresql/etc/repmgr.log'EOF

在利用repmgr命令须要指定repmgr.conf路径,通过-f/--config-file参数指定路径。
如果找不到或无法读取该文件,则会引发缺点,并且不会考试测验检讨默认位置。
这是为了防止repmgr意外读取缺点的配置文件。
为理解决这个问题,可以将repmgr.con创建在pg_conf默认的sysconfidir路径下:

例如:通过查询sysconfdir路径

[postgres@lightdb1 etc]$ pg_config --sysconfdir/data/postgresql/etc

创建/data/postgresql/etc/repmgr.conf,并配置文件。

这样可以避免利用repmgr命令还须要指定配置文件目录。

把稳:Red Hat和CentOS系统及类似的系统,如果是通过yum源安装repmgr,安装完会在/etc/repmgr/12/目录自动天生一个repmgr.conf配置文件。

repmgr.conf配置文件是管理repmgr做事非常关键的一个配置文件,若要让repmgr集群更好的做事您的数据库框架,须要理解repmgr参数和命令的语法和含义:

参数名称参数阐明配置哀求node_id大于零的唯一整数,用于标识节点必配项node_name任意(但唯一)字符串.字符串的最大长度为63个字符,并且只能包含可打印的ASCII字符。
必配项node_name数据库连接信息。
群集中的所有做事器必须能够利用此字符串连接到本地节点。
如果利用repmgrd,请考虑connect_timeout在conninfo字符串中进行显式设置,以确定放弃网络连接考试测验的韶光;必配项data_directoryPostgreSQL节点的数据目录必配项config_directory如果PostgreSQL配置文件位于数据目录之外,请指定主postgresql.conf文件所在的目录。
可选项replication_user与PostgreSQL用户建立复制连接。
如果未设置默认值,则为conninfo中定义的用户可选项replication_type必须为physical(默认值)可选项location定义节点位置的任意字符串;在故障转移期间利用它来检讨当前主节点的可见性可选项use_replication_slots是否利用物理复制插槽。
把稳:利用复制插槽时, max_replication_slots应至少配置为要连接到主数据库的备用数据库的数量可选项ssh_options建议添加-q以抑制任何多余的SSH信息,例如登录标语,以及一个显式 ConnectTimeout值,例如:ssh_options ='-q -o ConnectTimeout = 10'可选项log_levelDEBUG,INFO,NOTICE, WARNING,ERROR,ALERT,CRIT 或EMERG。
默认值为INFO。
须要把稳,这DEBUG将产生大量的日志输出,因此在正常利用中不应启用它。
可选项log_facility日志工具:可能的值是STDERR(默认),或Syslog集成,一LOCAL0,LOCAL1,LOCAL7,USER可选项log_file如果将log_facility设置为STDERR,则可以将日志输出重定向到指定的文件可选项log_status_interval此设置使repmgrd以指定的韶光间隔(以秒为单位,默认为300)发出状态日志行可选项

repmgr命令repmgr -f /etc/repmgr.conf node service --list-actions --action=stoprepmgr -f /etc/repmgr.conf node service --list-actions --action=startrepmgr -f /etc/repmgr.conf node service --list-actions --action=restartrepmgr -f /etc/repmgr.conf node service --list-actions --action=reload

5. 要使repmgr支持复制集群,必须向repmgr注册主节点。
这将安装repmgr扩展和元数据工具,并为主理事器添加元数据记录:

[postgres@lightdb1 etc]$ pwd/data/postgresql/etc[postgres@lightdb1 etc]$ repmgr primary registerINFO: connecting to primary database...NOTICE: attempting to install extension 'repmgr'NOTICE: 'repmgr' extension successfully installedNOTICE: primary node record (ID: 1) registered[postgres@lightdb1 etc]$ repmgr service status ID | Name | Role | Status | Upstream | repmgrd | PID | Paused? | Upstream last seen----+----------+---------+-----------+----------+-------------+-----+---------+-------------------- 1 | lightdb1 | primary | running | | not running | n/a | n/a | n/a psql -h 192.168.127.135 -U repmgr -d postgres -c 'select version()'psql -h 192.168.127.135 -U repmgr -d repmgr -c 'SELECT FROM repmgr.nodes;'[postgres@lightdb1 ~]$ psql -h 192.168.127.135 -U repmgr -d repmgr -c 'SELECT FROM repmgr.nodes;' node_id | upstream_node_id | active | node_name | type | location | priority | conninfo | repluser | slot_name | config_file ---------+------------------+--------+-----------+---------+----------+----------+----------------------------------------------------------------------------+----------+-----------+--------------- 1 | | t | lightdb1 | primary | default | 100 | host=192.168.127.135 port=5432 dbname=repmgr user=repmgr connect_timeout=2 | repmgr | | ./repmgr.conf(1 row)

6. 开启主库守护进程repmgrd

复制集群中的每个做事器都有自己的记录。
如果利用了repmgrd,则当节点的状态或角色发生变动时,将更新upstream_node_id、active和type字段。

[postgres@lightdb1 etc]$ repmgrd -d[2021-07-16 00:52:22] [NOTICE] redirecting logging output to '/data/postgresql/etc/repmgr.log'

克隆从库由于repmgr是一个客户端/做事器端PostgreSQL管理套件,在真正利用它之前,我们至少须要两个或两个以上节点,备库不须要初始化数据库。
如备库在搭建的过程中,须要停滞数据库或者删除data目录下面的文件。
通过复制主库,在备库创建克隆副本,并注册到repmgr中。
此处为了演示,在实际天生环境中,支配多个节点,数据丢失或系统中断的几率会降落,提高全体框架的高可用性。
下面操作须要在所有的备库repmgr2、repmgr3实行。

1) 配置repmgr.conf文件,对指定的参数进行以下变动:

sudo mkdir -pv /data/postgresql/etc/sudo chown -R postgres:postgres /data/postgresql/touch /data/postgresql/etc/repmgr.confcat >> /data/postgresql/etc/repmgr.conf << EOFnode_id=2node_name='lightdb2'conninfo='host=192.168.127.136 port=5432 dbname=repmgr user=repmgr connect_timeout=2'data_directory ='/data/postgresql/data'pg_bindir='/data/postgresql/bin'config_directory='/data/postgresql/data'log_level=INFOlog_facility=STDERRlog_file='/data/postgresql/etc/repmgr.log'EOF

2) 从库须要停滞数据数据做事,利用-dry-run选项检讨待机是否可以克隆.repmgr命令

[postgres@lightdb2 etc]$ repmgr -h 192.168.127.135 -U repmgr -d repmgr -f /data/postgresql/etc/repmgr.conf standby cloneWARNING: following problems with command line parameters detected: 'config_directory' set in repmgr.conf, but --copy-external-config-files not providedNOTICE: destination directory '/data/postgresql/data' providedINFO: connecting to source nodeDETAIL: connection string is: host=192.168.127.135 user=repmgr dbname=repmgrDETAIL: current installation size is 30 MBINFO: replication slot usage not requested; no replication slot will be set up for this standbyNOTICE: checking for available walsenders on the source node (2 required)NOTICE: checking replication connections can be made to the source server (2 required)INFO: creating directory '/data/postgresql/data'...NOTICE: starting backup (using pg_basebackup)...HINT: this may take some time; consider using the -c/--fast-checkpoint optionINFO: executing: /data/postgresql/bin/pg_basebackup -l 'repmgr base backup' -D /data/postgresql/data -h 192.168.127.135 -p 5432 -U repmgr -X stream NOTICE: standby clone (using pg_basebackup) completeNOTICE: you can now start your PostgreSQL serverHINT: for example: pg_ctl -D /data/postgresql/data startHINT: after starting the server, you need to register this standby with 'repmgr standby register'[postgres@lightdb2 etc]$ repmgr standby registerINFO: connecting to local node 'lightdb2' (ID: 2)INFO: connecting to primary databaseWARNING: --upstream-node-id not supplied, assuming upstream node is primary (node ID 1)INFO: standby registration completeNOTICE: standby node 'lightdb2' (ID: 2) successfully registered[postgres@lightdb2 etc]$

3) 利用以下命令克隆lightdb1节点作为postgres用户。
在lightdb2、lightdb3上实行此命令:

repmgr -h 192.168.127.135 -U repmgr -d repmgr -f /data/postgresql/etc/repmgr.conf standby clone

把稳:如果是要做级联复制,则须要在克隆时候添加--upstream-node-id=1参数,这个参数,它指定了数据流的来源

7. 启动新副本数据库

pg_ctl -D /data/postgresql/data -l logfile start

8. 注册rmha2为备节点:

repmgr standby register

9. 启动repmgrd守护进程

[postgres@lightdb2 ~]$ repmgrd -d[2021-07-18 19:23:18] [NOTICE] redirecting logging output to '/data/postgresql/etc/repmgr.log'

10. 查看从库repmgr状态

[postgres@lightdb2 ~]$ repmgr service status ID | Name | Role | Status | Upstream | repmgrd | PID | Paused? | Upstream last seen----+----------+---------+-----------+----------+---------+-------+---------+-------------------- 1 | lightdb1 | standby | running | lightdb3 | running | 40872 | no | 1 second(s) ago 2 | lightdb2 | standby | running | lightdb3 | running | 8665 | no | 1 second(s) ago 3 | lightdb3 | primary | running | | running | 2324 | no | n/a 4 | witness | witness | running | lightdb3 | running | 2387 | no | 0 second(s) ago

通过repmgr做事状态查看,主从三个节点目前状态均已正常在线。

配置见证做事见证节点紧张的事情是帮助备用数据库达到法定的数量。
大略来讲,备机连不上主机了,就会连接见证节点,如果也连接不上见证节点,那判断自己网络故障了,如果能连上见证节点,则认为主机故障,见证节点的浸染类似于一个信赖的网关。
所有见证节点安装在witness做事上,须要在此节点重新初始化数据。
按照以下步骤天生一个功能完好的repmgr证人:

1)利用initdb创建一个新的空的PostgreSQL实例

initdb -D /data/postgresql/data

2)修正postgresql.conf文件并设置如下参数:

shared_preload_libraries = 'repmgr'

3)修正pg_hba.conf文件:

拷贝主库pg_hba.conf即可

4)启动PostgreSQL做事

pg_ctl -D /data/postgresql/data -l logfile start

5)连接到PostgreSQL并实行以下操作来创建一个用户和数据库来管理repmgr:

create user repmgr with password 'repmgr' superuser replication;create database repmgr owner repmgr;

6)创建一个名为/data/postgresql/etc/repmgr.conf的文件,包含以下内容:

[root@lightdb4 ~]# more /data/postgresql/etc/repmgr.confnode_id=4node_name='witness'conninfo='host=192.168.127.138 port=5432 dbname=repmgr user=repmgr connect_timeout=2'data_directory ='/data/postgresql/data'pg_bindir='/data/postgresql/bin'config_directory='/data/postgresql/data'log_level=INFOlog_facility=STDERRlog_file='/data/postgresql/etc/repmgr.log'

7)利用以下命令将此节点注册为见证节点postgres用户:

[postgres@lightdb4 ~]$ repmgr witness register -h192.168.127.137 -drepmgr -UrepmgrINFO: connecting to witness node 'witness' (ID: 4)INFO: connecting to primary nodeNOTICE: attempting to install extension 'repmgr'NOTICE: 'repmgr' extension successfully installedINFO: witness registration completeNOTICE: witness node 'witness' (ID: 4) successfully registered

8)作为根用户,利用以下命令启用和重启repmgrd守护进程:

sudo systemctl enable repmgrdsudo systemctl restart repmgrd

9)验证眼见者可以利用以下命令作为postgres用户看到集群:

[postgres@lightdb4 ~]$ repmgr cluster show ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string ----+----------+---------+-----------+----------+----------+----------+----------+---------------------------------------------------------------------------- 1 | lightdb1 | primary | running | | default | 100 | 1 | host=192.168.127.135 port=5432 dbname=repmgr user=repmgr connect_timeout=2 2 | lightdb2 | standby | running | lightdb1 | default | 100 | 1 | host=192.168.127.136 port=5432 dbname=repmgr user=repmgr connect_timeout=2 3 | lightdb3 | standby | running | lightdb1 | default | 100 | 1 | host=192.168.127.137 port=5432 dbname=repmgr user=repmgr connect_timeout=2 4 | witness | witness | running | lightdb1 | default | 0 | n/a | host=192.168.127.138 port=5432 dbname=repmgr user=repmgr connect_timeout=2

Witness浸染:

常日,这对付担保在主节点不可用时建立投票法定人数是必要的。
必须选择一个现有的备用系统来提升和接管集群。
证人节点作为一个客不雅观的第三方,帮助保护我们免受这些情形。
如果它们与当前主理事器位于同一数据中央,那么网络分区将完备阻挡它进行投票,我们将受到网络分区的保护。
如果它位于阔别当前主节点或备用节点的第三数据中央,那么将有两个独立的路由来验证主节点实际上是否脱机或不可访问。
以此来防止集群涌现脑裂的情形。

三、做事管理

3.1 手动故障切换

当主理事器发生宕机或者做事不可用,就须要新的备做事器来接管故障的主理事器,以确保全体repmgr集群对外可用。
这时可以通过repmgr Standby Promotion来完成。

1) $repmgr cluster show

[postgres@lightdb4 ~]$ repmgr cluster show ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string ----+----------+---------+-----------+----------+----------+----------+----------+---------------------------------------------------------------------------- 1 | lightdb1 | primary | running | | default | 100 | 1 | host=192.168.127.135 port=5432 dbname=repmgr user=repmgr connect_timeout=2 2 | lightdb2 | standby | running | lightdb1 | default | 100 | 1 | host=192.168.127.136 port=5432 dbname=repmgr user=repmgr connect_timeout=2 3 | lightdb3 | standby | running | lightdb1 | default | 100 | 1 | host=192.168.127.137 port=5432 dbname=repmgr user=repmgr connect_timeout=2 4 | witness | witness | running | lightdb1 | default | 0 | n/a | host=192.168.127.138 port=5432 dbname=repmgr user=repmgr connect_timeout=2

仿照主库宕机,停滞主库数据库做事

2) $ pg_ctl -m fast stop

此时,复制群集将处于部分禁用状态,两个备用数据库都将考试测验连接到已停滞的主数据库时接管只读连接。
请把稳,repmgr元数据表尚未更新;实行repmgr cluster show将把稳差异:

3) $ repmgr cluster show

[postgres@lightdb4 ~]$ repmgr cluster show ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string ----+----------+---------+-----------+------------+----------+----------+----------+---------------------------------------------------------------------------- 1 | lightdb1 | standby | running | lightdb3 | default | 100 | 21 | host=192.168.127.135 port=5432 dbname=repmgr user=repmgr connect_timeout=2 2 | lightdb2 | standby | - failed | ? lightdb3 | default | 100 | | host=192.168.127.136 port=5432 dbname=repmgr user=repmgr connect_timeout=2 3 | lightdb3 | primary | running | | default | 100 | 21 | host=192.168.127.137 port=5432 dbname=repmgr user=repmgr connect_timeout=2 4 | witness | witness | running | lightdb3 | default | 0 | n/a | host=192.168.127.138 port=5432 dbname=repmgr user=repmgr connect_timeout=2WARNING: following issues were detected - unable to connect to node 'lightdb2' (ID: 2)HINT: execute with --verbose option to see connection error messages[postgres@lightdb4 ~]$

4) 现在利用以下方法在lightdb2升级第一个备用数据库:

[postgres@lightdb2 ~]$ repmgr standby promoteWARNING: 2 sibling nodes found, but option '--siblings-follow' not specifiedDETAIL: these nodes will remain attached to the current primary: lightdb3 (node ID: 3) witness (node ID: 4, witness server)NOTICE: promoting standby to primaryDETAIL: promoting server 'lightdb2' (ID: 2) using pg_promote()NOTICE: waiting up to 60 seconds (parameter 'promote_check_timeout') for promotion to completeNOTICE: STANDBY PROMOTE successfulDETAIL: server 'lightdb2' (ID: 2) was successfully promoted to primary

5) 实行repmgr集群显示将显示当前状态

repmgr -f /etc/repmgr.conf cluster show

通过查看集群状态,主库不可用的集群告警已自动打消,由于原备库rmha2已成功接管了主库的做事。
仅仅接管,对付repmgr全体集群的可靠性还不足。
还须要实行repmgr Standby Follow,使未跟随新主理事的备库重新加入到新的集群中来。

6) 在lightdb2上,实行跟随新主理事的命令:

repmgr standby follow

7) 重新加入原主理事到新集群

repmgr node rejoin -h 192.168.127.135 -U repmgr -d repmgr --force-rewind

8) 检讨被用数据库跟随新主理事的新集群的状态:

repmgr cluster show

此时再来查看集群状态,lightdb2已变主库,原主库已重新加入到集群中并注册为备库

3.2 自动故障转移

1) 定制failover参数文件,利用repmgrd进程实现自动的failover,前面的操作同等,修正repmgr.conf文件,添加故障切换干系参数。
主从节点均添加。

failover='automatic'promote_command='/data/postgresql/bin/repmgr standby promote -f /data/postgresql/etc/repmgr.conf'follow_command='/data/postgresql/bin/repmgr standby follow -f /data/postgresql/etc/repmgr.conf --upstream-node-id=%n'monitoring_history=true (启用监控参数) monitor_interval_secs=2 (定义监视数据间隔写入韶光参数)connection_check_type='ping'reconnect_attempts=3 (故障转移之前,考试测验重新连接主库次数(默认为6)参数)reconnect_interval=5 reconnect_interval=5standby_disconnect_on_failover =true

参数阐明:

promote_command、follow_command提升备库切换为主库monitoring_history-启动监控参数monitor_interval_secs-定义监视数据间隔写入韶光参数reconnect_attempts-故障切换之前,设置10s内自动连接,超过10s超reconnect_interval-每间隔5s考试测验重连一次

2) 重启repmgr主库做事,使配置生效

[postgres@node1 ~]$ repmgr node service --action=restart1. DETAIL: executing server command 'pg_ctl -w -D '/data/postgresql/data ' restart'

3) 主备库启动repmgrd

[postgres@lighdtdb1 ~]$ repmgrd –f /data/postgresql/etc/repmgr.conf --pid-file /tmp/repmgrd.pid

4) 仿照主库故障

[postgres@lighdtdb2 ~]$ pg_ctl stopwaiting for server to shut down..... doneserver stopped

5) 查看备库日志,创造lightdb1已经升为主库:/data/postgresql/etc/repmgr.log

6) 查看cluster状态,备库已经升主

[postgres@lightdb3 ~]$ repmgr service status ID | Name | Role | Status | Upstream | repmgrd | PID | Paused? | Upstream last seen----+----------+---------+-----------+----------+---------+-------+---------+-------------------- 1 | lightdb1 | primary | running | | running | 7325 | no | n/a 2 | lightdb2 | standby | running | lightdb1 | running | 23929 | no | 2 second(s) ago 3 | lightdb3 | standby | running | lightdb1 | running | 31001 | no | 1 second(s) ago 4 | witness | witness | running | lightdb1 | running | 24790 | no | 1 second(s) ago

四、常用管理命令

repmgr primary register 安装pg的repmgr扩展并注册为主节点repmgr primary unregister 注销不活动的主节点repmgr standby clone 从其他节点复制数据到从节点repmgr standby register 注册从节点(添加从的信息到repmgr元数据)repmgr standby unregister repmgr 元数据中移除从的信息repmgr standby promote 将从提升为主repmgr standby follow 将从跟随新主repmgr standby switchover 将从提升为主并将主降级为从repmgr witness register 注册一个不雅观察节点repmgr witness unregister 移除一个不雅观察节点repmgr node status 显示节点的基本信息和复制状态repmgr node check 从复制的角度对节点进行康健监测repmgr node rejoin 重新加入一个失落效节点到集群repmgr cluster show 显示所有集群中注册的节点信息repmgr cluster matrix 在所有节点运行show并汇总repmgr cluster crosscheck 在节点间两两交叉监测连接repmgr cluster event 输出韶光记录repmgr cluster cleanup 清理监控历史repmgr service status 节点状态

相关文章

昼伏夜出,提示生物钟背后的奇妙世界

在浩瀚的自然界中,生物们遵循着各自独特的生存法则,其中,“昼伏夜出”便是众多生物共有的生存智慧。这一生物习性,不仅揭示了生物钟的神...

SEO优化 2024-12-27 阅读0 评论0

智慧IT,引领未来,赋能企业发展的新引擎

随着信息技术的飞速发展,智慧IT已经成为推动企业发展的新引擎。智慧IT公司凭借其先进的技术、丰富的经验和专业的团队,为企业提供全方...

SEO优化 2024-12-27 阅读0 评论0

协议_数字经济时代的关键纽带

随着互联网技术的飞速发展,数字经济已成为全球经济的新引擎。在这个时代,协议作为一种重要的规则和规范,发挥着至关重要的作用。本文将围...

SEO优化 2024-12-27 阅读0 评论0