简介:通过bitnami/postgresql-repmgr镜像快速实现PostgreSQL主从复制与故障自动切换的HA方案
PostgreSQL作为企业级关系型数据库,其高可用性(HA)架构是保障业务连续性的关键。本文详细介绍如何利用Bitnami官方提供的bitnami/postgresql-repmgr镜像,在Kubernetes或Docker环境中快速部署具备自动故障转移功能的PostgreSQL集群。通过repmgr工具实现主从复制管理,结合镜像内置的监控脚本,可构建出生产级可用的HA解决方案,显著降低运维复杂度。
传统单节点部署存在单点故障风险,而手动搭建主从复制架构需要处理:
Bitnami提供的postgresql-repmgr镜像将PostgreSQL与repmgr工具深度集成:
repmgr是专门为PostgreSQL设计的复制管理工具,核心功能包括:
其工作机制通过监控主节点WAL日志位置,当检测到主节点失效时,自动从候选从节点中选举新主节点,并重新配置其他从节点指向新主。
Bitnami镜像采用分层设计:
/opt/bitnami/├── postgresql/ # PostgreSQL核心文件│ ├── data/ # 数据目录(需持久化)│ └── conf/ # 配置文件├── scripts/ # 启动脚本│ └── postgresql-entrypoint.sh└── repmgr/ # repmgr相关文件├── repmgr.conf # repmgr配置└── utils/ # 辅助脚本
创建docker-compose.yml示例:
version: '3.8'services:primary:image: bitnami/postgresql-repmgr:latestenvironment:- POSTGRESQL_POSTGRES_PASSWORD=primary_pass- REPMGR_PASSWORD=repmgr_pass- REPMGR_PRIMARY_HOST=primary- REPMGR_PARTNER_NODES=primary:5432,standby1:5432- REPMGR_NODE_NAME=primary- REPMGR_NODE_ID=1volumes:- primary_data:/bitnami/postgresqlports:- "5432:5432"standby1:image: bitnami/postgresql-repmgr:latestenvironment:- POSTGRESQL_POSTGRES_PASSWORD=standby_pass- REPMGR_PASSWORD=repmgr_pass- REPMGR_PRIMARY_HOST=primary- REPMGR_PARTNER_NODES=primary:5432,standby1:5432- REPMGR_NODE_NAME=standby1- REPMGR_NODE_ID=2- REPMGR_UPSTREAM_NODE_ID=1volumes:- standby1_data:/bitnami/postgresqldepends_on:- primaryvolumes:primary_data:standby1_data:
| 变量名 | 说明 | 示例值 |
|---|---|---|
| REPMGR_PRIMARY_HOST | 主节点主机名 | primary |
| REPMGR_NODE_NAME | 当前节点名称 | node1 |
| REPMGR_NODE_ID | 唯一节点ID | 1 |
| REPMGR_UPSTREAM_NODE_ID | 上游节点ID(从节点用) | 1 |
| REPMGR_PARTNER_NODES | 所有节点连接信息 | node1:5432,node2:5432 |
| POSTGRESQL_REPLICATION_PASSWORD | 复制用户密码 | repmgr_pass |
进入主节点容器执行:
repmgr cluster show
正常输出应显示所有节点状态为”running”,主节点标记为”*”。
apiVersion: apps/v1kind: StatefulSetmetadata:name: postgresql-haspec:serviceName: postgresql-hareplicas: 3selector:matchLabels:app: postgresqltemplate:spec:containers:- name: postgresqlimage: bitnami/postgresql-repmgr:latestenv:- name: REPMGR_NODE_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: REPMGR_NODE_IDvalueFrom:fieldRef:fieldPath: metadata.name# 需转换metadata.name为数字IDvolumeMounts:- name: datamountPath: /bitnami/postgresqlvolumeClaimTemplates:- metadata:name: dataspec:accessModes: [ "ReadWriteOnce" ]resources:requests:storage: 10Gi
推荐Prometheus+Grafana监控栈:
关键监控指标:
在postgresql.conf中调整:
# 内存配置shared_buffers = 4GBeffective_cache_size = 12GBwork_mem = 16MBmaintenance_work_mem = 1GB# 复制配置wal_level = replicamax_wal_senders = 10wal_keep_segments = 1024hot_standby = on
在repmgr.conf中设置:
failover=automaticpromote_command='/opt/bitnami/scripts/postgresql-repmgr/action-promote-replica.sh'follow_command='/opt/bitnami/scripts/postgresql-repmgr/action-follow-master.sh'
建议采用三重备份策略:
当网络分区导致多个主节点时:
repmgr cluster show确认实际状态
repmgr standby unregister --node-id=<wrong_master_id>repmgr standby follow --upstream-node-id=<correct_master_id>
当pg_stat_replication.lag_bytes持续增大时:
max_replication_slots参数启用SSL复制时需确保:
ssl=on和ssl_ca_file参数replication属性
kubectl delete pod postgresql-ha-2 --grace-period=300
建议使用ConfigMap管理核心配置:
apiVersion: v1kind: ConfigMapmetadata:name: postgresql-configdata:postgresql.conf: |# 自定义配置repmgr.conf: |# repmgr配置
通过Bitnami的postgresql-repmgr镜像,开发者可以在数小时内构建出符合生产标准的PostgreSQL HA集群。该方案相比手动搭建具有显著优势:
未来发展方向可关注:
建议用户在实际生产前进行完整的故障转移测试,并建立完善的监控告警体系。对于超大规模部署,可考虑结合Patroni等更高级的复制管理器。