温馨提示×

centos postgresql高可用性解决方案

小樊
37
2025-10-17 22:00:14
栏目: 云计算

CentOS PostgreSQL高可用性解决方案

在CentOS环境下,实现PostgreSQL高可用性需结合数据复制故障转移负载均衡等技术。以下是几种主流方案及详细实现步骤:

一、基础方案:流复制(Streaming Replication)

流复制是PostgreSQL原生支持的高可用机制,通过WAL(Write-Ahead Log)日志实现主从数据同步,适用于需要读写分离基础故障转移的场景。

1. 主节点配置

  • 安装PostgreSQLsudo yum install -y postgresql-server postgresql-contrib
  • 初始化数据库sudo postgresql-setup initdb
  • 修改postgresql.conf:设置listen_addresses = '*'(监听所有IP)、wal_level = replica(开启复制日志)、max_wal_senders = 10(允许10个WAL发送进程)、wal_keep_segments = 64(保留64个WAL文件)。
  • 修改pg_hba.conf:添加复制权限host replication replicator 0.0.0.0/0 md5(允许所有IP的复制用户连接,密码认证)。
  • 创建复制用户CREATE USER replicator REPLICATION LOGIN ENCRYPTED PASSWORD 'your_password';
  • 重启服务sudo systemctl restart postgresql

2. 从节点配置

  • 备份主节点数据pg_basebackup -h master_ip -U replicator -D /var/lib/pgsql/data --wal-method=stream --no-password(实时拉取主节点数据)。
  • 修改postgresql.conf:与主节点一致(listen_addresses = '*'hot_standby = on)。
  • 创建recovery.confstandby_mode = 'on'(声明为备库)、primary_conninfo = 'host=master_ip dbname=postgres user=replicator password=your_password'(连接主库的信息)。
  • 启动服务sudo systemctl start postgresql

3. 验证复制状态

在主节点执行SELECT * FROM pg_stat_replication;,若返回备库连接信息,则说明复制成功。

二、进阶方案:repmgr + 流复制

repmgr是PostgreSQL生态中的复制管理工具,可自动化监控、故障转移和节点管理,弥补原生流复制的不足。

1. 安装repmgr

sudo yum install -y repmgr

2. 配置主从节点

  • 主节点:编辑/etc/repmgr.conf,设置node_id = 1node_name = masterconninfo = 'host=master_ip dbname=postgres user=repmgr password=repmgr_pass'
  • 从节点:编辑/etc/repmgr.conf,设置node_id = 2node_name = standbyconninfo = 'host=standby_ip dbname=postgres user=repmgr password=repmgr_pass'primary_conninfo = 'host=master_ip dbname=postgres user=replicator password=your_password'

3. 初始化集群

  • 主节点repmgr -f /etc/repmgr.conf init(初始化主节点)。
  • 从节点repmgr -f /etc/repmgr.conf standby clone master_ip(克隆主节点数据)、repmgr -f /etc/repmgr.conf standby register(注册备库到集群)。

4. 故障转移

若主节点故障,执行repmgr -f /etc/repmgr.conf failover,repmgr会自动提升备库为新主库,并更新集群状态。

三、企业级方案:Patroni + etcd + Keepalived

Patroni是基于Raft算法的高可用管理工具,结合etcd(分布式键值存储)实现集群状态同步,Keepalived提供虚拟IP(VIP),确保客户端始终访问可用节点。

1. 环境准备

  • 至少3台CentOS服务器(1主2从)。
  • 安装etcd集群(用于Patroni状态同步)。

2. 安装Patroni

sudo yum install -y python3-pip && sudo pip3 install patroni

3. 配置Patroni

  • 主节点(/etc/patroni.yml):
    scope: postgres_cluster name: node1 namespace: /db restapi: listen: 0.0.0.0:8008 connect_address: 192.168.1.101 etcd: host: 192.168.1.103:2379 # etcd服务器IP bootstrap: dcs: ttl: 30 retry_timeout: 10 postgresql: use_pg_rewind: true parameters: wal_level: replica hot_standby: on max_wal_senders: 4 initdb: - encoding: UTF8 - locale: en_US.UTF-8 pg_hba: - host replication replicator 192.168.1.0/24 md5 - host all all 192.168.1.0/24 md5 postgresql: listen: 0.0.0.0:5432 connect_address: 192.168.1.101 data_dir: /var/lib/pgsql/data authentication: replication: username: replicator password: replicator_pass superuser: username: postgres password: postgres_pass 
  • 从节点:修改nameconnect_addressdata_dir,其他配置与主节点一致。

4. 安装Keepalived(提供VIP)

  • 主节点(/etc/keepalived/keepalived.conf):
    vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1234 } virtual_ipaddress { 192.168.1.100 # 虚拟IP,客户端通过此IP访问PostgreSQL } } 
  • 从节点:将state改为BACKUPpriority改为90(低于主节点)。

5. 启动服务

  • 所有节点sudo systemctl start patroni && sudo systemctl enable patroni
  • 所有节点sudo systemctl start keepalived && sudo systemctl enable keepalived

6. 验证高可用性

  • 访问虚拟IP192.168.1.100,确认能连接到PostgreSQL。
  • 手动停止主节点的PostgreSQL服务,观察Keepalived是否将VIP切换到备用节点,Patroni是否自动提升备库为主库。

四、注意事项

  • 监控与告警:使用Prometheus+Granafa监控集群状态(如节点存活、复制延迟),设置邮件/短信告警。
  • 备份策略:定期使用pg_dumpbarman备份数据,测试恢复流程。
  • 安全性:配置防火墙(仅允许必要端口,如5432、2379、8008),禁用默认用户(如postgres),启用SSL加密连接。

0