# CentOS7中如何實現Pacemaker高可用安裝配置
## 一、高可用集群概述
### 1.1 什么是高可用性集群
高可用性集群(High Availability Cluster,簡稱HA Cluster)是指通過軟件和硬件技術,將多臺服務器組成一個整體,當其中某個節點發生故障時,其他節點能夠自動接管服務,從而保證業務連續性的解決方案。
### 1.2 Pacemaker簡介
Pacemaker是一個開源的高可用集群資源管理器,它:
- 監測節點和服務狀態
- 提供故障檢測和恢復能力
- 支持多種資源類型(IP、服務、文件系統等)
- 與Corosync共同工作提供集群通信層
### 1.3 典型應用場景
- Web服務器負載均衡與故障轉移
- 數據庫主從切換
- 關鍵業務應用的高可用保障
## 二、環境準備
### 2.1 硬件要求
- 至少兩臺x86_64架構服務器
- 每臺服務器建議配置:
- 2核CPU
- 4GB內存
- 50GB磁盤空間
- 共享存儲(可選,用于需要數據一致性的服務)
### 2.2 網絡配置
| 節點 | 主機名 | IP地址 | 用途 |
|--------|-------------|----------------|--------------------|
| 節點1 | node1 | 192.168.1.101 | 集群節點 |
| 節點2 | node2 | 192.168.1.102 | 集群節點 |
| 虛擬IP | - | 192.168.1.100 | 對外服務IP |
### 2.3 系統準備
在兩臺節點上執行:
```bash
# 關閉防火墻
systemctl stop firewalld
systemctl disable firewalld
# 禁用SELinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
# 配置主機名解析
echo "192.168.1.101 node1" >> /etc/hosts
echo "192.168.1.102 node2" >> /etc/hosts
# 安裝基礎工具
yum install -y vim wget net-tools
在兩臺節點上執行:
# 添加High Availability倉庫
yum install -y centos-release-highavailability
# 安裝Pacemaker和Corosync
yum install -y pacemaker pcs corosync fence-agents-all
# 啟動pcsd服務并設置開機啟動
systemctl start pcsd
systemctl enable pcsd
設置hacluster用戶密碼(所有節點使用相同密碼):
echo "password" | passwd --stdin hacluster
在任意一個節點上執行:
# 認證節點
pcs cluster auth node1 node2 -u hacluster -p password
# 創建并啟動集群
pcs cluster setup --name my_cluster node1 node2
pcs cluster start --all
pcs cluster enable --all
# 檢查集群狀態
pcs status cluster
# 禁用STONITH(測試環境)
pcs property set stonith-enabled=false
# 設置資源粘性(防止資源在節點間頻繁切換)
pcs property set default-resource-stickiness=100
# 設置故障轉移策略
pcs property set start-failure-is-fatal=false
編輯/etc/corosync/corosync.conf:
totem {
version: 2
cluster_name: my_cluster
transport: udpu
interface {
ringnumber: 0
bindnetaddr: 192.168.1.0
mcastport: 5405
ttl: 1
}
}
quorum {
provider: corosync_votequorum
two_node: 1
}
logging {
to_logfile: yes
logfile: /var/log/cluster/corosync.log
to_syslog: yes
}
重啟服務生效:
pcs cluster reload corosync
pcs resource create Virtual_IP ocf:heartbeat:IPaddr2 \
ip=192.168.1.100 cidr_netmask=24 \
op monitor interval=30s
# 在兩臺節點上安裝Apache
yum install -y httpd
# 創建資源
pcs resource create WebServer ocf:heartbeat:apache \
configfile=/etc/httpd/conf/httpd.conf \
statusurl="http://127.0.0.1/server-status" \
op monitor interval=1min
# 設置資源組(確保資源在同一節點運行)
pcs resource group add WebGroup Virtual_IP WebServer
# 設置首選節點
pcs constraint location WebGroup prefers node1=100
# 將資源遷移到node2
pcs resource move WebGroup node2
# 清除遷移約束
pcs constraint remove cli-prefer-WebGroup
# 在node1上停止集群服務
pcs cluster stop
# 觀察node2是否接管資源
pcs status
# 重新啟動node1集群服務
pcs cluster start node1
# 觀察資源是否會回切
watch pcs status
# 示例:使用IPMI電源管理
pcs stonith create my_fence fence_ipmilan \
pcmk_host_list="node1 node2" \
ipaddr="192.168.1.50" login="admin" passwd="password" \
op monitor interval=1min
# 啟用STONITH
pcs property set stonith-enabled=true
# 示例:NFS共享的高可用配置
pcs resource create NFS_Share ocf:heartbeat:Filesystem \
device="/dev/sdb1" directory="/mnt/nfs" fstype="ext4"
pcs resource create NFS_Server ocf:heartbeat:nfsserver \
op monitor interval=30s
pcs constraint order NFS_Share then NFS_Server
pcs constraint colocation add NFS_Server with NFS_Share INFINITY
# 查看集群狀態
pcs status
# 查看資源配置
pcs resource show
# 查看約束規則
pcs constraint show
# 臨時禁用節點
pcs cluster standby node1
# Corosync日志
tail -f /var/log/cluster/corosync.log
# Pacemaker日志
journalctl -u pacemaker -f
/var/log/messages獲取詳細錯誤本文詳細介紹了在CentOS7環境下部署Pacemaker高可用集群的全過程,包括: 1. 環境準備與基礎配置 2. Pacemaker+Corosync集群安裝 3. 高可用資源配置與管理 4. 故障轉移測試與日常維護
通過合理配置,Pacemaker可以提供企業級的高可用保障,確保關鍵業務服務的連續性。實際生產環境中,建議結合監控系統(如Zabbix或Prometheus)對集群狀態進行實時監控,并定期進行故障演練以驗證集群可靠性。
注意:本文配置適用于測試環境,生產環境部署需要考慮網絡安全、性能調優和詳細的災難恢復方案。 “`
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。