Pacemaker 集群搭建与高可用Web服务实战

张开发
2026/5/31 10:32:58 15 分钟阅读
Pacemaker 集群搭建与高可用Web服务实战
1. 为什么需要Pacemaker集群想象一下你运营着一个电商网站突然服务器宕机了所有用户都无法下单。这种情况每年会给企业带来数百万的损失。这就是为什么我们需要高可用集群——确保服务永不中断。Pacemaker作为Linux生态中最成熟的开源集群资源管理器能够实现自动故障检测与恢复当主节点宕机时备节点能在秒级完成接管资源智能调度根据节点负载自动平衡服务零单点故障通过多节点协作消除系统脆弱点我经手的一个金融项目使用Pacemaker后系统可用性从99.9%提升到99.99%相当于每年故障时间从8小时缩短到52分钟。这种提升对关键业务来说价值巨大。2. 环境准备与基础配置2.1 硬件与网络规划建议使用两台配置相近的服务器物理机或虚拟机网络拓扑要特别注意心跳网络用于节点间通信建议使用独立网卡和交换机业务网络承载VIP和实际服务流量存储网络如果使用共享存储需要额外规划这里我们用两台CentOS 7虚拟机演示node1: 192.168.139.87node2: 192.168.139.88VIP: 192.168.139.118提示生产环境强烈建议使用3节点以上集群双节点存在脑裂风险2.2 系统基础配置首先在所有节点执行这些基础操作# 关闭防火墙和SELinux systemctl stop firewalld systemctl disable firewalld setenforce 0 # 永久关闭SELinux sed -i s/SELINUXenforcing/SELINUXdisabled/g /etc/selinux/config # 安装集群组件 yum install -y fence-agents-all corosync pacemaker pcs我遇到过不少案例因为SELinux没彻底关闭导致集群异常建议双重确认配置已生效。3. 集群核心组件搭建3.1 节点通信配置集群节点需要通过主机名互相解析修改/etc/hosts文件# node1和node2上都要配置 cat /etc/hosts EOF 192.168.139.87 node1 192.168.139.88 node2 EOF # 验证解析 ping -c 3 node2然后配置SSH免密登录# 生成密钥对 ssh-keygen -t rsa -b 4096 -N -f ~/.ssh/id_rsa # 互相分发公钥 ssh-copy-id node1 ssh-copy-id node23.2 集群用户与认证Pacemaker使用hacluster用户进行管理# 设置统一密码 echo YourSecurePassword | passwd --stdin hacluster # 启动pcsd服务 systemctl start pcsd systemctl enable pcsd # 节点认证 pcs cluster auth node1 node2 -u hacluster -p YourSecurePassword4. Web服务高可用实战4.1 Apache服务配置在两台节点安装配置Apacheyum install -y httpd # 创建测试页面 echo This is node1 /var/www/html/index.html # node1上执行 echo This is node2 /var/www/html/index.html # node2上执行 # 配置状态检测 cat /etc/httpd/conf/httpd.conf EOF Listen 0.0.0.0:80 ServerName www.demo.com Location /server-status SetHandler server-status Require all granted /Location EOF # 注意先不要启动httpd服务4.2 集群资源管理创建并启动集群# 初始化集群 pcs cluster setup --name web_cluster node1 node2 pcs cluster start --all pcs cluster enable --all # 添加VIP资源 pcs resource create ClusterVIP ocf:heartbeat:IPaddr2 \ ip192.168.139.118 cidr_netmask24 \ op monitor interval30s # 添加Web资源 pcs resource create WebService ocf:heartbeat:apache \ configfile/etc/httpd/conf/httpd.conf \ statusurlhttp://localhost/server-status \ op monitor interval30s资源约束配置是关键# 资源组方式推荐新手 pcs resource group add WebGroup ClusterVIP WebService # 或者使用约束方式 pcs constraint colocation add WebService ClusterVIP INFINITY pcs constraint order ClusterVIP then WebService5. 故障转移测试与优化5.1 手动模拟故障首先查看当前资源运行节点pcs status | grep -E ClusterVIP|WebService然后主动关闭主节点服务# 在主节点执行 pcs cluster stop node1 # 在备节点查看接管情况 pcs status curl http://192.168.139.1185.2 自动化调优建议根据业务需求调整检测参数# 更敏感的心跳检测 pcs resource update ClusterVIP op monitor interval10s timeout20s # 设置故障恢复策略 pcs resource defaults resource-stickiness100 pcs property set start-failure-is-fatalfalse我在生产环境发现这些参数组合效果最佳检测间隔10-30秒超时时间检测间隔的2倍故障重试次数3次6. 生产环境注意事项实际部署时还需要考虑** fencing设备配置**防止脑裂现象DRBD存储同步保证数据一致性监控集成与Prometheus等监控系统对接日志收集统一分析集群事件曾经有个客户集群频繁发生误切换最后发现是网络抖动导致。我们通过调整corosync的token超时时间解决了问题# 调整心跳超时参数 pcs property set token30000 pcs property set consensus36000记住任何高可用方案都需要定期演练。我建议至少每季度做一次完整的故障转移测试包括模拟硬件故障、网络分区等极端情况。

更多文章