凝望长空 发表于 2011-11-02 09:22

LVS+Heartbeat实施案例及经验分享

本帖最后由 凝望长空 于 2011-11-02 09:25 编辑

LVS+Heartbeat实施案例及经验分享







作者:NetSeek http://www.linuxtone.org(IT运维专家网|集群架构|性能调优)
欢迎转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明.


一.基本介绍

背景描述
某大型商业网站由于业务量的发展非常迅速,目前的网站架构根本无法缓解用户访问压力及单点故障。急需一套高可用的负载均衡集群解决方案,http://www.linuxTone.org(IT运维专家网)根据这家商业网站在的状况,部署了一套负载均衡及高可用的集群方案及相关优化,项目实施以后该商业网站得WEB压力得到了很好的解决,访问速度大幅度提升.以下是相关配置文档,拿出来与朋友们分享希望对更多想了解LVS/HA的朋友有帮助。

本文特色
1.介绍LVS+Heartbeat两种案装方法
2.采用heartbeat 2.x style的配置(使用cib.xml)
3.测试日志分析及相关技巧
4.两个在真实案例中的经验分享

二.网络拓朴图及相关描述


1.主机名:(/etc/hosts)
192.168.1.102primarylb.linuxtone.org

•192.168.1.103backuplb.linuxtone.org
复制代码2.VIP 192.168.1.106
   Realserver1 192.168.1.104
   Realserver2 192.168.1.1053.Heartbeat心跳线
a)使用一根串口直连线缆(serial null modem cable)Heartbeat
b)eth1:10.0.0.110.0.0.2 (交叉线直连Heartbeat)

三.安装Heartbeat1.通过Yum安装Heartbeat
#yuminstall heartbeat-*

•//安装HA

•#yuminstall heartbeat-ldirectord

•//安装ldirectord实现LVS的高可用性

•# rpm-qa|grep heartbeat复制代码//查看安装相关的包heartbeat-stonith-2.1.3-3.el5.centos

•heartbeat-devel-2.1.3-3.el5.centos

•heartbeat-pils-2.1.3-3.el5.centos

•heartbeat-2.1.3-3.el5.centos

•heartbeat-ldirectord-2.1.3-3.el5.centos

•heartbeat-gui-2.1.3-3.el5.centos复制代码#rpm -q heartbeat ?d
//查看Heartbeat相关doc目录
将相关配置文件拷贝到/etc/ha.d目录下
# cp /usr/share/doc/heartbeat-2.1.3/ha.cf/etc/ha.d/

•# cp /usr/share/doc/heartbeat-2.1.3/authkeys/etc/ha.d/

•# cp /usr/share/doc/heartbeat-2.1.3/haresources/etc/ha.d复制代码2. 编译安装Heartbeat
下载相关软件包:
http://linux-ha.org/DownloadSoftware
http://www.packetfactory.net/libnet/dist/libnet.tar.gz
编译安装#tar-xvzf libnet.tar.gz

•#cdlibnet

•#./configure

•#make;makeinstall



•#groupadd-g 694 haclient

•#useradd-u 694 -g haclient hacluster



•# tar-xvzf heartbeat-2.1.3.tar.gz

•# cdheartbeat-2.1.3

•#./ConfigureMe configure

•#make;make install



•#cpdoc/ha.cf doc/haresources doc/authkeys /etc/ha.d/

•#cpldirectord/ldirectord.cf /etc/ha.d/复制代码【注意】
heartbeat软件包在RHEL或者CentOS安装盘中都能找到。所需软件包:heartbeat、libnet、net-snmp、net-snmp-libs、perl-Compress-Zlib、perl- HTML-Parser、perl-HTML-Tagset、perl-libwww-perl、perl-Mailinuxtoneools、perl- TimeDate、perl-URI、ipvsadm.以上软件包,有的在rhel5光盘中,有的要在网上搜索的。


四. 配置Heartbeat实现服务高可用性.
1.分别配置两台HA的主机名.#vi /etc/hosts
192.168.1.102primarylb.linuxtone.org

•192.168.1.103backuplb.linuxtone.org复制代码2./etc/ha.d/ha.cf 配置介绍
#cd /etc/ha.d
#cat ha.cf|grep -v ^#
logfile/var/log/ha-log

