兩者結合形成高可用容器,服務資源可動態加入/移除,架構靈活。
| 角色 | 主機名 | IP |
|---|---|---|
| Node 1 | node1 | 192.168.10.103 |
| Node 2 | node2 | 192.168.10.219 |
| VIP | — | 192.168.10.130 |
# 兩節點皆執行
yum install -y corosync pacemaker python-dateutil redhat-rpm-config pssh
rpm -ivh https://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-6/x86_64/crmsh-2.1-1.6.x86_64.rpm
# 設定主機名
hostnamectl set-hostname node1 # node2 同理改為 node2
# /etc/hosts 互解
192.168.10.103 node1
192.168.10.219 node2
# 關閉防火牆與 SELinux(測試階段)
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
# node1 建立設定
cp /etc/corosync/corosync.conf.example /etc/corosync/corosync.conf
cat > /etc/corosync/corosync.conf <<'EOF'
compatibility: whitetank
totem {
version: 2
secauth: off
threads: 0
interface {
ringnumber: 0
bindnetaddr: 192.168.10.0
mcastaddr: 239.255.1.1
mcastport: 5405
ttl: 1
}
}
logging {
fileline: off
to_stderr: no
to_logfile: yes
to_syslog: yes
logfile: /var/log/cluster/corosync.log
debug: off
timestamp: on
logger_subsys { subsys: AMF; debug: off }
}
amf { mode: disabled }
service { ver: 0; name: pacemaker }
aisexec { user: root; group: root }
EOF
# node1
corosync-keygen
scp /etc/corosync/authkey /etc/corosync/corosync.conf node2:/etc/corosync/
# 兩節點
systemctl start corosync
systemctl enable corosync
netstat -na | grep 5405 # 應見 UDP 5405 監聽
crm status # 應顯示 2 nodes online
若crm status報Could not establish cib_ro connection,檢查防火牆、SELinux、主機名解析、authkey 是否在修改主機名後產生。
crm configure <<'EOF'
# 全域屬性
property stonith-enabled=false
property no-quorum-policy=ignore
# VIP 資源
primitive vip ocf:heartbeat:IPaddr2 \
params ip=192.168.10.130 cidr_netmask=24 nic=eth0 \
op monitor interval=30s timeout=20s on-fail=restart
# Nginx 資源 (LSB init script)
primitive nginx lsb:nginx \
op monitor interval=30s timeout=20s on-fail=restart
# 併置約束:Nginx 必須跟著 VIP 跑
colocation nginx-with-vip inf: nginx vip
# 順序約束:先起 VIP 再起 Nginx
order vip-before-nginx mandatory: vip nginx
# 驗證並提交
verify
commit
EOF
# 建立測試頁面區分節點
# node1
echo "this is node1" > /var/www/html/index.html
# node2
echo "this is node2" > /var/www/html/index.html
# 瀏覽器訪問 https://192.168.10.130 觀察內容
# 模擬 node1 故障
crm node standby node1
# 觀察 VIP 與 Nginx 是否遷移至 node2,瀏覽器重新整理內容應變為 "this is node2"
# 恢復 node1
crm node online node1
# 資源依約束遷回 node1(若設定 stickiness 可留在 node2)
pkill -9 corosync 強制終止,否則關機極慢。/etc/init.d/nginx 存在且可 start/stop/status,Pacemaker 才能透過 LSB 代理管理。bindnetaddr 必須設為對應網段,nic 參數指定正確介面。stonith-enabled=true)並配置 fence 裝置(IPMI/PDU/VMware SDK),防止腦裂導致資料損壞。完成上述部署,即擁有一套以 Corosync 做心跳、Pacemaker 做資源決策的 Nginx 高可用叢集,可平滑應對單節點故障與計畫性維護,保障 Web 服務連續性。