HA 即 (high available)高可用,又被叫做雙機熱備, 常用實作高可用的開源軟體有heartbeat和keepalived,其中keepalived有負載均衡的功能。
<a href="http://s1.51cto.com/wyfs02/M02/87/BA/wKioL1fgUHqStuSFAAHn-ygfo50631.png" target="_blank"></a>
下面我們使用heartbeat來做HA叢集,并且把nginx服務作為HA對應的服務。
試驗準備:
兩個機器, 都是centos6.5,網卡eth0 ip如下:
aming 192.168.31.166
aming1 192.168.31.100
兩個eth1 ip如下:
aming 192.168.21.166
aming1 192.168.21.100
<a href="http://s3.51cto.com/wyfs02/M00/87/BE/wKiom1fgUK6gnb1lAAW5oM3FmtM091.png" target="_blank"></a>
下面操作1-5都是在兩個機器上操作
1. hostname 設定好,分别為aming 和 aming1
2. 關閉防火牆 iptables -F;
關閉selinux: setenforce 0
3. vi /etc/hosts // 增加内容如下:
192.168.31.166 aming
192.168.31.100 aming1
4. 安裝epel擴充源:
rpm -ivh 'http://www.lishiming.net/data/attachment/forum/epel-release-6-8_32.noarch.rpm'
yum install -y epel-release
5. 兩個機器都安裝heartbeat / libnet
yum install -y heartbeat* libnet nginx
6. 主上(aming)配置
cd /usr/share/doc/heartbeat-3.0.4/
cp authkeys ha.cf haresources /etc/ha.d/
cd /etc/ha.d
vi authkeys //加入或更改為auth 3
3 md5 Hello!
<a href="http://s5.51cto.com/wyfs02/M01/87/BE/wKiom1fgUN2Ctd4kAABMUfX3Phs274.png" target="_blank"></a>
chmod 600 authkeys
vi haresources //加入
aming 192.168.31.110/24/eth0:0 nginx
aming 是主伺服器的名字, 192。168。31。110是虛拟出來的IP
<a href="http://s2.51cto.com/wyfs02/M00/87/BA/wKioL1fgUQywD6GCAAB2bAhrGh4542.png" target="_blank"></a>
nginx因為在etc/init.d下
虛拟IP要寫對外提供服務的IP段
vi ha.cf //改為如下内容:
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 30
warntime 10
initdead 60
udpport 694
ucast eth1 192.168.21.100
auto_failback on
node aming
node aming1
ping 192.168.21.1
respawn hacluster /usr/lib/heartbeat/ipfail
<a href="http://s3.51cto.com/wyfs02/M00/87/BA/wKioL1fgUVzSSSlXAABYjAjyALE185.png" target="_blank"></a>
這些要打開
<a href="http://s3.51cto.com/wyfs02/M02/87/BE/wKiom1fgUXXieEW2AAAmtXbw-Mo192.png" target="_blank"></a>
這裡寫IP也可以寫網卡的名字(對方的)不是業務網卡
<a href="http://s5.51cto.com/wyfs02/M01/87/BE/wKiom1fgUZKSfzvPAAA5C507W00228.png" target="_blank"></a>
檢測網絡聯通性
64位系統可能要寫成 ls /usr/lib64/heartbeat/ipfail
<a href="http://s2.51cto.com/wyfs02/M01/87/BB/wKioL1fgUcKxeFaaAAA19s1sEUc223.png" target="_blank"></a>
<a href="http://s2.51cto.com/wyfs02/M01/87/BB/wKioL1fgUcPgKgrzAABIWS4GsnE612.png" target="_blank"></a>
這裡是以那個使用者的身份
<a href="http://s5.51cto.com/wyfs02/M02/87/BE/wKiom1fgUfHBy-7TAAB0fjV77mQ127.png" target="_blank"></a>
這個IP是個仲裁
<a href="http://s3.51cto.com/wyfs02/M02/87/BB/wKioL1fgUhuDq8OkAAAG7s-xYqk411.png" target="_blank"></a>
主節點,從節點
從上:
<a href="http://s4.51cto.com/wyfs02/M00/87/BE/wKiom1fgUjuwqBqIAAAkANq8ee4805.png" target="_blank"></a>
這裡寫IP也可以寫網卡的名字(對方的)
less /var/log/ha-log 日志檔案
7. 把主上的三個配置拷貝到從上:
cd /etc/ha.d/
scp authkeys ha.cf haresources aming1:/etc/ha.d/
<a href="http://s5.51cto.com/wyfs02/M02/87/BB/wKioL1fgUmvhy7HJAAAc3EJoKPs725.png" target="_blank"></a>
8. 到從上(aming1) 編輯ha.cf
vi /etc/ha.d/ha.cf //隻需要更改一個地方
ucast eth1 192.168.21.100 改為 ucast eth1 192.168.21.166
9. 啟動heartbeat :
先主,後從
service heartbeat start
/etc/init.d/heartbeat start
10. 檢查測試
ifconfig 看是否有 eth0:0
ps aux |grep nginx 看是否有nginx程序
11. 測試1
主上故意禁ping
iptables -I INPUT -p icmp -j DROP
12. 測試2
主上停止heartbeat服務
service heartbeat stop
13. 測試腦裂
主和從上都down掉eth1網卡
ifdown eth1
腦列就是伺服器的第二張網卡,全部down掉
<a href="http://s3.51cto.com/wyfs02/M01/87/BE/wKiom1fgUqaThNYHAAD_iD2fpM4361.png" target="_blank"></a>
腦列後,網卡接通後,主跟從都會擷取不到資源,我們把
主上的hearbent重新開機下
<a href="http://s3.51cto.com/wyfs02/M00/87/BE/wKiom1fgUtfgmuseAAKF--8YKsI790.png" target="_blank"></a>
<a href="http://s3.51cto.com/wyfs02/M00/87/BB/wKioL1fgUtfjxPmaAABhfOuJNYs486.png" target="_blank"></a>
<a href="http://s1.51cto.com/wyfs02/M00/87/BB/wKioL1fgUxbRGGMSAAFQIhhsmfM670.png" target="_blank"></a>
本文轉自 15816815732 51CTO部落格,原文連結:http://blog.51cto.com/68686789/1854288