PCS 管理 PostgreSQL 高可用集群

以下是使用 Pacemaker/Corosync (PCS) 管理 PostgreSQL 高可用集群的详细实现方案。该方案基于 流复制(Streaming Replication) 和 自动故障转移,确保数据库服务的高可用性。


一、环境准备

  1. 节点规划
    | 节点名称 | IP 地址 | 角色 |
    |-------------|--------------|--------------------|
    | node1 | 192.168.1.101| 主节点 + 集群节点 |
    | node2 | 192.168.1.102| 备节点 + 集群节点 |
    | virtual_ip | 192.168.1.100| 虚拟 IP(VIP) |

  2. 前提条件
    • 所有节点时间同步(使用 NTP)。

• 主机名解析(/etc/hosts 或 DNS)。

• 关闭 SELinux 和防火墙(或放行相关端口)。

• 已安装相同版本的 PostgreSQL。


二、安装与配置 Pacemaker/Corosync

  1. 安装 PCS 软件包
# 在所有节点执行
yum install -y pacemaker corosync pcs psmisc
systemctl enable pcsd --now
  1. 配置集群认证
# 设置集群管理用户密码(所有节点)
echo "hacluster" | passwd hacluster --stdin

# 节点间认证(在任意节点执行)
pcs cluster auth node1 node2 -u hacluster -p hacluster
  1. 创建集群
# 在 node1 上初始化集群
pcs cluster setup pg_cluster node1 node2 --force

# 启动集群
pcs cluster start --all
pcs cluster enable --all
  1. 禁用无关服务
pcs property set stonith-enabled=false      # 禁用 STONITH(测试环境)
pcs property set no-quorum-policy=ignore    # 忽略仲裁丢失

三、配置 PostgreSQL 流复制

  1. 主节点配置(node1)

  2. 启用流复制:
    修改 postgresql.conf

    wal_level = replica
    max_wal_senders = 5
    listen_addresses = '*'
    
  3. 配置复制用户:
    pg_hba.conf 中添加:

    host replication rep_user 192.168.1.0/24 md5
    
  4. 重启 PostgreSQL:

    systemctl restart postgresql
    
  5. 备节点配置(node2)

  6. 初始化备库:

    pg_basebackup -h node1 -U rep_user -D /var/lib/pgsql/data -P -R
    
  7. 启动 PostgreSQL:

    systemctl start postgresql
    

四、配置 Pacemaker 资源

  1. 定义资源代理
# 安装 PostgreSQL 资源代理
yum install -y resource-agents
  1. 创建集群资源
# 添加虚拟 IP 资源
pcs resource create pg_vip ocf:heartbeat:IPaddr2 ip=192.168.1.100 cidr_netmask=24 op monitor interval=30s

# 添加 PostgreSQL 资源
pcs resource create pgsql ocf:heartbeat:pgsql \
  pgctl="/usr/bin/pg_ctl" \
  psql="/usr/bin/psql" \
  pgdata="/var/lib/pgsql/data" \
  rep_mode="sync" \
  node_list="node1 node2" \
  primary_conninfo_opt="password=rep_password" \
  op start timeout=60s \
  op stop timeout=60s \
  op promote timeout=30s \
  op demote timeout=120s \
  op monitor interval=15s timeout=10s role=Master \
  op monitor interval=16s timeout=10s role=Slave

# 设置资源组(确保 VIP 和 PostgreSQL 在同一节点)
pcs constraint colocation add pg_vip with pgsql INFINITY
pcs constraint order pgsql then pg_vip
  1. 验证资源配置
pcs status          # 查看集群状态
pcs resource show   # 显示资源详情

五、故障转移测试

  1. 模拟主节点故障
# 在 node1 上停止 PostgreSQL
systemctl stop postgresql

# 观察 Pacemaker 自动切换至 node2
pcs status | grep "Current DC"
  1. 手动切换主备
pcs resource move pgsql node2  # 将主库切换至 node2
pcs resource clear pgsql       # 清理操作状态

六、日常管理命令

命令说明
pcs cluster status查看集群状态
pcs resource disable pgsql临时禁用资源
pcs node standby node1将节点设为备用
pcs cluster stop --all停止整个集群
crm_mon -Afr实时监控集群

七、常见问题解决

  1. 脑裂(Split-Brain)
    • 现象:节点间通信中断,各自认为自己是主节点。

• 解决:

pcs cluster stop --all          # 停止所有节点
pcs cluster start --all         # 重新启动
  1. 资源无法启动
    • 现象:pcs status 显示资源 FAILED

• 解决:

pcs resource cleanup pgsql      # 清理资源状态
journalctl -xe                  # 查看详细日志
  1. VIP 无法切换
    • 现象:虚拟 IP 未迁移到新主节点。

• 解决:

arping -c 3 -U -I eth0 192.168.1.100  # 强制刷新 ARP
pcs resource restart pg_vip           # 重启 VIP 资源

八、性能优化建议

  1. 调整复制模式:
    pcs resource update pgsql rep_mode=async  # 异步复制(更高性能)
    
  2. 监控同步延迟:
    SELECT pg_current_wal_lsn(), replay_lsn FROM pg_stat_replication;
    
  3. 启用 WAL 压缩:
    postgresql.conf 中设置 wal_compression = on

通过此方案,可以实现基于 Pacemaker/Corosync 的 PostgreSQL 高可用集群,确保数据库服务在节点故障时自动恢复。根据实际业务需求调整资源参数和监控策略。(腾讯元宝AI生成结果)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值