以下都是在vmware环境下调试,请把所有的网卡都设置成bridge模式;而且没有使用Com口的心跳线,用的是vmware之间的网络连接。
实验环境如下A 172.16.8.6  B172.16.8.7 C 172.16.8.100 
 
lvs-dr_lvs
实验一:lvs---dr实验
从Linux内核版本2.6起,ip_vs code已经被整合进了内核中,因此,只要在编译内核的时候选择了ipvs的功能,您的Linux即能支持LVS。Linux 2.4.23以后的内核版本也整合了ip_vs code,但如果是更旧的内核版本,您得自己手动将ip_vs code整合进内核原码中,并重新编译内核方可使用lvs。
在做下面实验前,需要安装拓扑搭建环境和安装ipvsadm工具(yum装)。
对于C
[root@slave ~]# ifconfig eth0:1 172.16.8.120 broadcast 172.16.8.120 netmask 255.255.255.255
[root@slave ~]# route add -host 172.16.8.120 dev eth0:1
[root@slave ~]# ipvsadm -A -t 172.16.8.120:80 -s wlc
[root@slave ~]# ipvsadm -a -t 172.16.8.120:80  -r 172.16.8.6 -w 2 -g
[root@slave ~]# ipvsadm -a -t 172.16.8.120:80  -r 172.16.8.7 -w 1 -g
[root@slave ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.8.120:80 wlc
  -> 172.16.8.7:80                Route   1      0          0        
  -> 172.16.8.6:80                Route   2      0          0   
[root@slave ~]# ipvsadm -L -n --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  172.16.8.120:80                    65      325        0    38920        0
  -> 172.16.8.7:80                      22      110        0    13091        0
  -> 172.16.8.6:80                      43      215        0    25829        0

对于A,B
[root@localhost ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@localhost ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@localhost ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@localhost ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@localhost ~]# ifconfig lo:0 172.16.8.120 broadcast 172.16.8.120 netmask 255.255.255.255
[root@localhost ~]# route add -host 172.16.8.120 dev lo:0
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.16.8.120    0.0.0.0         255.255.255.255 UH    0      0        0 lo
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
172.16.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         172.16.0.1      0.0.0.0         UG    0      0        0 eth0
 
arp_ignore 设置为1,当别人的arp请求过来的时候,如果接收的设备上面没有这个ip,就不响应,默认是0,只要这台机器上面任何一个设备上面有这个ip,就响应arp请求,并发送mac地址应答。
arp_announce 设置为 2,当内网的机器要发送一个到外部的ip包,那么它就会请求 路由器的Mac地址,发送一个arp请求,这个arp请求里面包括了自己的ip地址和Mac地址,而linux默认是使用ip的源ip地址作为arp里面 的源ip地址,而不是使用发送设备上面的 ,这样在lvs这样的架构下,所有发送包都是同一个VIP地址,那么arp请求就会包括VIP地址和设备 Mac,而路由器收到这个arp请求就会更新自己的arp缓存,这样就会造成ip欺骗了,VIP被抢夺,所以就会有问题。
脚本实现
Director脚本:
#!/bin/bash
#
# LVS script for VS/DR
#
. /etc/rc.d/init.d/functions
#
VIP=172.16.8.120
RIP1=172.16.8.6
RIP2=172.16.8.7
PORT=80
#
case "$1" in
start)          
  /sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up
  /sbin/route add -host $VIP dev eth0:1
# Since this is the Director we must be able to forward packets
  echo 1 > /proc/sys/net/ipv4/ip_forward
# Clear all iptables rules.
  /sbin/iptables -F
# Reset iptables counters.
  /sbin/iptables -Z
# Clear all ipvsadm rules/services.
  /sbin/ipvsadm -C
# Add an IP virtual service for VIP  port 80
# In this recipe, we will use the round-robin scheduling method.
# In production, however, you should use a weighted, dynamic scheduling method.
  /sbin/ipvsadm -A -t $VIP:80 -s wlc
# Now direct packets for this VIP to
# the real server IP (RIP) inside the cluster
  /sbin/ipvsadm -a -t $VIP:80 -r $RIP1 -g -w 1
  /sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -g -w 2
  /bin/touch /var/lock/subsys/ipvsadm &> /dev/null
;;
stop)
# Stop forwarding packets
  echo 0 > /proc/sys/net/ipv4/ip_forward
# Reset ipvsadm
  /sbin/ipvsadm -C
# Bring down the VIP interface
  /sbin/ifconfig eth0:1 down
  /sbin/route del $VIP
 
  /bin/rm -f /var/lock/subsys/ipvsadm
 
  echo "ipvs is stopped..."
;;
status)
  if [ ! -e /var/lock/subsys/ipvsadm ]; then
    echo "ipvsadm is stopped ..."
  else
    echo "ipvs is running ..."
    ipvsadm -L -n
  fi
;;
*)
  echo "Usage: $0 {start|stop|status}"
;;
esac

RealServer脚本:
#!/bin/bash
#
# Script to start LVS DR real server.
# description: LVS DR real server
#
.  /etc/rc.d/init.d/functions
VIP=172.16.8.120
host=`/bin/hostname`
case "$1" in
start)
       # Start LVS-DR real server on this machine.
        /sbin/ifconfig lo down
        /sbin/ifconfig lo up
        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
        /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
        /sbin/route add -host $VIP dev lo:0
;;
stop)
        # Stop LVS-DR real server loopback device(s).
        /sbin/ifconfig lo:0 down
        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
;;
status)
        # Status of LVS-DR real server.
        islothere=`/sbin/ifconfig lo:0 | grep $VIP`
        isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
        if [ ! "$islothere" -o ! "isrothere" ];then
            # Either the route or the lo:0 device
            # not found.
            echo "LVS-DR real server Stopped."
        else
            echo "LVS-DR real server Running."
        fi
;;
*)
            # Invalid entry.
            echo "$0: Usage: $0 {start|status|stop}"
            exit 1
;;
esac