Postgresql16 Etcd + Patroni 部署 PG 高可用集群
文章目录
PG 高可用集群部署
说明
规划
- 操作系统使用 CentOS 7.9
- 测试服务器IP及用户如下:
hostanme | IP | 用户 | 用户目录 | 部署服务 |
---|---|---|---|---|
c1 | 192.168.212.81 | postgres | /app/postgres | etcd+pg16+Patroni |
c2 | 192.168.212.82 | postgres | /app/postgres | etcd+pg16+Patroni |
c3 | 192.168.212.83 | postgres | /app/postgres | etcd+pg16+Patroni |
前期环境准备
创建 postgres 用户
- c1/c2/c3 三个节点均执行
|
|
部署 ETCD
- git:https://github.com/etcd-io/etcd
安装
- 三个节点均执行,使用 postgres 用户
- 下载
|
|
- 安装
|
|
更改配置文件
创建用到的目录
- 三个节点均执行
|
|
c1 更改配置文件
|
|
c2 更改配置文件
|
|
c3 更改配置文件
|
|
启动
- 三个节点均执行
|
|
- 查看状态
|
|
部署一主两从流复制
安装 postgresql
- 三个节点均需操作
yum 安装依赖的包
|
|
配置目录及变量
- 创建安装目录
|
|
- 配置环境变量
|
|
编译安装
- 下载
|
|
- 编译安装
|
|
主节点初始化pg、更改配置
- 注意:仅主节点执行
初始化数据库
|
|
更改配置文件
- $PGDATA/postgresql.conf
|
|
- $PGDATA/pg_hba.conf
|
|
启动主库
|
|
- 创建复制槽
|
|
构建流复制
拉取数据
|
|
配置 db2
- 更改 postgresql.auto.conf
- 增加 application_name ,针对 synchronous_standby_names 要配置 ANY 时生效
- primary_slot_name 指定指定的复制槽名:slot_db02
|
|
配置 db3
- 更改 postgresql.auto.conf
- 增加 application_name ,针对 synchronous_standby_names 要配置 ANY 时生效
- primary_slot_name 指定指定的复制槽名:slot_db03
|
|
启动从库,检查从库状态
- 两个从库执行
|
|
-
查看主从状态
- 主库
1 2
# 连接数据库 psql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
-- 开启换行显示 postgres=# \x Expanded display is on. -- 查看从库节点 postgres=# select * from pg_stat_replication; -[ RECORD 1 ]----+------------------------------ pid | 1568 usesysid | 16388 usename | repl application_name | db03 client_addr | 192.168.212.83 client_hostname | client_port | 34130 backend_start | 2025-02-10 10:41:46.419486+08 backend_xmin | state | streaming sent_lsn | 0/D000148 write_lsn | 0/D000148 flush_lsn | 0/D000148 replay_lsn | 0/D000148 write_lag | flush_lag | replay_lag | sync_priority | 0 sync_state | async reply_time | 2025-02-10 10:49:18.760612+08 -[ RECORD 2 ]----+------------------------------ pid | 1567 usesysid | 16388 usename | repl application_name | db02 client_addr | 192.168.212.82 client_hostname | client_port | 55766 backend_start | 2025-02-10 10:41:41.667841+08 backend_xmin | state | streaming sent_lsn | 0/D000148 write_lsn | 0/D000148 flush_lsn | 0/D000148 replay_lsn | 0/D000148 write_lag | flush_lag | replay_lag | sync_priority | 0 sync_state | async reply_time | 2025-02-10 10:49:18.7555+08 -- 主库查看备库落后WAL的字节数 postgres=# select pg_wal_lsn_diff(pg_current_wal_lsn(),replay_lsn) from pg_stat_replication; -[ RECORD 1 ]---+-- pg_wal_lsn_diff | 0 -[ RECORD 2 ]---+-- pg_wal_lsn_diff | 0 -- 查看复制槽状态 -[ RECORD 1 ]-------+---------- slot_name | slot_db02 plugin | slot_type | physical datoid | database | temporary | f active | t active_pid | 1567 xmin | catalog_xmin | restart_lsn | 0/D000148 confirmed_flush_lsn | wal_status | reserved safe_wal_size | two_phase | f conflicting | -[ RECORD 2 ]-------+---------- slot_name | slot_db03 plugin | slot_type | physical datoid | database | temporary | f active | t active_pid | 1568 xmin | catalog_xmin | restart_lsn | 0/D000148 confirmed_flush_lsn | wal_status | reserved safe_wal_size | two_phase | f conflicting |
- 从库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
-- t 表示为是从库 postgres=# select pg_is_in_recovery(); pg_is_in_recovery ------------------- t -- 查看 WAL 日志接收情况 postgres=# select * from pg_stat_wal_receiver; -[ RECORD 1 ]---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ pid | 2601 status | streaming receive_start_lsn | 0/D000000 receive_start_tli | 1 written_lsn | 0/E000000 flushed_lsn | 0/E000000 received_tli | 1 last_msg_send_time | 2025-02-10 11:12:42.983258+08 last_msg_receipt_time | 2025-02-10 11:12:42.988852+08 latest_end_lsn | 0/E000000 latest_end_time | 2025-02-10 11:09:05.679155+08 slot_name | slot_db02 sender_host | 192.168.212.81 sender_port | 5432 conninfo | user=repl password=******** channel_binding=disable dbname=replication host=192.168.212.81 port=5432 application_name=slot_db02 fallback_application_name=PGCluster sslmode=disable sslcompression=0 sslcertmode=disable sslsni=1 ssl_min_protocol_version=TLSv1.2 gssencmode=disable krbsrvname=postgres gssdelegation=0 target_session_attrs=any load_balance_hosts=disable
更改主库,设置任意数据库接收后即提交
- 更改主库,设置两个主库任意一个库接收数据后才提交
|
|
编译安装 python 3.7
- 三个节点均执行
- root/sudo 用户安装编译 Python 依赖的包
|
|
- 包下载地址:https://www.python.org/downloads/
- 下载包
|
|
- 编译
|
|
配置 Patroni
安装 Patroni
-
三个节点均执行
-
创建 Patroni 用的 python 虚拟目录
|
|
- 安装 Patroni
|
|
更改配置文件
- 每个节点单独操作
c1 节点
|
|
|
|
- postgres-pg01.yml 增加如下配置
|
|
c2 节点
|
|
|
|
- postgres-pg02.yml 增加如下配置
|
|
c3 节点
|
|
|
|
- postgres-pg02.yml 增加如下配置
|
|
启动
- 每个节点单独操作
- c1
|
|
- c2
|
|
- c3
|
|
问题处理
patroni 命令由于 urllib3 报错
- 报错信息如下:
|
|
- 解决方案:降级 urllib 版本
- 可以查看对应版本 patroni 的 requirements.txt 查看推荐的版本
|
|
参考
- https://developer.aliyun.com/article/1346766?spm=a2c6h.12873639.article-detail.4.4c1b49b5Wna0xc
- https://zhuanlan.zhihu.com/p/150690831
- https://developer.aliyun.com/article/1355923?spm=a2c6h.13262185.profile.44.51f096518e9T3a
- https://www.modb.pro/db/1862019398018543616
- https://www.cndba.cn/dave/article/116393
文章作者 Xiang
上次更新 2024-12-12