Heartbeat实现LVS的故障转移

LVS是1998年5月由章文嵩博士发起和领导的优秀的集群解决方案,许多商业的集群产品,比如RedHat的Piranha,TurboLinux公司的Turbo Cluster等,都是基于LVS的核心代码的。在现实的应用中,LVS得到了大量的部署。
1.3 用Heartbeat实现LVS的高可用性
LVS可以对Apache等服务实现负载均衡,LVS与Ldirectord搭配使用也可以实现Apache等服务的故障转移,但是LVS自己本身没有冗余机制,因此需要借助第三方工具Heartbeat来实现。
1.3.1 Heartbeat简介
Heartbeat最核心包括两个部分:心跳监测部分和资源接管部分。心跳监测可以通过网络链路和串口进行,而且支持冗余链路,它们之间相互发送报文来告诉对方自己当前的状态,如果在指定的时间内未受到对方发送的报文,那么就认为对方失效,这时需启动资源接管模块来接管运行在对方主机上的资源或者服务。
1.3.2 Heartbeat安装
建立用户
groupadd haclient
useradd hacluster -g haclient
先安装libnet
rpm -ivh libnet-1.1.2.1-2.1.i386.rpm
再安装Heartbeat
tar jxvf Heartbeat-STABLE-2-1-STABLE-2.1.4.tar.bz2
cd Heartbeat-STABLE-2-1-STABLE-2.1.4
./ConfigureMe install --enable-all
1.3.3 配置LVS Drector的冗余热备
假设服务器A,B的公网IP分别是:
A 192.168.100.10
B 192.168.100.4
集群虚IP: 192.168.100.200
1.3.3.1 设置服务器A,B的hostname为test10及test4
如不是请修改/etc/sysconfig/network的HOSTNAME部分,并执行
#hostname test10 以立即生效。
在/etc/hosts加入两行:
192.168.100.10 test10
192.168.100.4 test4
1.3.3.2 修改服务器A,B的/etc/sysctl.conf文件,加入如下5行,并执行
#sysctl -p 以立即生效。

net.ipv4.ip_forward = 1
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.eth0.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.eth0.arp_announce = 2
1.3.3.3 以root进入服务器A,执行以下配置:
在/etc/init.d/heartbeat中的#!/bin/sh后插入一行:
ifconfig lo:100 192.168.100.200 netmask 255.255.255.255 up

#chkconfig heartbeat --level 35 on
#cd /etc/ha.d 进入集群配置文件目录
#vi authkeys 创建集群认证文件
auth 3
3 md5 HA_2009

#chmod 600 authkeys 必要的操作

#vi ha.cf 创建集群节点文件
logfile /var/log/ha.log
#logfacility local0
keepalive 2
deadtime 30
warntime 10
initdead 80
mcast eth0 231.231.231.231 694 1 0
##有双网卡最好做个两台机器最好做交叉线连接,eth0 改eth1
#ucast eth0 192.168.100.4 #(test10直接指向对端ip,test4的化ucast eth0 192.168.100.10)
ping 192.168.100.2
auto_failback on
node test10
node test4
respawn hacluster /usr/lib/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster

集群中有两个节点test10及test4,通过多播IP通信(主要用于超过2个节点)
ping 192.168.100.2 网关ping检测
#vi /etc/ha.d/resource.d/vip.sh 创建我们自己的集群IP切换shell脚本
#!/bin/sh
case "$4" in
start)
ifconfig lo:100 down
ifconfig $1:100 $2 netmask $3 up
;;
stop)
ifconfig $1:100 down
ifconfig lo:100 $2 netmask 255.255.255.255 up
;;
esac
/etc/ha.d/resource.d/SendArp 192.168.100.200/eth0 start
最后一行通知其他连接过这台服务器的客户机VIP已经变更,MAC需要更新。

提供了LVS集群备用节点在回路地址监听集群虚IP以用于应用服务器。

#chmod +x resource.d/vip.sh

#vi haresources 创建集群资源文件
test10 ldirectord::ldirectord.cf LVSSyncDaemonSwap::master vip.sh::eth0::192.168.100.200::255.255.255.0

注:ldirectord是用于管理LVS配置实现Apache的故障转移,ldirectord.cf文件的创建,请查看《使用LVS+Ldirectord实现Web服务器的负载均衡及故障转移及其性能测试.doc》
1.3.3.4 在服务器B上进行第2、3步操作,scp也可。
1.3.4 测试
1、在服务器A、B上分别执行service heartbeat start 启动集群软件,
及执行service httpd start启动apache。

2、在服务器A、B上分别执行:
#echo "WebSite A">/var/www/html/index.html
#echo "WebSite B">/var/www/html/index.html

3、在服务器A、B上分别执行ifconfig
正常的话, 在服务器A上出现eth0:100 IP:192.168.100.200
在服务器B上出现l0:100 IP:192.168.100.200

在服务器A上执行service heartbeat stop,结果是:
在服务器A上出现lo0:100 IP:192.168.100.200
在服务器B上出现eth0:100 IP:192.168.100.200

在服务器A上执行service heartbeat start将恢复正常情况。

posted @ 2011-11-01 10:02  wwh  阅读(608)  评论(0编辑  收藏  举报