雙節點高可用叢集 (Active-Passive):
node1 (192.168.122.168)node2 (192.168.122.169)192.168.122.101 (ClusterIP)# 主機名
hostnamectl set-hostname node1 # node2 同理
# /etc/hosts 互解
cat >> /etc/hosts <<'EOF'
192.168.122.168 node1
192.168.122.169 node2
EOF
# 時間同步
dnf install -y chrony
systemctl enable --now chronyd
chronyc sources
# 關閉 SELinux (生產建議設為 permissive 並配置布林值)
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
setenforce 0
# 防火牆開放叢集通訊埠
firewall-cmd --permanent --add-service=high-availability
firewall-cmd --permanent --add-port=5404/udp # corosync
firewall-cmd --permanent --add-port=5405/udp # corosync
firewall-cmd --permanent --add-port=2224/tcp # pcsd
firewall-cmd --reload
dnf install -y pcs pacemaker corosync fence-agents-all
passwd hacluster
# 輸入相同密碼,如: ClusterPass123!
systemctl enable --now pcsd
# 節點 1 執行
ssh-keygen -t rsa -b 4096 -N "" -f ~/.ssh/id_rsa
ssh-copy-id hacluster@node2
# 節點 2 執行
ssh-keygen -t rsa -b 4096 -N "" -f ~/.ssh/id_rsa
ssh-copy-id hacluster@node1
# 在任一節點執行
pcs host auth node1 node2 -u hacluster -p ClusterPass123!
# 輸出: node1: Authorized, node2: Authorized
pcs cluster setup --name mycluster node1 node2
pcs cluster start --all
pcs cluster enable --all # 開機自啟
pcs status
# 輸出關鍵資訊:
# Cluster name: mycluster
# Stack: corosync
# Current DC: node1 (version 2.x.x) - partition with quorum
# 2 nodes configured
# Online: [ node1 node2 ]
#
# No resources configured
Corosync 預設啟用 STONITH,無隔離裝置叢集不會啟動資源。
# 方案 A:禁用 STONITH (僅測試/實驗環境)
pcs property set stonith-enabled=false
# 方案 B:配置 fence_virtd (KVM/libvirt 虛擬機環境)
# 宿主機安裝 fence-virtd,節點安裝 fence-agents-virtd
# pcs stonith create fence_virtd fence_virtd pcmk_host_list="node1 node2" ...
# 方案 C:IPMI/iDRAC/iLO (實體伺服器)
# pcs stonith create my-ipmi fence_ipmilan pcmk_host_list="node1 node2" \
# ipaddr=192.168.1.100 login=admin passwd=secret lanplus=1 \
# pcmk_host_map="node1:192.168.1.101;node2:192.168.1.102"
# pcs stonith level add 1 node1 my-ipmi
# pcs stonith level add 1 node2 my-ipmi
⚠️ **生產環境務必配置真實 STONITH**,否則腦裂風險極高。
pcs resource create ClusterIP ocf:heartbeat:IPaddr2 \
ip=192.168.122.101 cidr_netmask=24 nic=eth0 \
op monitor interval=30s timeout=20s on-fail=restart
ocf:heartbeat:IPaddr2:標準 IP 代理op monitor interval=30s:每 30 秒健康檢查on-fail=restart:失敗先嘗試本地重啟資源dnf install -y httpd
systemctl disable --now httpd # 重要:叢集接管服務生命週期
# node1
echo "Node1: $(hostname)
" > /var/www/html/index.html
# node2
echo "Node2: $(hostname)
" > /var/www/html/index.html
pcs resource create WebSite systemd:httpd \
op monitor interval=30s timeout=20s on-fail=restart \
op start timeout=60s op stop timeout=60s
# 建立群組:VIP 優先啟動,WebSite 依賴 VIP
pcs resource group add WebGroup ClusterIP WebSite
# 等價於分別設定:
# pcs constraint order ClusterIP then WebSite kind=Mandatory
# pcs constraint colocation WebSite with ClusterIP score=INFINITY
pcs constraint location WebGroup prefers node1=50
分數 >0 表示偏好,INFINITY 為強制。雙節點任意 >0 即生效。
pcs resource defaults resource-stickiness=100
# 或針對群組
pcs resource meta WebGroup resource-stickiness=100
100:傾向留在當前節點,避免因微小分數波動頻繁遷移INFINITY:強制不遷移 (手動遷移除外)# 查看完整狀態
pcs status
# 手動遷移測試
pcs resource move WebGroup node2
pcs status
pcs resource clear WebGroup # 清除位置限制,恢復偏好規則
# 模擬節點故障 (節點 1)
# 在 node1 執行:
systemctl stop pacemaker
# 觀察 node2:VIP + httpd 應在 30-60 秒內漂移過去
# 恢復節點 1
systemctl start pacemaker
# 觀察是否根據 stickiness/偏好遷回
| 操作 | 指令 |
|---|---|
| 查看狀態 | pcs status / pcs status --full |
| 查看配置 | pcs config |
| 啟用/停用叢集 | pcs cluster enable --all / pcs cluster disable --all |
| 啟動/停止叢集服務 | pcs cluster start --all / pcs cluster stop --all |
| 待機/恢復節點 | pcs cluster standby node1 / pcs cluster unstandby node1 |
| 手動遷移資源 | pcs resource move WebGroup node2 |
| 清除遷移限制 | pcs resource clear WebGroup |
| 進入維護模式 | pcs property set maintenance-mode=true |
| 查看資源預設值 | pcs resource defaults |
| 查看約束 | pcs constraint list --full |
/etc/hosts 互解、主機名正確hacluster 密碼兩節點**完全一致**systemctl disable httpdCentOS 7 系高可用叢集標準棧:**PCS + Pacemaker + Corosync**。核心流程:基礎環境同步 → 安裝軟體 → 認證授權 → 建立叢集 → 配置 STONITH → 定義資源 (VIP + 服務) → 群組綁定 → 約束優化 → 驗證測試。生產部署必須重視 STONITH 配置、資源黏著性、監控告警與定期故障演練。