《童虎学习笔记》半小时精通PostgreSQL高可用方案repmgr

本文章配套视频 https://www.ixigua.com/7077056019024904717?id=7074028198836568583
本专栏全部文章 https://blog.csdn.net/tonghu_note/category_11713514.html
总目录 https://blog.csdn.net/tonghu_note/article/details/124333034

我的dou音 aa10246666, 看配套视频


一、部署场景

主库(node1) PG 14.2 10.211.55.9
从库1(node2) PG 14.2 10.211.55.4
从库2(node3) PG 14.2 10.211.55.6
repmgr 5.3.1

二、安装repmgr

1、主从库都做如下操作

apt-get install -y postgresql-14-repmgr --因PG版本是14,所以下载与PG匹配的版本

apt-get install -y rsync

/etc/hosts 在结尾新增

10.211.55.9 node1

10.211.55.4 node2

10.211.55.6 node3

visudo 在结尾新增

postgres ALL = NOPASSWD: systemctl stop postgresql, systemctl start postgresql, systemctl restart postgresql, systemctl status postgresql, systemctl reload postgresql

在root用户下设置postgres用户的密码为 postgres

在postgres用户下配置ssh免密,三台之前都需要免密,包括自已,即如下操作在所有节点都需要做

ssh-keygen -t rsa

ssh-copy-id [email protected]

ssh-copy-id [email protected]

ssh-copy-id [email protected]

ssh-copy-id postgres@node1

ssh-copy-id postgres@node2

ssh-copy-id postgres@node3

2、仅在主库做如下操作

/etc/postgresql/14/main/postgresql.conf 配置文件含以下内容

listen_addresses = '*'

max_wal_senders = 10

max_replication_slots = 10

wal_level = hot_standby

hot_standby = on

archive_mode = always    -- 从库需要设置为 always, 但为了避免failover后需要再次处理,所以主从都设为 always 比较好

archive_command = '/bin/true'

shared_preload_libraries = 'repmgr'

/etc/postgresql/14/main/pg_hba.conf 配置文件含以下内容

local all postgres peer

local replication repmgr trust

host    replication  repmgr      127.0.0.1/32            trust

host    replication  repmgr      10.211.55.0/24          trust

local  repmgr        repmgr                              trust

host    repmgr        repmgr      127.0.0.1/32            trust

host    repmgr        repmgr      10.211.55.0/24          trust

重启pg服务

/etc/repmgr.conf 配置文件含以下内容

node_id=1

node_name='node1'

conninfo='host=node1 user=repmgr dbname=repmgr connect_timeout=2'

data_directory='/var/lib/postgresql/14/main'

failover=automatic

promote_command='/usr/lib/postgresql/14/bin/repmgr standby promote -f /etc/repmgr.conf --log-to-file'

follow_command='/usr/lib/postgresql/14/bin/repmgr standby follow -f /etc/repmgr.conf --log-to-file --upstream-node-id=%n'

service_start_command  = 'sudo systemctl start postgresql'

service_stop_command    = 'sudo systemctl stop postgresql'

service_restart_command = 'sudo systemctl restart postgresql'

service_reload_command  = 'sudo systemctl reload postgresql'

repmgrd_pid_file='/tmp/repmgrd.pid'

log_file='/tmp/repmgrd.log'

priority=100

切到postgres用户下,新创建用户及数据库

createuser -s repmgr

createdb repmgr -O repmgr

切到postgres用户下集群注册主节点

repmgr -f /etc/repmgr.conf primary register

3、仅从库,以node2为例,node3与node2是相同的

数据目录需要为空,例如/var/lib/postgresql/14/main,如果创建了可以保留目录结构,但要清空里面的文件

测试可以连接到主库

psql 'host=node1 user=repmgr dbname=repmgr connect_timeout=2'

/etc/repmgr.conf 配置文件含以下内容

node_id=2

node_name='node2'

conninfo='host=node2 user=repmgr dbname=repmgr connect_timeout=2'

data_directory='/var/lib/postgresql/14/main'

failover=automatic

promote_command='/usr/lib/postgresql/14/bin/repmgr standby promote -f /etc/repmgr.conf --log-to-file'

