RHEL6上配置基於heartbeat+lvs+ldirectord的高可用服務

 

Heartbeat+ipvsadm+ldirectord組建linux高可用集羣html

 

1. 系統環境:node

RHEL6,組建選擇base--默認,developmenttools—選擇全部可選包。linux

對外虛擬IP:192.168.50.179web

網關:192.168.50.254緩存

負載節點:(eth0爲外網卡)master-192.168.50.170bash

負載心跳:(eth1爲心跳檢查)192.168.94.10服務器

備用節點:(eth0爲外網卡)backup-192.168.50.171網絡

備用節點心跳:(eth1爲心跳檢查)192.168.94.11session

真實節點1rs1-192.168.50.172app

真實節點2rs2-192.168.50.173

確保hostname==uname –n

編輯各機器/etc/hosts 文件,添加解析:

192.168.50.170 master

192.168.50.171 backup

192.168.50.172 rs1

192.168.50.173rs2

2.所需軟件

Heartbeat2.1.3ipvsadm1.24

安裝heartbeat時須要:

Libnet  (此包系統中未包含須要下載,我下載的是libnet-1.1.5.tar.gz

glib2-devel (系統自帶)

libxml2-devel (系統自帶)

bzip2-devel (系統自帶)

Ldirector服務須要的依賴幾個perl包(CPAN下載):

Socket6-0.23.tar.gz

IO-Socket-INET6-2.65.tar.gz

MailTools-1.74.tar.gz

依次安裝以上各包之後應該能夠安裝heartbeat了。官網最新版的heartbeat3.0已分紅3個組件包,安裝時每一個包時都須要去INTERNET驗證大量XML文件,耗費的時間驚人,依賴包也比較多。Heartbeat2.1.4版本在rhe6上安裝時須要大量依賴包,並且make install時報錯,在網上也沒找到相關的解決方案,因此退而求其次選擇了heartbeat2.1.3版,此版安裝時比較順利,下面記錄了個人安裝過程。其中大部分配置都能從網上找到,我也只是借來用的,此文檔的目的只是本身備忘,以及提示一下在安裝過程當中和我有一樣錯誤的菜鳥們。涉及到版權問題還請見諒。並給我留言。

3.安裝ipvsadm

#ln -s/usr/src/kernels/`uname -r` /usr/src/linux

#tar zxvfipvsadm-1.24.tar.gz

#cdipvsadm-1.24

#make;make install

---------------------------------------------------------------------------------------------------------------------------

4. 安裝heartbeat

[root@mastersrc]# tar zxvf heartbeat-2.1.3.tar.gz

[root@mastersrc]# cd heartbeat-2.1.3

[root@masterheartbeat-2.1.3]# ./ConfigureMe configure

[root@masterheartbeat-2.1.3]#groupadd haclient

[root@masterheartbeat-2.1.3]# useradd -g haclient hacluster

[root@masterheartbeat-2.1.3]# ./ConfigureMe make --enable-fatal-warnings=no

[root@masterheartbeat-2.1.3]# make install

[root@masterheartbeat-2.1.3]# cp doc/{authkeys,haresources,ha.cf} /etc/ha.d/

[root@master heartbeat-2.1.3]# cp ldirectord/ldirectord.cf /etc/ha.d/

配置heartbeat

-------------------------------------------------------------------------------------

1)編輯authkeys文件:

[root@master heartbeat-2.1.3]# sed -e /^#/d /etc/ha.d/authkeys  

auth 3

3 md5 Hello!

[root@masterheartbeat-2.1.3]#chmod 600 /etc/ha.d/authkeys

--------------------------------------------------------------------------------------

2)編輯ha.cf文件:

 [root@master heartbeat-2.1.3]# sed -e /^#/d /etc/ha.d/ha.cf

debugfile/var/log/ha-debug

logfile/var/log/ha-log

logfacility     local0

keepalive 2

deadtime 5

initdead 15   #此數值大於等於deadtime的兩倍

udpport 694

ucast eth1192.168.94.11

auto_failback on   #負載節點從新恢復正常後從備用節點自動接管

node    master

node    backup

crm no    #禁用crm

-----------------------------------------------------------------------------------------

 3)編輯haresources文件

 [root@masterheartbeat-2.1.3]# sed -e /^#/d /etc/ha.d/haresources 

master 192.168.50.179 cluster ldirectord 

#負載節點主機名 虛擬IP地址 要有heartbeat管理的程序

---------------------------------------------------------------------------------------------------------------------------

    4)編輯ldirectord文件

[root@masterheartbeat-2.1.3]# sed -e /^#/d /etc/ha.d/ldirectord.cf 

 

checktimeout=3

checkinterval=1

autoreload=yes

logfile="/var/log/ldirectord.log"

logfile="local0"

quiescent=no  #自動刪除down掉的真實節點

 

