Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 伺服器綜合

基於 Corosync + Pacemaker 的 Nginx 高可用叢集部署實戰

編輯:伺服器綜合

架構總覽

  • Corosync: 叢集通訊層,提供訊息傳遞、成員檢測、Quorum 仲裁。
  • Pacemaker: 資源管理器 (CRM),決定資源在哪個節點運行、故障時如何遷移。
  • 資源代理: 管理 Nginx、虛擬 IP (VIP) 等服務的啟停/監控腳本(OCF/LSB)。

兩者結合形成高可用容器,服務資源可動態加入/移除,架構靈活。

環境規劃

角色主機名IP
Node 1node1192.168.10.103
Node 2node2192.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

三、Corosync 設定

# 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 是否在修改主機名後產生。

五、Pacemaker 資源配置 (CRM Shell)

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)

七、常見坑點與解法

  • Corosync 無法停止: pkill -9 corosync 強制終止,否則關機極慢。
  • Nginx 未納入 systemd: 確保 /etc/init.d/nginx 存在且可 start/stop/status,Pacemaker 才能透過 LSB 代理管理。
  • 多網卡環境: bindnetaddr 必須設為對應網段,nic 參數指定正確介面。
  • STONITH: 生產環境務必啟用(stonith-enabled=true)並配置 fence 裝置(IPMI/PDU/VMware SDK),防止腦裂導致資料損壞。

完成上述部署,即擁有一套以 Corosync 做心跳、Pacemaker 做資源決策的 Nginx 高可用叢集,可平滑應對單節點故障與計畫性維護,保障 Web 服務連續性。

Copyright © Windows教程網 All Rights Reserved