follow_command='/usr/lib/postgresql/14/bin/repmgr standby follow -f /etc/repmgr.conf --log-to-file --upstream-node-id=%n'

service_start_command  = 'sudo systemctl start postgresql'

service_stop_command    = 'sudo systemctl stop postgresql'

service_restart_command = 'sudo systemctl restart postgresql'

service_reload_command  = 'sudo systemctl reload postgresql'

repmgrd_pid_file='/tmp/repmgrd.pid'

log_file='/tmp/repmgrd.log'

priority=100

运行dru-run命令以判断从库可以clone一份主库的数据

repmgr -h node1 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --dry-run

以上检测没问题后再实际执行

repmgr -h node1 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone

将主库/etc/postgresql/14/main下的postgresql.conf和pg_hba.conf考贝到从库,并启动pg

systemctl start postgresql

以standby身份注册集群

repmgr -f /etc/repmgr.conf standby register

检查集群状态, 应该加进来了一个从库

repmgr -f /etc/repmgr.conf cluster show

\c repmgr

SELECT * FROM repmgr.nodes;

相关事件查看

\c repmgr

SELECT * from repmgr.events;

用相同的方法处理node3

4、主从,启动repmgrd

repmgrd -f /etc/repmgr.conf

停止直接 killall repmgrd 即可

重载配置文件 kill -HUP `cat /tmp/repmgrd.pid`

检查本地节点状态 repmgr node check --repmgrd


三、管理

检查集群状态

node相关

repmgr -f /etc/repmgr.conf node status

repmgr -f /etc/repmgr.conf node check

cluster相关

repmgr -f /etc/repmgr.conf cluster show

repmgr -f /etc/repmgr.conf cluster crosscheck

service相关

repmgr -f /etc/repmgr.conf service status

库里的表也可以查状态, 进repmgr库里检查集群状态

SELECT * FROM repmgr.nodes;

暂停repmgrd,可以在任何一个节点上执行,可以用于例行维护,避免主库正常关闭,集群被切换

repmgr -f /etc/repmgr.conf service pause

解除暂停为

repmgr -f /etc/repmgr.conf service unpause

集群新增一个standby节点

相应节点做好配置后,在此节点为上执行

repmgr -f /etc/repmgr.conf standby register

集群删除一个standby节点执行

repmgr standby unregister -f /etc/repmgr.conf --node-id=3

集群删除一个primary节点

相应节点做好配置后,在此节点为上执行

repmgr -f /etc/repmgr.conf primary unregister  --node-id=1


四、故障处理

当主节点故障后系统会选举出一个新主节点,然后如果故障的主节点在未修复的情况下再次启动的话是这个状态 ! running

postgres@node2:~$ repmgr -f /etc/repmgr.conf service status

ID | Name      | Role    | Status    | Upstream  | repmgrd | PID  | Paused? | Upstream last seen

----+------------+---------+-----------+------------+---------+-------+---------+--------------------

1  | node1 | primary | ! running |            | running | 48488 | no      | n/a               

2  | node2 | primary | * running |            | running | 18529 | no      | n/a               

3  | node3 | standby |  running | node2 | running | 14483 | no      | 0 second(s) ago   

我们要将1节点移除,可以在2或3上用命令,如果卸载不掉可以在结尾加上 --force 强制卸载

repmgr primary unregister --node-id 1

如果是坏了的standby节点修复后,可以再加回来


五、相关参数

log_status_interval 

支持重载,检查repmgrd是否正常, 默认是300秒,会在日志中打印类似的信息 [2018-07-12 00:47:32] [INFO] monitoring connection to upstream node "node1" (ID: 1)

monitor_interval_secs

支持重载,检查主库节点的状态的间隔时间,默认是2秒

connection_check_type 

探测主库是否可用, 1. ping(默认)  2.connection 仅建立连接 3.query发起一个select查询

reconnect_attempts 

当主库连不上的时侯,再次尝试连接的次数,默认6次

reconnect_interval 

当主库连不上的时侯,二次连接的时间间隔,默认10秒

六、作业

repmgr实现的是服务端HA,  类似于MySQL的原生MGR,思考一下如何实现客户端的HA呢?

你可能感兴趣的:(postgresql,debian,数据库)