virtual=192.168.50.179:80

                real=192.168.50.172:80 gate

                real=192.168.50.173:80 gate  #此項在有更多真實節點時自由添加

                fallback=127.0.0.1:80 gate

                service=http

                scheduler=rr

                #persistent=600

                #netmask=255.255.255.255

                protocol=tcp

                checktype=negotiate

                checkport=80

                request="index.html"   #向真實服務器請求的網頁文件名

                receive="Test Page"    #從真實服務器返回的網頁內容,當檢測不到此網頁文件或返回的內容不正確時即認爲真實 節點down

                #virtualhost=www.x.y.z

5)編輯cluster腳本文件(此腳本要放在/etc/init.d)下

 [root@masterheartbeat-2.1.3]# vi /etc/init.d/cluster

#!/bin/bash

#description:start LVS of DirectorServer

#Writtenby qiu

# define used ip.

GW=192.168.50.254

VIP=192.168.50.179

RIP1=192.168.50.172

RIP2=192.168.50.173

./etc/rc.d/init.d/functions

 

case"$1" in

start)

#Clear all ipvsadmrules.

/sbin/ipvsadm -C

#set lvs vip fordr

/sbin/ipvsadm--set 30 5 60

/sbin/ifconfigeth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up

/sbin/route add-host $VIP dev eth0:0

/sbin/ipvsadm -A-t $VIP:80 -s rr -p 3  #-p選項設置session保持時間

/sbin/ipvsadm -a-t $VIP:80 -r $WEB_RIP1:80 -g -w 1

/sbin/ipvsadm -a-t $WEB_VIP:80 -r $RIP2:80 -g -w 1

touch/var/lock/subsys/ipvsadm > /dev/null 2>&1

# set Arp 當一臺VS接管LVS服務時,可能會網絡不通,這是由於路由器的MAC緩存表裏沒法及時刷新MAC.關於vip這個地址的MAC地址仍是替換的VSMAC,解決方法是使用arping命令.

/sbin/arping -Ieth0 -c 5 -s $VIP $GW > /dev/null 2>&1

;;

 

stop)

/sbin/ipvsadm -C

/sbin/ipvsadm -Z

ifconfig eth0:0down

route del $VIP

rm -rf /var/lock/subsys/ipvsadm> /dev/null 2>&1

/sbin/arping -Ieth0 -c 5 -s $VIP $GW

echo "ipvsadmstoped"

;;

 

status)

if [ ! -e/var/lock/subsys/ipvsadm ];then

echo "ipvsadmis stoped"

status)

if [ ! -e/var/lock/subsys/ipvsadm ];then

echo "ipvsadmis stoped"

exit 1

else

ipvsadm -ln

echo"..........ipvsadm is OK."

fi

;;

restart

$0 stop

$0 start

;;

*)

echo "Usage:$0{start|stop|restart|status}"

exit 1

esac

exit 0

 

[root@masterheartbeat-2.1.3]#chmod 755 /etc/init.d/cluster

[root@masterheartbeat-2.1.3]#chkconfig -add heartbeat

至此負載節點已經安裝配置完畢,把一下文件拷貝到相應備用節點的目錄:

 authkeys,ha.cf,haresources,ldirectord.cf,cluster

備用幾點只須要改動ha.cf文件中 「ucast eth1 192.168.94.11」中的ip地址爲負載節點的eth1所對應的ip便可。其餘配置徹底相同。

 

5.真實節點的相關操做

1)在真實節點上安裝lamp環境,或者其餘應用程序,在制定存放網頁的目錄創建index.html文件,確保內容爲「Test Page

2)在/etc/init.d下創建腳本文件:

#cat /etc/init.d/lvs_rs

#!/bin/sh

# chkconfig: 234572 08

# description:Config realserver lo:0 port and apply arp patch

VIP=192.168.50.179

./etc/rc.d/init.d/functions

case $1 in

            start)

            echo "lo:0 port starting"

            echo "0">/proc/sys/net/ipv4/ip_forward

            /sbin/ifconfig lo:0 $VIP broadcast $VIPnetmask 255.255.255.255 up

            /sbin/route add -host $VIP dev lo:0

            echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore

            echo"2" > /proc/sys/net/ipv4/conf/lo/arp_announce

            echo"1" > /proc/sys/net/ipv4/conf/all/arp_ignore

            echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

            sysctl -p

            ;;

            stop)

            echo "lo:0 port closing"

            ifconfig lo:0 down

            echo "1" > /proc/sys/net/ipv4/ip_forward

            echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce

            ;;

            *)

            echo "Usage: $0 {start|stop}"

            exit 1

esac

 

 

    添加爲系統啓動服務:

    #chkconfig –add lvs_rs

    #chkconfig –level345 on

6.測試

在負載節點上運行  watch ipvsadm -ln  命令,此命令現實lvs的轉發列表。

交替停掉或者所有停掉兩臺真實節點上的httpd服務,查看轉發列表的變化。

停掉負載節點,查看備用節點是否接管任務。訪問虛擬IP看是否能訪問到兩臺真實節點的web服務。

7.維護

主要是查看 /var/log/messages;/var/log/ha-log;/var/log/ldirectord.log 中相關的日誌信息作出調整

相關文章
相關標籤/搜索