在lvs的nat模式的基础上

1.清空ipvsadm规则

 ipvsadm -C

查看
ipvsadm -ln

2.清空iptables规则

iptables -t nat -F

3.修改rs的网卡配置,修改回来网关

 vi /etc/sysconfig/network-scripts/ifcfg-eth0

重启eth0
ifdown eth0; ifup eth0

4.dir网卡

 ifdown eth1

dirIP:192.168.1.118  rs1IP:192.168.1.116 rs2IP:192.168.1.115  都视为外网IP。

在dir上面添加虚拟IP,vip

 vim /usr/local/sbin/lvs_dr.sh
#! /bin/bash
echo > /proc/sys/net/ipv4/ip_forward
ipv=/sbin/ipvsadm
vip=192.168.1.100
rs1=192.168.1.116
rs2=192.168.1.115
ifconfig eth0: $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev eth0:
$ipv -C
$ipv -A -t $vip: -s rr
$ipv -a -t $vip: -r $rs1: -g -w
$ipv -a -t $vip: -r $rs2: -g -w

echo 1 > /proc/sys/net/ipv4/ip_forward  //打开端口转发
ipv=/sbin/ipvsadm
vip=192.168.1.100    
rs1=192.168.1.116
rs2=192.168.1.115
ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up  //VIP子网掩码是4个255,只发不收
route add -host $vip dev eth0:0  //增加一条路由
$ipv -C
$ipv -A -t $vip:80 -s rr
$ipv -a -t $vip:80 -r $rs1:80 -g -w 1
$ipv -a -t $vip:80 -r $rs2:80 -g -w 1

sh !$
ifconfig

虚拟网卡已经出来了
eth0: Link encap:Ethernet HWaddr :0C::CC::E7
inet addr:192.168.1.100 Bcast:192.168.1.100 Mask:255.255.255.255
UP BROADCAST RUNNING MULTICAST MTU: Metric:
 ipvsadm -ln

多出来一个VIP
IP Virtual Server version 1.2. (size=)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.100: rr
-> 192.168.1.115: Route
-> 192.168.1.116: Route

5.rs1-2都写个脚本

vim /usr/local/sbin/lvs_rs.sh
#! /bin/bash
vip=192.168.1.100
ifconfig lo: $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:
echo "" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/all/arp_announce
 ifconfig

启动了回环虚拟网卡
lo: Link encap:Local Loopback
inet addr:192.168.1.100 Mask:255.255.255.255
UP LOOPBACK RUNNING MTU: Metric:

6.测试

在第四台机器上curl  vip

05-11 17:30