天天看點

Linux系統架構(LB—HA叢集)-HA叢集配置

HA叢集配置

準備兩台裝置,分别為主從

主:

[root@client ~]# ifconfig

eth0       inet addr:192.168.137.21

從:

[root@Client ~]# ifconfig

eth0      inet addr:192.168.137.23

先開始配置

主上:

[root@client ~]# hostname master

[root@client ~]# iptables -F

[root@client ~]# setenforce 0

[root@client ~]# vim /etc/hosts

192.168.137.21 master

192.168.137.23 slave

root@client ~]# yum list |grep heartbeat

無顯示,先安裝擴充源

[root@client~]#wget www.lishiming.net/data/p_w_upload/forum/epel-release-6-8_32.noarch.rpm

[root@client ~]# rpm -ivh epel-release-6-8_32.noarch.rpm

[root@client ~]# yum  install -y heartbeat  libnet

從上類似操作:

[root@Client ~]# hostname slave

[root@Client ~]# iptables -F

[root@Client ~]# setenforce 0

[root@Client ~]# vim /etc/hosts

[root@Client~]#wget www.lishiming.net/data/p_w_upload/forum/epel-release-6-8_32.noarch.rpm

[root@Client ~]#  rpm -ivh epel-release-6-8_32.noarch.rpm

[root@Client ~]# yum  install -y heartbeat  libnet

軟體安裝完成,現配置檔案

[root@client ~]# cd /usr/share/doc/heartbeat-3.0.4/

[root@client heartbeat-3.0.4]# ls

[root@client heartbeat-3.0.4]# cp authkeys ha.cf haresources /etc/ha.d/

[root@client heartbeat-3.0.4]# cd /etc/ha.d/

[root@client ha.d]# vim authkeys

auth 3

#1 crc

#2 sha1 HI!

3 md5 Hello!

[root@client ha.d]# chmod 600 authkeys

[root@client ha.d]# vim haresources              //加入

master 192.168.137.100/24/eth0:0 nginx

// master (主的主機名)192.168.137.100/24/eth0:0(虛拟的IP,指定網段,網卡eth0:0) nginx(哪些資源做主從)

此不可不做,此不為一個網卡配置多個IP位址方法:

[root@client ha.d]# cd /etc/sysconfig/network-scripts/

[root@client network-scripts]# ls

[root@client network-scripts]# cp ifcfg-eth0 ifcfg-eth0\:1

[root@client network-scripts]# vim ifcfg-eth0\:1

DEVICE=eth0:1

HWADDR=00:0C:29:6A:A5:9B

TYPE=Ethernet

UUID=4e9798c5-cbbc-45d1-9483-51d8c50643f8

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

IPADDR=192.168.137.109

NETMASK=255.255.255.0

#GATEWAY=192.168.137.1

[root@client network-scripts]# /etc/init.d/network restart

[root@client network-scripts]# ifconfig

eth0      inet addr:192.168.137.21

eth0:1    inet addr:192.168.137.109

[root@client network-scripts]# cd -

/etc/ha.d

[root@client ha.d]# vim 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 eth0 192.168.137.23

 auto_failback on

 node    master

 node    slave

 ping 192.168.21.1

 respawn hacluster /usr/lib/heartbeat/ipfail

[root@client ha.d]# scp authkeys haresources ha.cf slave:/etc/ha.d/

//将這幾份檔案拷貝到從slave裝置上

[root@client ha.d]# yum install -y  nginx

先配置從:

[root@Client init.d]# cd /etc/ha.d/

[root@Client ha.d]# cat authkeys           //該檔案不用改

[root@Client ha.d]# cat haresources        //該檔案也不用改

[root@Client ha.d]# vim ha.cf

ucast eth0 192.168.137.21                 //改成對端IP

[root@Client ha.d]#  yum install -y  nginx

測試:

啟動heartbeat先啟動主,再啟動從

[root@client ha.d]# /etc/init.d/heartbeat start

[root@client ha.d]# ps aux |grep nginx                    //nginx還未啟動

[root@client ha.d]# ip add

inet 192.168.137.100/24 brd 192.168.137.255 scope global secondary eth0:0

//虛拟IP已有

[root@client ha.d]# ps aux |grep nginx

root      5309  0.0  0.1  15648  1512 ?        Ss   14:35   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf

nginx     5311  0.0  0.1  15804  1960 ?        S    14:35   0:00 nginx: worker process

//nginx已啟動

[root@Client ha.d]# /etc/init.d/heartbeat start

[root@Client ha.d]# ip add

//虛拟IP未啟動

[root@Client ha.d]# ps aux |grep nginx

//nginx也未啟動

[root@client ha.d]# echo "master" >/usr/share/nginx/html/index.html

//将網站預設頁面改成master,友善測試區分

windows用戶端通路192.168.137.100頁面為master

[root@client ha.d]# curl 192.168.137.100

master

從上:

[root@Client ha.d]# echo "slave" > /usr/share/nginx/html/index.html

現模拟主裝置當機了,看是否能切換到從上

方法使主上禁ping,模拟當機

[root@client ha.d]# iptables -A INPUT -p icmp -j DROP

//禁ping規則

[root@client ha.d]# tail /var/log/ha-log

[root@client ha.d]# tail -f /var/log/ha-log

[root@master ha.d]# ifconfig

eth0:0已釋放

[root@master ha.d]# ps aux |grep nginx

//無nginx程序

從上檢視:

[root@Client ha.d]# less /var/log/ha-log

IPaddr(IPaddr_192.168.137.100)[3276]:   2015/07/25_19:15:47 INFO: Bringing device eth0 up

ResourceManager(default)[3115]: 2015/07/25_19:15:47 info: Running /etc/init.d/nginx  start

//eth0啟動,nginx啟動

[root@Client ha.d]# ifconfig

eth0:0    inet addr:192.168.137.100

//虛拟IP已啟動

nginx     4010  0.0  0.2  15804  2324 ?     S    20:02   0:00 nginx: worker process

//nginx啟動了

windows用戶端通路:192.168.137.100出現slave頁面

[root@Client ha.d]# curl 192.168.137.100

slave

主上打開禁ping規則

[root@master ha.d]#  iptables -D INPUT -p icmp -j DROP

//虛拟IP釋放

nginx程序停止

windows用戶端通路192.168.137.100出現master頁面

當主上停止heartbeat時,網站切換到從上

[root@master ha.d]# /etc/init.d/heartbeat stop

window用戶端通路192.168.137.100出現slave頁面