首先配置Corosync与Pacemaker实现通信与资源管理,1.准备节点环境并配置网络、时间同步及主机解析,2.安装必要软件包并启用pcsd服务,3.设置hacluster用户密码用于认证,4.认证节点并创建启动集群,5.可选配置STONITH防止脑裂,6.添加VIP和Web服务资源并设置依赖关系,7.验证故障转移与维护状态,确保服务高可用。
在linux系统中配置高可用(High Availability, HA)集群,主要目标是确保关键服务在节点故障时仍能持续运行。常用方案是基于 Corosync 和 Pacemaker 实现集群通信与资源管理。以下为详细的配置步骤,适用于如centos、RHEL或ubuntu等主流发行版。
1. 准备工作与环境要求
确保所有节点满足基本条件:
- 至少两台运行相同Linux版本的服务器(建议3台以避免脑裂)
- 各节点间可通过静态IP通信,建议使用内网专用网络
- 所有节点时间同步(使用NTP或chrony)
- 配置主机名解析(通过/etc/hosts或dns)
- 关闭防火墙或开放Corosync/Pacemaker所需端口(如5405、2224等)
- 设置ssh互信以便管理操作
示例:编辑 /etc/hosts
192.168.10.10 node1 192.168.10.11 node2
2. 安装Corosync与Pacemaker
在所有节点上安装必要软件包。
CentOS/RHEL:
yum install -y pacemaker pcs corosync fence-agents-all
Ubuntu/debian:
apt-get install pacemaker corosync pcs fence-agents
安装完成后启动并启用 pcsd 服务:
systemctl start pcsd systemctl enable pcsd
3. 设置集群用户密码
默认使用 hacluster 用户管理集群,需在所有节点上设置相同密码:
passwd hacluster
该用户用于节点间认证和集群命令执行。
4. 配置并启动集群
选择一个节点执行集群初始化操作。
认证节点:
pcs host auth node1 node2 -u hacluster -p your_password
创建集群:
pcs cluster setup my-cluster node1 node2
启动集群:
pcs cluster start --all
启用开机自启:
pcs cluster enable --all
查看集群状态:
pcs status
5. 配置STONITH与资源代理(可选但推荐)
STONITH(Shoot The Other Node In The Head)用于防止脑裂,需配置 fencing 设备。
若测试环境无物理fencing设备,可临时禁用:
pcs property set stonith-enabled=false
生产环境应配置真实设备,例如IPMI、vSphere等。
6. 添加集群资源(如VIP、http服务)
定义虚拟IP地址作为浮动IP:
pcs resource create vip ocf:heartbeat:ipaddr2 ip=192.168.10.100 cidr_netmask=24 op monitor interval=30s
添加apache服务资源:
pcs resource create webserver systemd:httpd op monitor interval=60s
设置资源共存关系(VIP与Web服务在同一节点运行):
pcs constraint colocation add webserver with vip INFINITY
设置启动顺序:
pcs constraint order promote vip then start webserver
7. 验证与维护
检查资源分布与状态:
pcs status resources
模拟节点宕机测试故障转移:
pcs cluster standby node1
观察资源是否自动迁移到node2。恢复节点:
pcs cluster unstandby node1
定期查看日志排查问题:
tail -f /var/log/pacemaker.log
基本上就这些。只要网络、时间、权限配置正确,Linux高可用集群可以稳定运行。关键是理解Corosync负责通信,Pacemaker负责资源调度,两者配合实现自动故障切换。配置过程中注意安全策略和fencing机制,避免数据损坏风险。