ToB企服应用市场:ToB评测及商务社交产业平台

标题: 如何搭建PostgreSQL高可用方案repmgr?详解安装部署与关键组件疑问 [打印本页]

作者: 立聪堂德州十三局店    时间: 2024-9-11 10:10
标题: 如何搭建PostgreSQL高可用方案repmgr?详解安装部署与关键组件疑问
Hi~这里是ProXiao


一、repmgr概述

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


二、组件说明

repmgr 包罗两个主要的组件:




三、安装部署

1、情况

留意要点

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已安装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
复制代码

  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
复制代码

  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
复制代码

  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;
复制代码

  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从的集群,但仍旧遗留了部分事项:

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

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




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4