Centos5.5下lvs+heartbeat+ldirectord

Centos5.5 lvs+heartbeat+ldirectord
高可靠性集羣
目標:實現業務的高可靠性
實驗環境:4centos5.5(32bit)
撰寫人:hx10
日期:2010/6/1  hangzhou
Blog:http://hi.baidu.com/hx10
環境以下:
lvs-master:10.80.11.245
lvs-backup:10.80.11.246
vip:10.80.11.240
web1:10.80.11.243
web2:10.80.11.244
netmask:255.255.255.0
gateway:10.80.11.1
 
第一步、系統設置
[root@lvs-master conf]# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 lvs-master        localhost.localdomain localhost
::1              localhost6.localdomain6 localhost6
10.80.11.245   lvs-master
10.80.11.246   lvs-backup
lvs-backup上一樣設置
接下來關掉一些沒必要要的系統服務
chkconfig bluetooth off
chkconfig firstboot off
chkconfig cups off
chkconfig ip6tables off
chkconfig iptables off
chkconfig isdn off
chkconfig kudzu off
chkconfig sendmail off
chkconfig smartd off
chkconfig autofs off
service bluetooth stop
service firstboot stop
service cups stop
service ip6tables stop
service iptables stop
service isdn stop
service kudzu stop
service sendmail stop
service smartd stop
service autofs stop
 
同步系統時間
#ntpdate time.nist.gov
 
第二步、安裝軟件(2lvs須要安裝一樣的軟件和腳本,惟一的區別是ha.cf有點不一樣)
#yum -y install heartbeat heartbeat-ldirectord ipvsadm
#yum -y install heartbeat
檢查下是否徹底安裝如下包
[root@lvs-master /]# rpm -qa |grep heartbeat
heartbeat-stonith-2.1.3-3.el5.centos
heartbeat-ldirectord-2.1.3-3.el5.centos
heartbeat-gui-2.1.3-3.el5.centos
heartbeat-2.1.3-3.el5.centos
heartbeat-devel-2.1.3-3.el5.centos
heartbeat-pils-2.1.3-3.el5.centos
[root@lvs-master /]# rpm -qa |grep ipvsadm
ipvsadm-1.24-10
#rpm -q heartbeat -d  查看Heartbeat安裝到那裏
#mkdir /etc/ha.d/conf 
#cp /usr/share/doc/heartbeat-2.1.3/authkeys /etc/ha.d
#cp /usr/share/doc/heartbeat-2.1.3/ ha.cf /etc/ha.d
#cp /usr/share/doc/heartbeat-2.1.3/ haresources /etc/ha.d
#cp /usr/share/doc/heartbeat-ldirectord-2.1.3/ ldirectord.cf  /etc/ha.d/conf
 
第三步、修改配置文件
#cd /etc/ha.d
[root@lvs-master ha.d]# cat authkeys |grep -v ^#
auth 1
1 crc
#chmod 600 authkeys
[root@lvs-master ha.d]# cat ha.cf |grep -v ^#
debugfile /var/log/ha-debug
logfile       /var/log/ha-log
logfacility local0
keepalive 2
deadtime 30
warntime 10
initdead 120
udpport    694
bcast        eth0          # Linux
ucast eth0 10.80.11.246
auto_failback on
node         lvs-master
node         lvs-backup
ping 10.80.11.1
respawn hacluster /usr/lib/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster
 
注:ucast eth0 10.80.11.246  是從本地(lvs-master)eth0網卡去ping lvs-backupIP,在lvs-backup服務器上,須要把這個IP改爲lvs-masterIP,這是2LVS惟一區別
[root@lvs-master ha.d]# cat haresources |grep -v ^#
lvs-master 10.80.11.240/24/eth0/10.80.11.255 ldirectord::ldirectord.cf LVSSyncDaemonSwap::master
注:
主節點的主機名   vip/子網掩碼/本地eth0/廣播地址
[root@lvs-master ha.d]# cat conf/ldirectord.cf |grep -v ^#
checktimeout=20
checkinterval=5
autoreload=no
logfile="/var/log/ldirectord.log"
quiescent=no
 
virtual=10.80.11.240:80
         fallback=127.0.0.1:80
         real=10.80.11.243:80 gate 1
         real=10.80.11.244:80 gate 1
         service=http
         request="lvscheck.html"
         receive="lvscheck"
         scheduler=rr
         protocol=tcp
         checktype=negotiate
         #persistent=300
必定要注意格式,第一次作實現,就是由於所有左對齊,一直出問題
quiescent=no web服務器其中一臺down機,能夠自動從lvs刪除,這樣訪問vip就不會輪訓到down機的那個機器
persistent=300是會話保持之間,就是300秒一直訪問web1,下個300秒一直訪問web2
fallback=127.0.0.1:80的意思是若是lvs中止,會打開lvs服務器的http頁面,能夠作一個提示頁面
web1web2上分別建立lvscheck.html文件,內容爲lvscheck,這是健康檢查的標準
下面在lvs-backup上進行系統配置和安裝相應軟件,準備好進行下一步
[root@lvs-master ha.d]# scp authkeys ha.cf haresources root@10.80.11.246:/etc/ha.d
root@10.80.11.246's password:
[root@lvs-master ha.d]# scp conf/ldirectord.cf root@10.80.11.246:/etc/ha.d/conf
root@10.80.11.246's password:
把這4個配置文件拷貝到lvs-backup
而後在lvs-backup上把ha.cf中的
ucast eth0 10.80.11.246
這裏的ip改爲10.80.11.245
 
第四步、修改web服務器的配置
[root@web1 ~]# vim /etc/init.d/realserver.sh
 
#!/bin/bash
#ignore arp script
VIP1=10.80.11.240
#*************************************************************
case $1 in
start)
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
#Add ip alias for lo
/sbin/ifconfig lo:0 $VIP1 netmask 255.255.255.255 up
/sbin/route add -host $VIP1 dev lo:0
;;
stop)
echo "0">/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0">/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0">/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0">/proc/sys/net/ipv4/conf/all/arp_announce
/sbin/ifconfig lo:0 $VIP1 netmask 255.255.255.255 down
/sbin/route del -host $VIP1
;;
status)
iothere=`/sbin/ifconfig lo:0|grep $VIP1`
iorthere=`/bin/netstat -rn|grep $VIP1`
if [ ! "$iothere" -o ! "$iorthere" ];then
    echo "LVS Real Server is STOP"
    else
    echo "LVS Real Server is ALIVE"
fi
;;
*)
echo "Usage:$0 start|stop|status"
;;
esac
紅色標記部分是vip地址
#chmod 755 /etc/init.d/realserver.sh
#sh /etc/init.d/realserver.sh start
#vim /etc/rc.local  加入下一行
/etc/init.d/realserver.sh start
web2上建立一樣的腳本並直接,加入自啓動
第五步、啓動服務並測試
lvs-masterlvs-backu上分別啓動服務
#service heartbeat start
觀察
[root@lvs-master ha.d]# watch ipvsadm -L –n
而後中止lvs-master服務,看看lvs-backup是否是接管
停掉web1服務,看vip是否是打開正常
相關文章
相關標籤/搜索