首页 > 代码库 > CentOS6.4 配置LVS(DR模式)

CentOS6.4 配置LVS(DR模式)

DR模式中LVS主机与实际服务器都有一块网卡连在同一物理网段上。

IP分配

VIP:10.10.3.170

RIP1:10.10.3.140

RIP2:10.10.3.141

1、安装所需的依赖包

yum install -y wget make kernel-devel gcc gcc-c++ libnl* libpopt* popt-static

2、创建一个软链接,防止后面编译安装ipvsadm时找不到系统内核

ln -s /usr/src/kernels/2.6.32-358.18.1.el6.x86_64/ /usr/src/linux

3、下载安装ipvsadm

wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gztar zxvf ipvsadm-1.26.tar.gzcd ipvsadm-1.26make && make install

4、编写并运行脚本(LVS服务器的脚本)

vi lvs.sh
#!/bin/bash# 2013-09-07 by kgdxprVIP=10.10.3.170RIP1=10.10.3.140RIP2=10.10.3.141/etc/rc.d/init.d/functionslogger $0 called with $1case "$1" instart)echo " start LVS of DirectorServer"/sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up/sbin/route add -host $VIP dev eth0:0echo "1" >/proc/sys/net/ipv4/ip_forward#Clear IPVS table/sbin/ipvsadm -C#set LVS/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#Run LVS/sbin/ipvsadm;;stop)echo "close LVS Directorserver"echo "0" >/proc/sys/net/ipv4/ip_forward/sbin/ipvsadm -C/sbin/ifconfig eth0:0 down;;*)echo "Usage: $0 {start|stop}"exit 1esac

注:/sbin/ipvsadm -A -t $VIP:80 -s rr (rr代表轮询,还有分配方式)

/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g (-g代表DR模式,还有其他模式)

5、给脚本加权限,并执行

chmod +x lvs.sh
chmod 755 /etc/rc.d/init.d/functions./lvs.sh start

6、配置后端的WEB服务器脚本

vi realserver.sh
#!/bin/bash# 2013-09-07 by kgdxprVIP=10.10.3.170/etc/rc.d/init.d/functionscase "$1" instart)echo " start LVS of REALServer"/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up/sbin/route add -host $VIP dev lo:0echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/lo/arp_announceecho "1" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/all/arp_announcesysctl -p >/dev/null 2>&1;;stop)/sbin/ifconfig lo:0 downecho "close LVS Directorserver"echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "0" >/proc/sys/net/ipv4/conf/lo/arp_announceecho "0" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "0" >/proc/sys/net/ipv4/conf/all/arp_announce;;*)echo "Usage: $0 {start|stop}"exit 1esac

 7、给脚本加权限,并执行 

chmod +x realserver.sh
chmod 755 /etc/rc.d/init.d/functions./realserver.sh start