LVS负载均衡之TUN IP隧道
Virtual Server via IP Tunneling采用的是报文单向重写的策略,与Virtual Server via NAT不同的是NAT采用的是报文双向重写的策略,数据报文经过调度器的时候就会将地址和端口改为Real Server的真实地址以及相应的端口,而当相应报文返回时,则反之。而LVS-TUN如果在二层,就在MAC之外再加一层MAC,在三层源IP目标IP再加一层IP,主要实现隐藏以及跨区域。
TUN隧道主要实现的是非同一区域的节点集群,突破了DR(节点与Director 同一区域,因为其主要靠MAC地址进行局域网的数据报文转发)的区域限制。
以下是它的特点:
1.集群节点和Director不必在同一个网络
2.RIP必须使用公网地址
3.Director只需要处理进来的请求,不需要处理出去的请求(与DR原理一样,直接将响应发给客户端)
4.响应的请求一定不能经过Direcor.(在互联网的其他节点)
5.Directory不支持端口映射
6.只能使用那些支持IP 隧道协议的操作系统做realserver
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 ----------echo的内容也可以用vi编辑器修改
;;
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
6,lvs查看访问连接
[root@localhost ~]# ipvsadm -lnc
IPVS connection entries
pro expire state sourcevirtual 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