LVS负载均衡-TUN模式实验

发布时间:2019-12-29编辑:脚本学堂
本文介绍使用LVS实现负载均衡的TUN模式实验。

本文介绍使用LVS实现负载均衡的TUN模式实验。
TUN模式与DR模式实验步骤区别在VIP绑定设备不一样,简单修改DR脚本即可。

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

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

复制代码 代码如下:

[root@localhost ~]# vi /etc/init.d/lvsdstun
#!/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 Tun"
           /sbin/ifconfig tunl0 $VIP broadcast $VIP netmask 255.255.255.255 up
           /sbin/route add -host $VIP dev tunl0
           /sbin/ipvsadm -C
           /sbin/ipvsadm -A -t $VIP:80 -s rr
           /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -i###-i指定工作模式为TUN
           /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -i
           /sbin/ipvsadm
       ;;
stop)
           echo "stop LVS of DirectorServer Tun"
           /sbin/ifconfig tunl0 down
           /sbin/ipvsadm -C
           ;;
*)
          echo "Usage: $0 {start|stop}"
          exit 1
esac

[root@localhost ~]# chmod +x /etc/rc.d/init.d/lvsdsdr
[root@localhost ~]# /etc/rc.d/init.d/lvsdsdr start
[root@localhost ~]# ifconfig tunl0
tunl0     Link encap:IPIP Tunnel  HWaddr 
          inet addr:192.168.1.17  Mask:255.255.255.255
          UP RUNNING NOARP  MTU:1480  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
 

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

复制代码 代码如下:

[root@localhost ~]# vi /etc/init.d/lvsrstun
#!/bin/bash
VIP=192.168.1.17
/etc/rc.d/init.d/functions
case "$1" in
start)
           echo "start LVS of RealServer Tun"
           /sbin/ifconfig tunl0 $VIP broadcast $VIP netmask 255.255.255.255 up
           /sbin/route add -host $VIP dev tunl0
           echo "1" >/proc/sys/net/ipv4/conf/tunl0/arp_ignore
           echo "2" >/proc/sys/net/ipv4/conf/tunl0/arp_announce
           echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
           echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
       ;;
stop)
           echo "close LVS of RealServer Tun"
           /sbin/ifconfig tunl0 down
           echo "0" >/proc/sys/net/ipv4/conf/tunl0/arp_ignore
           echo "0" >/proc/sys/net/ipv4/conf/tunl0/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 tunl0
tunl0     Link encap:IPIP Tunnel  HWaddr 
          inet addr:192.168.1.17  Mask:255.255.255.255
          UP RUNNING NOARP  MTU:1480  Metric:1
          RX packets:15 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:1890 (1.8 KiB)  TX bytes:0 (0.0 b)

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-TUN模式实验

6,lvs查看访问连接

复制代码 代码如下:
[root@localhost ~]# ipvsadm -lnc
IPVS connection entries
pro expire state       source             virtual            destination
TCP 01:24  FIN_WAIT    192.168.1.10:34526 192.168.1.17:80    192.168.1.19:80
TCP 01:25  FIN_WAIT    192.168.1.10:34532 192.168.1.17:80    192.168.1.19:80
TCP 01:25  FIN_WAIT    192.168.1.10:34531 192.168.1.17:80    192.168.1.20:80
TCP 01:25  FIN_WAIT    192.168.1.10:34534 192.168.1.17:80    192.168.1.19:80
TCP 01:24  FIN_WAIT    192.168.1.10:34525 192.168.1.17:80    192.168.1.20:80
TCP 01:25  FIN_WAIT    192.168.1.10:34528 192.168.1.17:80    192.168.1.19:80
TCP 01:25  FIN_WAIT    192.168.1.10:34527 192.168.1.17:80    192.168.1.20:80
TCP 01:25  FIN_WAIT    192.168.1.10:34530 192.168.1.17:80    192.168.1.19:80
TCP 01:25  FIN_WAIT    192.168.1.10:34533 192.168.1.17:80    192.168.1.20:80
TCP 01:25  FIN_WAIT    192.168.1.10:34529 192.168.1.17:80    192.168.1.20:80