•logfacility

•local0

•keepalive2

•deadtime30

•warntime10

•initdead120

•udpport694

•baud

•19200

•serial

•/dev/ttyS0

•# Linux

•#ucasteth2 10.0.0.3

•ucasteth0 192.168.1.103

•auto_failbackon

•watchdog/dev/watchdog

•crm on

•nodeprimarylb.linuxtone.org

•nodebackuplb.linuxtone.org

•ping192.168.1.103

•ping_grouplbgroup 10.0.0.1 10.0.0.2

•ping_grouplbgroup 192.168.1.102 192.168.1.103

•respawnhacluster /usr/lib/heartbeat/ipfail

•apiauthipfail gid=haclient uid=hacluster复制代码3./etc/ha.d/authkeys
#vi authkeys
auth 1

•1 sha1Hey!复制代码#chmod 600 authkeys4./etc/ha.d/haresources
# cat haresources |grep -v"#"
primarylb.linuxtone.org lvsldirectord复制代码
5.建立软连接让HA自动接管lvs-dr.sh,ldirectord等资源.#ln -s /etc/init.d/lvs/etc/ha.d/resource.d///etc/init.d/lvs 存放LVS启动相关脚本

•#ln -s /usr/sbin/ldirectord/etc/ha.d/resource.d复制代码

6.采用Heartbeat2.x style,利用crm 来管理资源.#/usr/lib/heartbeat/haresources2cib.py-stout -c /etc/ha.d/ha.cf/etc/ha.d/haresources复制代码将生成的cib.xml文件在/var/lib/heartbeat/crm目录
【注意】
关于crm(ClusterResource Manager):crm资源配置方法是在新的版本中引入的,如果想使用该格式来配置资源(ip,服务,共享磁盘等),可以参看配置示例:http://www.linux-ha.org/v2/Examples/Simple 。
/var/lib/heartbeat/crm/cib.xml 文件的作用与/etc/ha.d/haresources相同,是二者择一的关系。
# cat/dev/null > haresources
//把haresources清空,采用crm来管理资源

7. /etc/ha.d/ldirectord.cf 配置
# GlobalDirectives

•checktimeout=3

•checkinterval=1

•fallback=127.0.0.1:80

•autoreload=yes

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

•#logfile="local0"

•emailalert="cnseek@gmail.com"

•emailalertfreq=3600

•emailalertstatus=all

•quiescent=no



•# Samplefor an http virtual service

•virtual=192.168.1.106:80



•real=192.168.1.104:80 gate



•real=192.168.1.105:80 gate





•#fallback=127.0.0.1:80gate



•#service=http



•#request="index.html"



•#receive="Test Page"



•#virtualhost=some.domain.com.au



•scheduler=wrr



•persistent=5



•#netmask=255.255.255.255



•protocol=tcp





•checktype=connect





•checkport=80





•#request="index.html"





•#receive="TestPage"





•#virtualhost=www.x.y.z复制代码【注意】
Ldirectord的作用是监测Real Server,当RealServer失效时,把它从Load Balancer列表中删除,恢复时重新添加,在安装heartbeat时已经安装了Ldirectord.配置中quiescent=no设置
实现如果Realserver机器down掉以后,立即从IPVS列表中删除,恢复时重新添加.

8.启动Heartbeat服务# /etc/init.d/heartbeat start五.测试Heartbeat及日志分析
1.
测试串口线配置是否正常
串行连接通过两个节点的串口,使用一根直连线缆将它们连接起来。现在测试串行连接,如下所示:在 primarylb(接收者)上输入以下内容:cat < /dev/ttyS0
在backuplb(发送者)上输入以下内容:
echo "Serial Connection test!Welcome http://www.linuxtone.org !"> /dev/ttyS02. 如果主节点需要维护时,可先将其转移到备份服务器上
/usr/lib/heartbeat/hb_standby(hb_standby命令把资源强迫到迁移到备份服务器)
如果主节点要切回来掌管服务:
/usr/lib/heartbeat/hb_takeover

