如何搭建PostgreSQL高可用方案repmgr?详解安装部署与关键组件疑问 ...

打印 上一主题 下一主题

主题 510|帖子 510|积分 1530

Hi~这里是ProXiao


一、repmgr概述

repmgr:是一个用于增强和管理PostgreSQL数据库内建复制和故障转移机制的开源工具集。其主要功能包罗:设置备用服务器、监控复制状态、以及在故障发生时主动或手动实行故障转移和切换操作。


  • Replication cluster: 在repmgr中,“replication cluster”是指一组通过流式复制技能连接在一起的PostgreSQL服务器。这些服务器之间复制数据,从而确保数据一致性和高可用性。
  • Node: 在复制集群中,”node”表示单个的PostgreSQL服务器实例。每个节点可以扮演主节点或备用节点的脚色。
  • Upstream node: 在备用服务器的上下文中,”upstream node”是指该备用服务器正在吸收复制数据的节点。这通常指的是主节点,但在级联复制中也大概是另一台备用节点。
  • Failover: “Failover”操作发生在主节点失效时,一个选定的备用节点被提升为新的主节点。`repmgrd`守护进程可以设置为支持主动故障转移,以只管减少服务中断的时间。
  • Switchover: “Switchover”是一种受控的操作,用于主动将主节点的脚色切换到一台备用节点上。与故障转移不同,切换是在没有主节点失效的情况下出于主动维护等缘故原由举行的。
  • Fencing: 在举行故障转移后,为了防止原主节点不期望地重新参加集群并造成数据辩论(称为脑裂情况),必须实施”fencing”策略。Fencing能确保原主节点与集群的别的部分保持隔离。
  • Witness server: repmgr支持设置一个”Witness server”,其不到场数据复制,但包含关于集群状态的元数据。它的作用是在故障转移时资助确定谁是最适合成为新主节点的备用服务器。Witness server能提供额外的信息来协助做出正确选择,从而确保集群的一致性和稳固性。

二、组件说明

repmgr 包罗两个主要的组件:



  • repmgr:这是一个命令行工具,用于实行多种管理任务,比如:

    • 设置和启动备用服务器
    • 将备用服务器提升为新的主服务器
    • 在主服务器和备用服务器之间举行切换
    • 显示复制集群中各服务器的状态



  • repmgrd:这是一个守护进程,其主动监控复制集群并实行如下任务:

    • 监控复制性能并记载相干数据
    • 通过检测到主服务器的故障并主动提升最合适的备用服务器来实施故障转移
    • 向用户定义的脚本发送集群中事故的通知,这些脚本可以用来实行任务,比如发送电子邮件警报等


三、安装部署

1、情况

留意要点


  • 不支持在win上部署
  • 同一套集群PG版本统一
  • repmgr安装统一版本而且集群中所有节点都必须安装
repmgr+pg版本对应关系(版本关系可以在github检察或Document检察)
repmgr版本PG对应版本
repmgr 5.49.4, 9.5, 9.6, 10, 11, 12, 13, 15,16
repmgr 5.39.4, 9.5, 9.6, 10, 11, 12, 13, 14, 15
repmgr 5.29.4, 9.5, 9.6, 10, 11, 12, 13
repmgr 5.19.3, 9.4, 9.5, 9.6, 10, 11, 12
repmgr 5.09.3, 9.4, 9.5, 9.6, 10, 11, 12
repmgr 4.x9.3, 9.4, 9.5, 9.6, 10, 11
repmgr 3.x9.3, 9.4, 9.5, 9.6
repmgr 2.x9.0, 9.1, 9.2, 9.3, 9.4
2、安装:



  • 修改postgresql参数
注:postgresql已安装ok(不认识的可以yum一次性解决)
  1. #修改pg配置postgresql.conf
  2. listen_addresses = '*'
  3. wal_level = logical
  4. wal_log_hints = on
  5. #重启pg
  6. systemctl restart postgresql-15
