兩台伺服器,一主一備。我這裡用 yum 安裝,想用 rpm 的話請自行下載套件。先掛上 epel 擴充源——國外官方源實在太慢,我選了阿里的鏡像(感謝馬雲)。
[root@tudou ~]# rm -rf /etc/yum.repos.d/* [root@tudou ~]# wget -P /etc/yum.repos.d/ https://mirrors.aliyun.com/repo/epel-6.repo
關閉防火牆:
[root@tudou ~]# iptables -F
[root@tudou ~]# yum install -y heartbeat [root@tudou ~]# yum install -y libnet
Heartbeat 在 /usr/share/doc/heartbeat 提供了範例:
[root@tudou ~]# cd /usr/share/doc/heartbeat-3.0.4/ [root@tudou heartbeat-3.0.4]# cp authkeys ha.cf haresources /etc/ha.d/
auth 3 #1 crc #2 sha1 HI! 3 md5 Hello!
authkeys 用於設定 Heartbeat 的認證方式,共 crc、md5、sha1 三種,安全性依次提高,系統資源佔用也依次增加。叢集跑在安全網路中可用 crc;節點硬件配置很高建議用 sha1;在網路安全與資源之間取平衡則用 md5。這裡我選 md5。
請務必確認該檔案權限為 600:
-rw-------. 1 root root 643 Aug 7 14:10 authkeys
node1 192.168.1.122/24/eth0:0 nginx
指定 node1 啟動 nginx 服務,並在 eth0:0 上掛一個虛擬 IP 192.168.1.122。當 node1 宕機時,node2 會自動啟動 nginx 並把該 IP 配到自己身上。
logfile /var/log/ha_log/ha-log.log ## ha 日誌位置,目錄需先手動建立 ucast eth0 192.168.1.107 ## 單播探測對方 IP keepalive 2 ## 心跳間隔 2 秒 warntime 10 deadtime 30 initdead 120 hopfudge 1 udpport 694 ## 心跳使用 UDP 694 auto_failback on ## 主機恢復後自動切回 node node1 node node2 ping 192.168.1.1 ## 透過 ping 閘道判斷心跳是否正常 respawn hacluster /usr/lib64/heartbeat/ipfail apiauth ipfail gid=root uid=root debugfile /Datas/logs/ha_log/ha-debug.log
把設定複製到備機:
[root@tudou ha.d]# scp ha.cf haresources authkeys root@192.168.1.107:/etc/ha.d/
/etc/init.d/heartbeat start tail -f /var/log/ha_log/ha-log.log
Heartbeat 第一次啟動非常慢,別著急,耐心等。當 VIP 起來之後基本就沒問題了。日誌中會看到類似下列關鍵訊息:
info: Configuration validated. Starting heartbeat 3.0.4 info: glib: ucast: started on port 694 interface eth0 to 192.168.1.107 info: Local status now set to: 'up' WARN: node node2: is dead info: Comm_now_up(): updating status to active info: Local status now set to: 'active' info: Starting child client "/usr/lib64/heartbeat/ipfail" (0,0) WARN: No STONITH device configured. WARN: Shared disks are not protected. info: Resources being acquired from node2. info: mach_down takeover complete for node node2. INFO: Adding inet address 192.168.1.122/24 with broadcast address 192.168.1.255 to device eth0 (with label eth0:0) INFO: /usr/libexec/heartbeat/send_arp -i 200 -r 5 ... eth0 192.168.1.122 auto not_used not_used INFO: Success info: Running /etc/init.d/nginx start info: Local Resource acquisition completed. info: local resource transition completed.
有幾則訊息值得留意:「node node2: is dead」代表判定備機失效並接管資源;「No STONITH device configured」與「Shared disks are not protected」是提醒你沒有配置 fencing 裝置、共享磁碟未受保護,在正式環境中應該補上,否則有 Split-Brain 風險;「ha.cf 中 hamaster 不在配置裡」這類 ERROR 則通常是主機名稱與 node 宣告不一致所致,把 node 名稱改成
uname -n的輸出即可。
本文出自「幕後黑手」博客,請務必保留此出處 https://11942699.blog.51cto.com/11932699/1835491