lvs负载均衡-DR模式实验

发布时间:2020-02-27编辑:脚本学堂
本文介绍使用LVS实现负载均衡的DR模式实验。

本文介绍使用LVS实现负载均衡的DR模式实验。

1,实验环境:
负载均衡器:   eth0:192.168.1.16      vip: 192.168.1.17
后端WEB1:192.168.1.19
后端WEB2:192.168.1.20

2,创建LVS DR模式启动脚本
 

复制代码 代码如下:
[root@localhost ~]# vi /etc/init.d/lvsdsdr
#!/bin/bash
VIP=192.168.1.17
RIP1=192.168.1.19
RIP2=192.168.1.20
/etc/rc.d/init.d/functions
case "$1" in
start)
           echo "start LVS of DirectorServer DR"
           ###广播地址为VIP本身,掩码为255.255.255.255
           /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
           /sbin/ipvsadm -C
           /sbin/ipvsadm -A -t $VIP:80 -s rr        ###rr是调度算法
           /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g###-g指定工作模式为DR
           /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
           /sbin/ipvsadm
       ;;
stop)
           echo "stop LVS of DirectorServer DR"
           echo "0" >/proc/sys/net/ipv4/ip_forward
           /sbin/ipvsadm -C
           /sbin/ifconfig eth0:0 down
           ;;
*)
          echo "Usage: $0 {start|stop}"
          exit 1
esac
exit 0
[root@localhost ~]# chmod +x /etc/rc.d/init.d/lvsdsdr
[root@localhost ~]# /etc/rc.d/init.d/lvsdsdr start

3, web1,web2分别创建启动脚本
 

复制代码 代码如下:

[root@localhost ~]# vi /etc/init.d/lvsrsdr
#!/bin/bash
VIP=192.168.1.17
/etc/rc.d/init.d/functions
case "$1" in
start)
           echo "start LVS of RealServer DR"
           /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up     ###注意掩码
           /sbin/route add -host $VIP dev lo:0            ###VIP绑定至回环接口上
           echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore   ###抑制arp广播
           echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce      ###抑制arp广播
           echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
           echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
       ;;
stop)
           /sbin/ifconfig lo:0 down
           echo "close LVS of RealServer DR"
           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
exit 0

[root@localhost ~]# chmod +x /etc/rc.d/init.d/lvsrsdr
[root@localhost ~]# /etc/rc.d/init.d/lvsrsdr start
[root@localhost ~]# ifconfig lo:0
lo:0      Link encap:Local Loopback
          inet addr:192.168.1.17  Mask:255.255.255.255
          UP LOOPBACK RUNNING  MTU:16436  Metric:1

4,web1,web2分别修改首页,启动http服务
 

复制代码 代码如下:
[root@localhost ~]# vi /var/www/html/index.html
web1    192.168.1.19           ###web1
web2    192.168.1.20           ###web2
[root@localhost ~]# /etc/init.d/httpd start

5,终端及WEB测试

复制代码 代码如下:

[root@localhost ~]# for i in $(seq 1 10);do curl http://192.168.1.17;done
web2    192.168.1.20
web1    192.168.1.19
web2    192.168.1.20
web1    192.168.1.19
web2    192.168.1.20
web1    192.168.1.19
web2    192.168.1.20
web1    192.168.1.19
web2    192.168.1.20
web1    192.168.1.19
 

img1
LVS-DR模式实验

6,lvs查看访问连接

复制代码 代码如下:
[root@localhost ~]# ipvsadm -lnc
IPVS connection entries
pro expire state       source             virtual            destination
TCP 00:23  FIN_WAIT    192.168.1.10:53198 192.168.1.17:80    192.168.1.19:80
TCP 00:23  FIN_WAIT    192.168.1.10:53192 192.168.1.17:80    192.168.1.19:80
TCP 00:23  FIN_WAIT    192.168.1.10:53191 192.168.1.17:80    192.168.1.20:80
TCP 00:23  FIN_WAIT    192.168.1.10:53199 192.168.1.17:80    192.168.1.20:80
TCP 00:23  FIN_WAIT    192.168.1.10:53195 192.168.1.17:80    192.168.1.20:80
TCP 00:23  FIN_WAIT    192.168.1.10:53197 192.168.1.17:80    192.168.1.20:80
TCP 00:23  FIN_WAIT    192.168.1.10:53193 192.168.1.17:80    192.168.1.20:80
TCP 00:23  FIN_WAIT    192.168.1.10:53194 192.168.1.17:80    192.168.1.19:80
TCP 00:23  FIN_WAIT    192.168.1.10:53196 192.168.1.17:80    192.168.1.19:80
TCP 00:23  FIN_WAIT    192.168.1.10:53200 192.168.1.17:80    192.168.1.19:80