复制代码


  • 创建repmgr所需的库和账号
  1. #创建repmgr账号和库
  2. create user repmgr with superuser password 'repmgr123';
  3. create database repmgr owner  repmgr;
  4. 配置认证pg_hba.conf
  5. # 允许用户 repmgr 通过local,127.0.0.1,10.248.32. 连接到replication
  6. local   replication   repmgr                              trust
  7. host    replication   repmgr      10.248.32.187/24        trust
  8. host    replication   repmgr      10.248.32.188/24        trust
  9. # 允许用户 repmgr 通过local,127.0.0.1,10.248.32. 连接到repmgr schema
  10. local   repmgr   repmgr                              trust
  11. host    repmgr   repmgr      10.248.32.187/24        trust
  12. host    repmgr   repmgr      10.248.32.188/24        trust
  13. #重启pg
  14. systemctl reload postgresql-15
复制代码


  • repmgr节点间免密设置以及pg连接免密
  1. #选择任意节点创建密钥对(一路回车什么都不输入)
  2. ssh-keygen -t rsa -b
  3. Generating public/private rsa key pair.
  4. Enter file in which to save the key (/var/lib/postgresql/.ssh/id_rsa):
  5. Created directory '/var/lib/postgresql/.ssh'.
  6. Enter passphrase (empty for no passphrase):
  7. Enter same passphrase again:
  8. Your identification has been saved in /var/lib/postgresql/.ssh/id_rsa.
  9. Your public key has been saved in /var/lib/postgresql/.ssh/id_rsa.pub.
  10. The key fingerprint is:
  11. SHA256:fokF65XAW82Z8xI1SJuPlmCKnEuchkj6uder8nVp+c4 postgres@cda1-032187-test-tb-postgresql-goodscenter
  12. The key's randomart image is:
  13. +---[RSA 4096]----+
  14. |           ...o  |
  15. |       .   o.* . |
  16. |  .     + + X    |
  17. | o . + + O o B   |
  18. |. . . O S + = o  |
  19. | . . o + * o .   |
  20. |  o  .o O o      |
  21. |  .....o +       |
  22. |  .+o... .E      |
  23. +----[SHA256]-----+
  24. cat /var/lib/postgresql/.ssh/id_rsa.pub >/var/lib/postgresql/.ssh/authorized_keys
  25. #将密钥信息
  26. scp -r /var/lib/postgresql/.ssh/ root@other-ip:/var/lib/postgresql/
  27. -- other节点执行权限变更
  28. chmod 0700 /var/lib/postgresql/.ssh/
  29. chmod 0600 /var/lib/postgresql/.ssh/*
  30. chown postgres:postgres /var/lib/postgresql/.ssh/ -R
  31. #所有节点配置pgpass
  32. ip1:5432:repmgr:repmgr:repmgr123
  33. ip2:5432:repmgr:repmgr:repmgr123
  34. chmod 0600 .pgpass
复制代码


  • 添加Primary节点
  1. -- 注册primary节点(IP1)
  2. cat /etc/repmgr.conf
  3. node_id=****
  4. node_name='****'
  5. conninfo='host=**** port=**** user=**** dbname=**** connect_timeout=****'
  6. data_directory='/pgdata/'
  7. ssh_options='-q -o ConnectTimeout=10'
  8. -- 修改权限
  9. chown postgres:postgres /etc/repmgr.conf
  10. -- 注入primary node
  11. su - postgres
  12. repmgr -f /etc/repmgr.conf primary register
  13. INFO: connecting to primary database...
  14. NOTICE: attempting to install extension "repmgr"
  15. NOTICE: "repmgr" extension successfully installed
  16. NOTICE: primary node record (ID: 1) registered
  17. -- 验证集群
  18. repmgr -f /etc/repmgr.conf cluster show
  19. ID| Name          | Role    | Status    | Upstream | Location | Priority | Timeline | Connection string
  20. ----+---------------+---------+-----------+----------+----------+----------+----------+--------------------------------------------------------------------------
  21. **** | **** | primary | * running |          | default  | 100      | 1        | host=**** port=**** user=**** dbname=**** connect_timeout=****
  22. -- 元数据表中的记录
  23. repmgr=# select * from nodes;
  24. -[ RECORD 1 ]----+-------------------------------------------------------------------------
  25. node_id          | ****
  26. upstream_node_id |
  27. active           | t
  28. node_name        | ****
  29. type             | primary
  30. location         | default
  31. priority         | 100
  32. conninfo         | host=**** port=**** user=**** dbname=**** connect_timeout=****
  33. slot_name        |
  34. config_file      | /etc/repmgr.conf
  35. #在pg1写入测试数据
  36. psql -c "create database demo01;"
  37. pgbench -i -s 20 -d demo01;
复制代码


  • 添加standby节点
  1. -- 注册standby节点(IP2)
  2. cat /etc/repmgr.conf
  3. node_id=****
  4. node_name='****'
  5. conninfo='host=**** port=**** user=**** dbname=**** connect_timeout=****'
  6. data_directory='/pgdata/'
  7. ssh_options='-q -o ConnectTimeout=10'
  8. -- 修改权限
  9. chown postgres:postgres /etc/repmgr.conf
  10. -- 使用参数--dry-run 检查是否可以克隆从库
  11. 主要检查如下几点:
  12.   检查目录
  13.   检查参数 max_wal_senders 是否大于2
  14.   检查参数 wal_log_hints
  15.   检查通过会执行备份命令 pg_basebackup -l "repmgr base backup"
  16. systemctl stop postgresql-16
  17. -- 停止pg才能执行如下步骤(如果当前实例pgdata目录不为空,则加上--force参数)
  18. repmgr -h ip -U user -d database -f /etc/repmgr.conf standby clone --dry-run
  19. NOTICE: destination directory "/pgdata" provided
  20. INFO: connecting to source node
  21. DETAIL: connection string is: host=**** port=**** user=**** dbname=****
  22. DETAIL: current installation size is 337 MB
  23. INFO: replication slot usage not requested;  no replication slot will be set up for this standby
  24. NOTICE: checking for available walsenders on the source node (2 required)
  25. NOTICE: checking replication connections can be made to the source server (2 required)
  26. INFO: checking and correcting permissions on existing directory "/pgdata"
  27. NOTICE: starting backup (using pg_basebackup)...
  28. HINT: this may take some time; consider using the -c/--fast-checkpoint option
  29. INFO: executing:
  30.   pg_basebackup -l "repmgr base backup"  -D /pgdata -h ip -p port -U user -X stream
  31. NOTICE: standby clone (using pg_basebackup) complete
  32. NOTICE: you can now start your PostgreSQL server
  33. HINT: for example: pg_ctl -D /pgdata start
  34. HINT: after starting the server, you need to register this standby with "repmgr standby register"
  35. -- 启动从库
  36. systemctl start postgresql-16
  37. --注册从节点
  38. repmgr -f /etc/repmgr.conf standby register
  39. INFO: connecting to local node "ip2" (ID: 2)
  40. INFO: connecting to primary database
  41. WARNING: --upstream-node-id not supplied, assuming upstream node is primary (node ID: 1)
  42. INFO: standby registration complete
  43. NOTICE: standby node "ip2" (ID: 2) successfully registered
  44. -- 查看集群信息
  45. repmgr -f /etc/repmgr.conf cluster show
  46. ID | Name          | Role    | Status    | Upstream      | Location | Priority | Timeline | Connection string
  47. ----+---------------+---------+-----------+---------------+----------+----------+----------+--------------------------------------------------------------------------
  48. 1  | ip1 | primary | * running |               | default  | 100      | 1        | host=**** port=**** user=**** dbname=**** connect_timeout=****
  49. 2  | ip2 | standby |   running |               | default  | 100      | 1        | host=**** port=**** user=**** dbname=**** connect_timeout=****
  50. -- 主从切换
  51. repmgr -f /etc/repmgr.conf standby switchover
  52. repmgr -f /etc/repmgr.conf cluster show
  53. ID | Name          | Role    | Status    | Upstream | Location | Priority | Timeline | Connection string
  54. ----+---------------+---------+-----------+----------+----------+----------+----------+--------------------------------------------------------------------------
  55. 1  | ip1 | standby |   running |          | default  | 100      | 3        | host=**** port=**** user=**** dbname=**** connect_timeout=****
  56. 2  | ip2 | primary | * running |          | default  | 100      | 4        | host=**** port=**** user=**** dbname=**** connect_timeout=****
复制代码
四、总结

通过上述操作后,repmgr即可管理一套postgresql 1主1从的集群,但仍旧遗留了部分事项:

  • 没有实现failover主动感知pg节点异常举行主从切换
  • 节点扩容、节点缩容、节点更换等步骤
  • 主从入口分离【因为是云服务器,思量是联合slb来实现,而不是pgpool来复杂化】

更多有关智能化前沿洞察资讯在这儿~
欢迎留言关注ProXiao

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

立聪堂德州十三局店

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表