天天看點

HA高可用叢集

HA 即 (high available)高可用,又被叫做雙機熱備, 常用實作高可用的開源軟體有heartbeat和keepalived,其中keepalived有負載均衡的功能。

<a href="http://s1.51cto.com/wyfs02/M02/87/29/wKioL1fWZ2-x1FKBAAHn-ygfo50221.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://s5.51cto.com/wyfs02/M01/87/29/wKioL1fWZ7XzJBtUAANsgp-lXn8942.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://s1.51cto.com/wyfs02/M02/87/2C/wKiom1fWaCqDe2lGAABMUfX3Phs502.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://s5.51cto.com/wyfs02/M00/87/2C/wKiom1fWaIfAhJp9AAB2bAhrGh4662.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://s2.51cto.com/wyfs02/M02/87/2A/wKioL1fWawjjo5fYAAAG7s-xYqk047.png" target="_blank"></a>

主節點,從節點

<a href="http://s3.51cto.com/wyfs02/M01/87/2A/wKioL1fWagmRZ7HJAABgcQvKOrU634.png" target="_blank"></a>

<a href="http://s1.51cto.com/wyfs02/M00/87/2A/wKioL1fWak3DpvE-AAAvuJ8iJZY804.png" target="_blank"></a>

<a href="http://s2.51cto.com/wyfs02/M02/87/2C/wKiom1fWapTTn3tKAADVqoRPvmI046.png" target="_blank"></a>

<a href="http://s4.51cto.com/wyfs02/M02/87/2C/wKiom1fWas2BqsHSAACWnOYM6Fo961.png" target="_blank"></a>

從上:

<a href="http://s2.51cto.com/wyfs02/M01/87/2A/wKioL1fWa12RghT5AAAkANq8ee4306.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://s4.51cto.com/wyfs02/M00/87/2C/wKiom1fWa9eSvivYAAAc3EJoKPs688.png" target="_blank"></a>

8. 到從上(centos2) 編輯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程序

測試2

主上停止heartbeat服務

service heartbeat stop 

<a href="http://s2.51cto.com/wyfs02/M02/87/2C/wKiom1fWbTjiWIdWAAHYzB39nuU972.png" target="_blank"></a>

<a href="http://s5.51cto.com/wyfs02/M00/87/2C/wKiom1fWbXTQsUCkAACMhOdbTlc009.png" target="_blank"></a>

<a href="http://s2.51cto.com/wyfs02/M01/87/2C/wKiom1fWbJLh88VyAABhfOuJNYs303.png" target="_blank"></a>

<a href="http://s1.51cto.com/wyfs02/M00/87/2A/wKioL1fWbcjB_3-oAAFi2YACRyg451.png" target="_blank"></a>

本文轉自 15816815732 51CTO部落格,原文連結:http://blog.51cto.com/68686789/1852043

繼續閱讀