3. 拔掉网线,测试在primary节点的网络断开后的切换情况
4. 停掉primary的heartbeat(/etc/init.d/heartbeat stop) 查看backup是否能正常接管资源
5. shutdown primary主机,测试是否能够正常切换,基本上和上面测试2差不多。
【注意】# tail -f /var/log/messages//在两台负载均衡服务器上注意查看日志变化来排错。

六. 相关注意事项及参考


1.项目实施过程遇到的问题及总结.【经验分享】
【以下是项目中遇到的两个问题,提出方便对你有帮助】

a)
假如两台VS之间使用的互备关系,那么当一台VS接管LVS服务时,可能会网络不通,这时因为路由器的MAC缓存表里无法及时刷新MAC.关于vip这个地址的MAC地址还是替换的VS的MAC,有两种解决方法,一种是修改新VS的MAC地址,另一种是使用send_arp /arpiing命令.
以arping命令为例./sbin/arping -I eth0 -c 3 -s ${vip}${gateway_ip} > /dev/null 2>&1•例如:复制代码/sbin/arping -I eth0 -c 3 -s 192.168.1.6192.168.1.1
复制代码b)某台Realserver服务down掉以后如何从IPVS列表自动中删除恢复时如何自动添加?配置ldirectord.conf
quiescent=no或 echo 1 >/proc/sys/net/ipv4/vs/expire_nodest_conn2.ha.cf相关注释
auto_failback设置为on时,一旦主节点重新恢复联机,将从从节点取回所有资源。
Stonith设备是一种能够自动关闭电源来响应软件命令的设备
Watchdog在实现上可以是硬件电路也可以是软件定时器,能够在系统出现故障时自动重新启动系统。

3.lvs状态查看
操作步骤详细到命令行级别
查看LVS的连接情况:ipvsadm -L -n
查看LVS的吞吐量情况: ipvsadm -L -n --rate
查看LVS的统计信息:ipvsadm -L -n --stats
实时查看LVS连接状态变化: watch ipvsadm ?ln


4. 附上LVS(DR)脚本#!/bin/bash

•#description: start LVS of DirectorServer

•#Writtenby :NetSeek http://www.linuxtone.org

•GW=192.168.1.1





•# websitedirector vip.

•WEB_VIP=192.168.1.106



•WEB_RIP1=192.168.1.104

•WEB_RIP2=192.168.1.105







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





•logger $0called with $1





•case"$1" in





•start)



•# Clear all iptables rules.





•/sbin/iptables-F



•# Reset iptables counters.







•/sbin/iptables -Z





•#Clear all ipvsadm rules/services.





•/sbin/ipvsadm-C







•#set lvs vip for dr



•/sbin/ipvsadm --set 30 5 60



•/sbin/ifconfig eth0:0 $WEB_VIP broadcast $WEB_VIP netmask 255.255.255.255 up





•/sbin/route add -host $WEB_VIP dev eth0:0



•/sbin/ipvsadm -A -t $WEB_VIP:80 -s wrr-p 3



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



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



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







•# set Arp



•/sbin/arping -I eth0 -c 5 -s $WEB_VIP$GW >/dev/null 2>&1



•;;

•stop)



•/sbin/ipvsadm -C



•/sbin/ipvsadm -Z



•ifconfig eth0:0 down



•route del $WEB_VIP

•>/dev/null 2>&1



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



•/sbin/arping -I eth0 -c 5 -s $WEB_VIP$GW



•echo "ipvsadm stoped"



•;;





•status)







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



•echo "ipvsadm isstoped"





•exit 1



•else



•ipvsadm -ln



•echo "..........ipvsadm isOK."



•fi



•;;





•*)



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



•exit 1

•esac





•exit 0复制代码5. 关于ldirectord.cf文件详解:http://bbs.linuxtone.org/thread-1388-1-1.html

LVS高可用性方案汇总: http://bbs.linuxtone.org/thread-1402-1-1.html[推荐]

LVS负载均衡资料汇总: http://bbs.linuxtone.org/thread-1191-1-1.html[推荐]

更多的资料请参考: http://bbs.linuxtone.org/forum-26-1.html

6. 联系我们[如果你遇到问题欢迎联系我们]:

IT运维专家网 http://www.linuxtone.orgMail: cnseek@gmail.com
页: [1]
查看完整版本: LVS+Heartbeat实施案例及经验分享