实验结构图:
 
使用LVS实现多服务复杂化均衡_lvs
系统环境:
VMWare ESXi
CentOS 5.2
Apache
qmail
LVS 实现方式: DR
DR是指Direct Routing,它的转发流程是:Director机器收到外界请求,按相应的调度算法将其直接发送到相应Real ServerReal Server处理完该请求后,将结果数据包直接返回给客户,完成一次负载调度。
需要的软件包:ipvsadm-1.24-8.1
网络配置:
LD Server eth0 10.0.10.209
Real Server1 eth0 10.0.10.207
Real Server2 eth0 10.0.10.208
Real Server3 eth0 10.0.10.210
Real Server4 eth0 10.0.10.240
VIP 10.0.10.60
VIP2 10.0.10.61
 
用户请求http连接是指向10.0.10.60LD Server通过权重轮询(wrr)调度,将http请求转发到Real Server群中。
LVS的配置是通过前面所安装的IP虚拟服务器软件ipvsadm来实现的。ipvsadmLVS的关系类似于iptablesNetFilter的关系,前者只是一个建立和修改规则的工具,这些命令的作用在系统重新启动后就消失了,所以应该将这些命令写到一个脚本里,然后让它在系统启动后自动执行。
 
LD Server 启动脚本:/etc/init.d/lvsDR
#!/bin/sh
#
# description: start LVS of Director Server    
# created in 2009-04-29 by Dika.Ye

VIP=10.0.10.60
VIP2=10.0.10.61
RIP1=10.0.10.207
RIP2=10.0.10.208
RIP3=10.0.10.210
RIP4=10.0.10.240
#RIP3=10.0.10.x

GW=10.0.10.254

SER_PORT=80
SMTP_PORT=25
HTTP_CON_INTERFACE=eth0:6721
SMTP_CON_INTERFACE=eth0:6725
ATTEM=wrr

# Source function library.
. /etc/rc.d/init.d/functions

case $1 in
        start)
                echo -n "Starting LVS of Director Server "    
                #Set the Virtual IP Address    
                /sbin/ifconfig $HTTP_CON_INTERFACE $VIP broadcast $VIP netmask 255.255.255.255 up
                /sbin/ifconfig $SMTP_CON_INTERFACE $VIP2 broadcast $VIP2 netmask 255.255.255.255 up
                /sbin/route add -host $VIP dev $HTTP_CON_INTERFACE
                /sbin/route add -host $VIP2 dev $SMTP_CON_INTERFACE
                #Clear IPVS Table    
                /sbin/ipvsadm -C
                #Set Lvs for http
                /sbin/ipvsadm -A -t $VIP:$SER_PORT -s $ATTEM
                /sbin/ipvsadm -a -t $VIP:$SER_PORT -r $RIP1:$SER_PORT -g
                #Set LVS for smtp
                /sbin/ipvsadm -A -t $VIP2:$SMTP_PORT -s $ATTEM
                /sbin/ipvsadm -a -t $VIP2:$SMTP_PORT -r $RIP3:$SMTP_PORT -g
                /sbin/ipvsadm -a -t $VIP2:$SMTP_PORT -r $RIP4:$SMTP_PORT -g
                #Run Lvs    
                /sbin/ipvsadm
                echo "."
                ;;
        stop)
                echo -n "Close LVS Director Server "    
                /sbin/ifconfig $HTTP_CON_INTERFACE down
                /sbin/ifconfig $SMTP_CON_INTERFACE down
                /sbin/ipvsadm -C
                echo "."
                ;;
        status)
                echo "IPVSADM list"
                ipvsadm -Ln
                ;;
        *)
                echo "Usage: $0" {start|stop}    
                exit 1
esac
 
Real Server启动脚本:/etc/init.d/lvsRS
HTTP Real Server
 
#!/bin/sh
# decription:start realserver    
# chkconfig 235 26 26    

VIP=10.0.10.60
VIP2=10.0.10.61
RIP1=10.0.10.207
RIP2=10.0.10.208
RIP3=10.0.10.210
RIP4=10.0.10.240
#RIP3=10.0.10.x

GW=10.0.10.254

SER_PORT=80
SMTP_PORT=25
HTTP_CON_INTERFACE=lo:6721
SMTP_CON_INTERFACE=lo:6725
ATTEM=wrr

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

case "$1" in
                start)
                echo " start LVS of REALServer"    
                /sbin/ifconfig $HTTP_CON_INTERFACE $VIP broadcast $VIP netmask 255.255.255.255 up
                /sbin/route add -host $VIP dev $HTTP_CON_INTERFACE
                echo "0" > /proc/sys/net/ipv4/ip_forward
                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
                ;;
                stop)
                /sbin/ifconfig $HTTP_CON_INTERFACE down
                echo " stop LVS service"    
                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
                ;;
                *)
                echo "Usage: $0 {start|stop}"    
                exit 1
esac
 
Real Server启动脚本:/etc/init.d/lvsRS
SMTP Real Server
 
#!/bin/sh
# decription:start realserver    
# chkconfig 235 26 26    

VIP=10.0.10.60
VIP2=10.0.10.61
RIP1=10.0.10.207
RIP2=10.0.10.208
RIP3=10.0.10.210
RIP4=10.0.10.240
#RIP3=10.0.10.x

GW=10.0.10.254

SER_PORT=80
SMTP_PORT=25
HTTP_CON_INTERFACE=lo:6721
SMTP_CON_INTERFACE=lo:6725
ATTEM=wrr

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

case "$1" in
                start)
                echo " start LVS of REALServer"    
                /sbin/ifconfig $SMTP_CON_INTERFACE $VIP2 broadcast $VIP2 netmask 255.255.255.255 up
                /sbin/route add -host $VIP2 dev $SMTP_CON_INTERFACE
                echo "0" > /proc/sys/net/ipv4/ip_forward
                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
                ;;
                stop)
                /sbin/ifconfig $SMTP_CON_INTERFACE down
                echo " stop LVS service"    
                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
                ;;
                *)
                echo "Usage: $0 {start|stop}"    
                exit 1
esac
 
 
启动服务:
 
LD Server
 
# service lvsDR start
 
Real Server
 
# service lvsRS start