LVS搭建高可用性Web负载均衡服务器一.系统需求实现Linux下的Web服务器负载均衡,LVS以主备方式工作,并且实现LVS机器同时加入Web服务器群。
二.软硬件需求操作系统:Red Hat Enterprise Linux Server release 5(关闭selinux及iptables)相关软件:heartbeat-2.1.4 、net-snmp 、lm_sensors 、net-snmp、ipvsadm、perl模块网络要求:所有服务器位于同一VLan,机器无特殊要求。
三.软件安装以本次安装为例,先后安装下列rpm包以解决依赖关系.#rpm –ivh libnet-1.1.2.1-2.1.i386.rpm#rpm –ivh ipvsadm-1.24-8.1.i386.rpm#rpm –ivh lm_sensors-2.10.0-3.1.i386.rpm#rpm –ivh net-snmp-libs-5.3.1-14.el5.i386.rpm#rpm –ivh net-snmp-5.3.1-14.el5.i386.rpm#rpm –ivh perl-Compress-Zlib-1.42-1.fc6.i386.rpm#rpm –ivh perl-HTML-Parser-3.55-1.fc6.i386.rpm#rpm –ivh perl-HTML-Tagset-3.10-2.1.1.noarch.rpm#rpm –ivh perl-Net-SSLeay-1.30-4.fc6.i386.rpm#rpm –ivh perl-TimeDate-1.16-5.el5.noarch.rpm#rpm –ivh perl-MailTools-2.02-1.el5.rf.noarch.rpm#rpm –ivh perl-URI-1.35-3.noarch.rpm#rpm –ivh perl-libwww-perl-5.805-1.1.1.noarch.rpm以上软件包主要用来实现ISO/RM 2/3层数据转换及7层应用检测。
#rpm –ivh heartbeat-stonith-2.1.4-4.1.i386.rpm#rpm –ivh heartbeat-pils-2.1.4-4.1.i386.rpm#rpm –ivh heartbeat-ldirectord-2.1.4-4.1.i386.rpm#rpm –ivh heartbeat-2.1.4-4.1.i386.rpm#rpm –ivh heartbeat-devel-2.1.4-4.1.i386.rpmheartbeat实现LVS集群功能(如IP,磁盘管理),heartbeat-ldirectord管理应用服务器负载均衡。
#rpm –ivh httpd-2.2.3-6.el5.i386.rpm本次实验以apache为应用服务器,Squid同理。
四.软件配置1、按软件安装步骤,在两台服务器上安装软件.2、假设服务器A,B的公网IP分别是:A Eth0: 211.155.225.174/255.255.255.224B Eth0: 211.155.225.175/255.255.255.224集群虚IP: 211.155.225.173/255.255.255.224分别在A、B服务器的Eth0上配好IP及网关.3、设置服务器A,B的hostname为node1及node2,如不是请修改/etc/sysconfig/network的HOSTNAME部分,并执行#hostname node1 以立即生效。
在/etc/hosts加入两行:211.155.225.174 node1211.155.225.175 node24、修改服务器A,B的/etc/sysctl.conf文件,加入如下5行,并执行#sysctl -p 以立即生效。
net.ipv4.ip_forward = 1net.ipv4.conf.all.arp_ignore = 1net.ipv4.conf.eth0.arp_ignore = 1net.ipv4.conf.all.arp_announce = 2net.ipv4.conf.eth0.arp_announce = 25、以root进入服务器A,执行以下配置:➢在/etc/init.d/heartbeat中的#!/bin/sh后插入一行:ifconfig lo:100 211.155.225.173 netmask 255.255.255.255 up;➢#chkconfig heartbeat --level 35 on➢#cd /etc/ha.d 进入集群配置文件目录➢#vi authkeys 创建集群认证文件auth 33 md5 HA_2009➢#chmod 600 authkeys 必要的操作➢#vi ha.cf 创建集群节点文件logfile /var/log/ha.log#logfacility local0keepalive 2deadtime 30warntime 10initdead 80mcast eth0 231.231.231.231 694 1 0ping 211.155.225.161auto_failback onnode node1node node2respawn hacluster /usr/lib/heartbeat/ipfailapiauth ipfail gid=haclient uid=hacluster集群中有两个节点node1及node2,通过多播IP通信(主要用于超过2个节点)ping 211.155.225.161 网关ping检测➢#vi resource.d/vip.sh 创建我们自己的集群IP切换shell脚本#!/bin/shcase "$4" instart)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提供了LVS集群备用节点在回路地址监听集群虚IP以用于应用服务器。
➢#chmod +x resource.d/vip.sh➢#vi haresources 创建集群资源文件node1 ldirectord::ldirectord.cf LVSSyncDaemonSwap::master vip.sh::eth0::211.155.225.173::255.255.255.224➢#vi ldirectord.cf 创建应用服务器负载均衡配置文件checktimeout=5checkinterval=50autoreload=yeslogfile="/var/log/ldirectord.log"quiescent=yesvirtual=211.155.225.173:80real=211.155.225.174:80 gate 4real=211.155.225.175:80 gate 6#real=125.64.8.26:80 ipip 5fallback=127.0.0.1:80 gateservice=httpscheduler=wrrprotocol=tcpchecktype=negotiaterequest="cdntest.html"receive="CDN HA Test Page"virtualhost=各应用服务器检测间隔:50秒,检测5秒后无响应或响应错误使应用节点失效。
配置虚拟http服务器211.155.225.173,通过211.155.225.174和211.155.225.175的两台服务器做负载均衡。
发送/cdnest.html 到两个http服务器,5秒内成功获取内容为:"CDN HA Test Page"的页面则正常。
负载均衡以路由方式工作。
其中real=211.155.225.174:80 gate 4 4代表权值,real服务器权值越大分担的负载越大。
所有real服务器正常时,负载比例大约:4/(4+6)=40%scheduler=wrr 加权轮叫,可选(rr、wrr、lc、wlc、lblc、dh、sh)6、在服务器A的/var/www/html下创建文本。
#echo "CDN HA Test Page">/var/www/html/cdntest.html7、在服务器B上进行第4、5步操作,scp也可。
8、对于未安装集群软件的应用服务器(如IP:211.155.225.176,做http server),需要执行第4步操作,并在系统启动后执行ifconfig lo:100 211.155.225.173 netmask 255.255.255.255 up去除文件ldirectord.cf 行#real=211.155.225.176:80 gate前注释即可.五.测试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.html3、在服务器A、B上分别执行ifconfig正常的话,在服务器A上出现eth0:100 IP:211.155.225.173在服务器B上出现l0:100 IP:211.155.225.173 在服务器A上执行service heartbeat stop,结果是:在服务器A上出现lo0:100 IP:211.155.225.173在服务器B上出现eth0:100 IP:211.155.225.173 在服务器A上执行service heartbeat start将恢复正常情况。
4、在服务器A、B上httpd启动的情况,不断刷新http://211.155.225.173正常的话,交替出现WebSite A及WebSite B在服务器A上执行service httpd stop,结果是:只出现WebSite B在服务器A上执行service httpd start,B上执行stop操作,结果是:只出现WebSite A全部启动httpd,将恢复正常情况。