高可用叢集透過消除單點故障,確保服務持續可用。本文以 CentOS 7 雙節點為例,詳細記錄從環境準備到服務部署的完整建置流程,適合生產環境參考部署。
| 角色 | 主機名 | IP 位址 | 說明 |
|---|---|---|---|
| 主節點 | node1 | 192.168.122.168 | 主要服務節點 |
| 備用節點 | node2 | 192.168.122.169 | 熱備接管節點 |
| 虛擬 IP | ClusterIP | 192.168.122.101 | 對外服務固定入口 |
yum install -y pcs pacemaker corosync fence-agents-all
若需特定服務(如 Apache、MySQL),請同步安裝對應套件。
# 永久關閉 SELinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
setenforce 0
# 配置防火牆允許集群通訊埠
firewall-cmd --permanent --add-service=high-availability
firewall-cmd --reload


# 兩節點均執行
hostnamectl set-hostname node1 # node2 對應修改
# /etc/hosts 雙向解析
cat >> /etc/hosts <


# 兩節點各自產生金鑰並互傳
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
ssh-copy-id root@node1
ssh-copy-id root@node2


# 兩節點同步建立 hacluster 用戶,密碼一致
useradd hacluster
echo "your_password" | passwd --stdin hacluster
# 啟用並啟動 pcsd
systemctl enable pcsd --now


# 任一節點執行
pcs host auth node1 node2 -u hacluster -p your_password
pcs cluster setup --name mycluster node1 node2
pcs cluster start --all
pcs cluster enable --all



pcs status
pcs cluster status

預設啟用 STONITH 但無隔離裝置會導致叢集不可用,測試環境可暫時關閉:
pcs property set stonith-enabled=false
pcs property set no-quorum-policy=ignore


生產環境警告:正式上線必須配置合法 STONITH 裝置(IPMI、iDRAC、APC PDU 等),否則資料一致性無保障。
pcs resource create ClusterIP ocf:heartbeat:IPaddr2 \
ip=192.168.122.101 cidr_netmask=24 \
op monitor interval=30s

yum install -y httpd
systemctl disable httpd # 由叢集管理,不可自啟

# 兩節點各自建立識別頁面
echo "node1 active" > /var/www/html/index.html
echo "node2 active" > /var/www/html/index.html



pcs resource create WebSite ocf:heartbeat:apache \
configfile=/etc/httpd/conf/httpd.conf \
statusurl="https://localhost/server-status" \
op monitor interval=1min
# 資源群組綁定:IP 與服務綁定、順序啟動
pcs resource group add WebGroup ClusterIP WebSite
https://192.168.122.101 確認服務正常pcs cluster stop node1 模擬故障,觀察 VIP 漂移至 node2| 現象 | 可能原因 | 排查方向 |
|---|---|---|
| 叢集無法形成 | 防火牆/SELinux/時間不同步 | 檢查腐心埠、ntpdate、getenforce |
| 資源啟動失敗 | 配置檔錯誤/埠佔用 | journalctl -u pacemaker、netstat -tlnp |
| VIP 不漂移 | STONITH 啟用但無裝置/quorum 遺失 | pcs property、corosync 日誌 |
本文完整示範了 CentOS 7 雙節點高可用集群的建置全流程。生產部署時請務必:配置 STONITH、規劃共享存儲、建立完善監控、定期演練故障切換。高可用不是裝好即忘,而是持續運維的系統工程。