LVS 是LINUX VIRTUL SERVER的简称,是由章文嵩博士主持的著名开放源码项目,一个实现“三高”系统的解决方案。LVS旨在解决高速发展的Web商务中日益凸现的问题:如 何在有限资金投入的情况下,最大幅度的提高Web站点的潜在服务性能。核心就是通过一组服务器来进行负载均衡,通过前端的负载调度器(Load Balancer),无缝地将网络请求调度到真实服务器上,从而使得服务器集群的结构对客户是透明的,客户访问集群系统提供的网络服务就像访问一台高性 能、高可用的服务器一样。客户程序不受服务器集群的影响不需作任何修改。
系统的伸缩性通过在服务机群中透明地加入和删除一个节点来达到,通过检测节点或服务进程故障和正确地重置系统达到高可用性。由于我们的负载调度技术是在Linux内核中实现的,我们称之为Linux虚拟服务器(Linux Virtual Server)。
LVS的设计根据透明性、性能、高可用性、可管理性和可编程性的指导思想实现的。
我们把前面那台负载均衡机器叫做:director server(DR)。后面的实际服务机器叫做:real server(RS)
架构图:
所需软件:
ipvsadm-1.24-10.x86_64.rpm
heartbeat-2.1.3-3.el5.CentOS.x86_64.rpm
heartbeat-pils-2.1.3-3.el5.centos.x86_64.rpm
heartbeat-stonith-2.1.3-3.el5.centos.x86_64.rpm
PyXML-0.8.4-4.x86_64.rpm
系统环境:CentOS 5.9-64bit
192.168.137.14 虚拟ip
192.168.137.134 lvsmaster lvs主
192.168.137.135 lvsbackup lvs从
192.168.137.130 realserver1 web服务器
192.168.137.131 realserver2 web服务器
1、lvs安装
yum -y install ipvsadmyum -y install libnetyum -y install e2fsprogsyum -y install heartbeat
yum -y install heartbeat-devel
yum -y install heartbeat-ldirectord
echo "192.168.137.135 lvsmaster">>/etc/hosts
echo "192.168.137.135 lvsbackup">>/etc/hosts
2、配置步骤主
lvs配置
(1)lvsdr-basic脚本配置
[root@lvsmaster ~]#cat /etc/init.d/lvsdr-basic#!/bin/sh
VIP=192.168.137.14
RIP1=192.168.137.130
RIP2=192.168.137.131
/etc/rc.d/init.d/functions
case "$1" in
start)
echo "start [OK]"
/sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:1
echo "1" > /proc/sys/net/ipv4/ip_forward
/sbin/ipvsadm -C
/sbin/ipvsadm -A -t$VIP:80 -s rr
/sbin/ipvsadm -a -t$VIP:80 -r $RIP1:80 -g
/sbin/ipvsadm -a -t$VIP:80 -r $RIP2:80 -g
/sbin/ipvsadm
;;
stop)
echo "stop [OK]"
/sbin/ipvsadm -C
;;
*)
echo "Usage:$0 {start|stop}"
exit 1
esac
[root@lvsmaster ~]#chmod u+x /etc/init.d/lvsdr-basic
(2)ha.cf配置
[root@lvsmaster ~]#cat /etc/ha.d/ha.cf
debugfile /var/log/ha-debu
logfile /var/log/ha-log
logfacility local0
keepalive 2
warntime 10
deadtime 30
initdead 120
hopfudge 1
udpport 694
bcast eth0
ucast eth0 192.168.137.135
IPauto_failback on
node lvsmaster
node lvsbackup
ping 192.168.137.1
respawn root /usr/lib/heartbeat/ipfail
apiauth ipfail gid=root uid=root
(3)authkeys认证文件的配置
[root@lvsmaster ~]# cat /etc/ha.d/authkeys
auth 3
3 md5 9bf2f23aae3a63c16ea681295ffd7666[root@lvsmaster ~]#chmod u+x 600 authkeys
(4)ldirectord.cf文件配置
[root@lvsmaster ~]# cat /etc/ha.d/ldirectord.cf
checktimeout=3
checkinterval=1
autoreload=no
logfile="/var/log/ldirectord.log"
quiescent=no
virtual=192.168.137.14:80
real=192.168.137.130:80 gate
real=192.168.137.131:80 gate
service=http
request="test.html"
receive="Test"
scheduler=sh
protocol=tcp
(5)haresources文件配置[root@lvsmaster ~]# cat /etc/ha.d/haresources
lvsmaster IPaddr::192.168.137.14/24/eth0 ldirectord lvsdr-basic
(6)关闭ldirectord服务开启heartbeat服务。
[root@lvsmaster ~]# chkconfig --del ldirectord[root@lvsmaster ~]# chkconfig --level 2345 ldirectord off[root@lvsmaster ~]# chkconfig --level 2345 heartbeat on
备份lvs配置(1)switchdr配置
[root@lvsbackup ~]#cat /etc/init.d/switchdr#!/bin/sh
# description: close lo0 and arp_ignore
VIP=192.168.137.14
/etc/rc.d/init.d/functions
case "$1" in
start)
echo "************* start director server and close tunl ***********"
/sbin/ifconfig lo:0 down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
;;
stop)
echo "start Real Server"
/sbin/ifconfig eth0:0 down
/sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP up
/sbin/route add -host $VIP dev lo:0
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
sysctl -p
;;
*)
echo "Usage: switchdr {start|stop}"
exit 1
esac
[root@lvsbackup ~]#chmod u+x /etc/init.d/switchdr
(2)ha.cf配置
[root@lvsbackup ~]#cat /etc/ha.d/ha.cf
debugfile /var/log/ha-debu
logfile /var/log/ha-log
logfacility local0
keepalive 2
warntime 10
deadtime 30
initdead 120
hopfudge 1
udpport 694
bcast eth0
ucast eth0 192.168.137.134
auto_failback on
node lvsmaster
node lvsbackup
ping 192.168.137.1
respawn root /usr/lib/heartbeat/ipfail
apiauth ipfail gid=root uid=root
(3)authkeys 配置
[root@lvsbackup ~]# cat /etc/ha.d/authkeys
auth 3
3 md5 9bf2f23aae3a63c16ea681295ffd7666[root@lvsbackup ~]#chmod 600 authkeys
推荐阅读:
Linux 高可用(HA)集群之heartbeat基于crm进行资源管理详解