这个脚本是LVS的DR模式  当用户访问192.168.1.22的时候 实际访问的web地址是192.168.1.243和192.168.1.251.
 
Vip 地址 192.168.1.22
客户端一   192.168.1.243
客户端二   192.168.1.251
 
安装软件: yum -y install ipvsadm
将以下代码复制新建脚本 lvs     在将 lvs 复制到 /etc/init.d/ chmod 777 lvs
启动 lvs 脚本   /etc/init.d/lvs start
启动 ipvsadm  /etc/init.d/ipvsadm start
                                                 两个客户端操作相同
完成之后 192.168.1.22 来访问两个客户端的80 端口页面即可
 
#!/bin/bash
#
# LVS script for VS/DR
#
. /etc/rc.d/init.d/functions
#
VIP=192.168.1.22
RIP1=192.168.1.243
RIP2=192.168.1.251
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 192.168.0.219 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## 有几个 RIP 地址这里就需要几行
  /sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -g -w 1    ##-w 1 为平衡刷新页面
  /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 stopped..."
;;
status)
  if [ ! -e /var/lock/subsys/ipvsadm ]; then
    echo "ipvsadm stopped ..."
  else
    echo "ipvs is running ..."
    ipvsadm -L -n
  fi
;;
*)
  echo "Usage: $0 {start|stop|status}"
;;
esac