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.gz tar zxvf ipvsadm-1.26.tar.gz cd ipvsadm-1.26 make && make install
4、编写并运行脚本(LVS服务器的脚本)
vi lvs.sh
#!/bin/bash # 2013-09-07 by kgdxpr VIP=10.10.3.170 RIP1=10.10.3.140 RIP2=10.10.3.141 /etc/rc.d/init.d/functions logger $0 called with $1 case "$1" in start) 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:0 echo "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 1 esac
注:/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 kgdxpr VIP=10.10.3.170 /etc/rc.d/init.d/functions case "$1" in start) 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: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 >/dev/null 2>&1 ;; stop) /sbin/ifconfig lo:0 down echo "close LVS Directorserver" 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
7、给脚本加权限,并执行
chmod +x realserver.sh
chmod 755 /etc/rc.d/init.d/functions
./realserver.sh start