環境:
主機(master):192.168.11.160
主機(slave):192.168.11.20
流動 ip:192.168.11.100
1. 關閉主從機器的防火牆,配置檔案/etc/selinux/config,修改SELINUX=disabled,iptables -F
2. 編輯主從機器的hosts檔案/etc/hosts,修改hostname,便于标記
192.168.11.160 master
192.168.11.20 slave
3. 主從機器上yum安裝heartbeat和依賴包libnet
說明:預設yum沒有heartbeat,需下載下傳安裝擴充源,連結如下:
指令 yum install -y heartbeat libnet
主從配置
1. master上拷貝heartbeat的配置檔案
說明:authkey檔案,主從通信驗證作用
指令 cd /usr/share/doc/heartbeat-3.0.4/
指令 cp authkeys ha.cf haresources /etc/ha.d/
2. 編輯master檔案/etc/ha.d/authkeys,并賦予600權限
說明:啟用認證;加密級别為3(md5),加密級别,1<3<2
内容:
<code> </code><code>auth 3</code>
<code> </code><code>3 md5 Hello!</code>
授權:
<code> </code><code>chmod</code> <code>600 </code><code>/etc/ha</code><code>.d</code><code>/authkeys</code>
3. 編輯master上/etc/ha.d/haresources檔案
說明:主的主機名;虛拟ip,即主從共用的對外ip,當主ip挂了,流動ip自動流動到從主
機上,并對外開放;主從服務對象為nginx,主當機了,切換為從上
内容:
<code> </code><code>master 192.168.11.110</code><code>/24/eth0</code><code>:0 nginx</code>
一網卡配置多個ip:複制/etc/sysconfig/network-scripts/ifcfg-eth0,為ifcfg-eth0:\1,注釋掉mac和uuid,重新開機添加一個新的ip即可
4. 編輯master上/etc/ha.d/vim ha.cf檔案 ,清空配置,自定義編輯
<code> </code><code>debugfile </code><code>/var/log/ha-debug</code>
<code> </code><code>logfile </code><code>/var/log/ha-log</code>
<code> </code><code>logfacility local0</code>
<code> </code><code>keepalive 2</code>
<code> </code><code>deadtime 30</code>
<code> </code><code>warntime 10</code>
<code> </code><code>initdead 60</code>
<code> </code><code>udpport 694</code>
<code> </code><code>ucast eth0 192.168.11.20</code>
<code> </code><code>auto_failback on</code>
<code> </code><code>node master</code>
<code> </code><code>node slave</code>
<code> </code><code>ping</code> <code>192.168.11.1</code>
<code> </code><code>respawn hacluster </code><code>/usr/lib/heartbeat/ipfail</code>
解釋:
debugfile /var/log/ha-debug # debug檔案
logfile /var/log/ha-log #日志檔案
logfacility local0 #日志級别
keepalive 2 #2秒探測一次
deadtime 30 #30秒探測不到,認為死掉
warntime 10 #10秒探測不到,告警至日志檔案中
initdead 60 #為對方伺服器重新開機預留60秒時間
udpport 694 #心跳線通信端口為694
ucast eth0 192.168.11.20 #配置對方的ip,也可以使用bcast廣播形式,去搜尋ip,這裡直接指
#定ip(實驗中心跳網卡和主從網卡共用,不怎麼安全,實際線上,最好
#給心跳獨立網卡或用序列槽通信)
auto_failback on #當主恢複,從自動下線
node master #兩個節點
node slave #兩個節點
ping 192.168.11.1 #仲裁ip,仲裁主從誰線上
respawn hacluster /usr/lib/heartbeat/ipfail #以hacluster身份去運作腳本後面的腳本,該腳本使用者
#檢測網絡連通性,heartbeat自帶的斷網切換的工具-ipfail,
#64位系統位置/usr/lib64/heartbeat/ipfail
5. 将主上編輯好的三個配置檔案拷貝至從上
指令 cd /etc/ha.d/
指令 scp authkeys haresources ha.cf slave:/etc/ha.d/
6. 修改slave上的配置檔案/etc/ha.d/ha.cf
說明:修改為主的ip
指令 vim /etc/ha.d/ha.cf
内容:
<code> </code><code>ucast eth0 192.168.11.160</code>
7. master和slave上安裝nginx
指令 yum install -y nginx
8. 依次啟動master、slave上的heartbeat
說明:nginx 無需手動啟動;先啟動主上的heartbeat,再啟動從上heartbeat,等一段時間,主
上虛拟ip自動開啟,nginx也被拉起,從上nginx不會立即起來
指令 /etc/init.d/heartbeat start
ip addr; ifconfig ; ps aux |grep nginx
9. master、slave上修改nginx首頁
說明:先浏覽配置檔案cat /etc/nginx/nginx.conf-->找到"include /etc/nginx/conf.d/*.conf;"- ->cd /etc/nginx/conf.d/ -->cat default.conf-->找到“root /usr/share/nginx/html”,重定向首頁檔案
主:echo "11111111111master" > /usr/share/nginx/html/index.html
從:echo "22222222222222slave" > /usr/share/nginx/html/index.html
10. 測試
a. master上器上插入一條iptbales防火牆規則,禁用ping
iptables -A INPUT -p icmp -j DROP
結果:主、從上檢視日志tail -f /var/log/ha-log,發現主heartbeat挂掉;從heartbeat起來接管,并拉起nginx
b. master上器上去掉icmp規則,重新開啟ping
iptbales -D INPUT -p icmp -j DROP
結果: 從上釋放掉了流動ip,主自動啟用,網頁重新回到主機器上主機器上;停掉heartbeat, /etc/init.d/heartbeat stop,也會自動切換到從機器
本文轉自 huangzp168 51CTO部落格,原文連結:http://blog.51cto.com/huangzp/1885185,如需轉載請自行聯系原作者