Centos5.5lvs+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是不是打开正常