LVS-DR模型实现http负载均衡集群,VIP与DIP/RIP不在同一网络。
不废话原理,网上一堆一堆的。直接上拓扑和配置脚本。
网络拓扑如下:
各个设备的路由表信息。注:有时实验不出结果,可能是因为设备的路由表配置错误。
1.DR配置脚本:
1 #!/bin/bash 2 # 3 # DR配置脚本 4 5 vip='10.0.0.200' 6 iface='eth1:0' 7 mask='255.255.255.255' 8 port='80' 9 rs1='172.16.0.101' 10 rs2='172.16.0.102' 11 scheduler='wrr' 12 type='-g' 13 14 case $1 in 15 start) 16 ifconfig $iface $vip netmask $mask 17 iptables -F 18 19 ipvsadm -A -t ${vip}:${port} -s $scheduler 20 ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 1 21 ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 1 22 echo "The VS Server is Ready!" 23 ;; 24 stop) 25 ipvsadm -C 26 ifconfig $iface down 27 echo "The VS Server is Canceled!" 28 ;; 29 *) 30 echo "Usage: $(basename $0) start|stop" 31 exit 1 32 ;; 33 esac
2.RS配置脚本:
1 #!/bin/bash 2 # 3 #RS配置脚本 4 5 vip=10.0.0.200 6 mask='255.255.255.255' 7 dev=lo:1 8 case $1 in 9 start) 10 echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore 11 echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore 12 echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce 13 echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce 14 ifconfig $dev $vip netmask $mask 15 echo "The RS Server is Ready!" 16 ;; 17 stop) 18 ifconfig $dev down 19 echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore 20 echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore 21 echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce 22 echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce 23 echo "The RS Server is Canceled!" 24 ;; 25 *) 26 echo "Usage: $(basename $0) start|stop" 27 exit 1 28 ;; 29 esac
3.实验结果
1 [root@client ~]#curl 10.0.0.200 2 RS_1 3 [root@client ~]#curl 10.0.0.200 4 RS_2 5 [root@client ~]#curl 10.0.0.200 6 RS_1 7 [root@client ~]#curl 10.0.0.200 8 RS_2 9 [root@client ~]#