3、LVS_TUN实现负载均衡
LVS_TUN实现负载均衡
LVS 介绍:
LVS 是 Linux Virtual Server 的简称,在实际环境中经常作为 B/S 结构的网络应用中的负载均衡器来使用,工作在 7 层网络模型中的,网络层,也就是通常说的 IP 层,由于数据的处理是在 Linux 内核态完成的,所以相对反向代理服务器来说,性能一般会高一些;
实验原理图:
实验原理:
TUN 是 IP Tunneling ,IP 隧道的简称,它将调度器收到的 IP 数据包封装在一个新的IP 数据包中,转交给应用服务器,然后实际服务器的返回数据会直接返回给用户。
IP隧道:将一个完整的IP报文封装成另一个新的IP报文的数据部分,并通过路由器传送到指定的地点。在这个过程中路由器并不在意被封装的原始协议的内容。到达目的地点后,由目的地方依靠自己的计算能力和对IP隧道协议的支持,打开封装协议,取得原始协议。
LVS-DR模式和LVS-TUN模式的工作原理完全不一样,工作场景完全不一样。DR基于数据报文重写,TUN模式基于IP隧道,后者是对数据报文的重新封装。
优点:
可以说LVS-TUN方式基本上具有LVS-DR的全部优点,并在此基础上又支持跨子网间穿透。
缺点:
实验环境:
真实服务器端: test2:172.25.1.2
真实服务器端: test3:172.25.1.3
TUN端: test4:172.25.1.4
客户端: fundation1:172.25.1.250
TUN端:
[[email protected] ~]# /etc/init.d/iptables stop //关掉防火墙 iptables
[[email protected] ~]# ip addr add 172.25.1.100 dev eth0
[[email protected] ~]# ip addr
[[email protected] ~]# modprobe ipip //利用ipip建立tunnel(隧道)
[[email protected] ~]# ip addr
[[email protected] ~]# ip link set up tunl0
[[email protected] ~]# ip addr add 172.25.1.100/24 dev tunl0
[[email protected] ~]# ip addr
[[email protected] ~]# yum install ipvsadm.x86_64 -y //下载服务在Director服务器上安装ipvsadmin工具,此处我们使用RedHat自带的rpm包进行安装
[[email protected] ~]# /etc/init.d/ipvsadm start //开启服务
//将test2和test3加入Web集群服务
[[email protected] ~]# ipvsadm -A -t 172.25.1.100:80 -s rr
[[email protected] ~]# ipvsadm -a -t 172.25.1.100:80 -r 172.25.1.2:80 -i
[[email protected] ~]# ipvsadm -a -t 172.25.1.100:80 -r 172.25.1.3:80 -i
[[email protected] ~]# ipvsadm -l
test2端:
[[email protected] ~]# /etc/init.d/iptables stop
[[email protected] ~]# modprobe ipip
[[email protected] ~]# ip link set up tunl0
[[email protected] ~]# ip addr add 172.25.1.100/24 dev tunl0
[[email protected] ~]# /etc/init.d/arptables_jf start
[[email protected] ~]# sysctl -a|grep rp_filter
[[email protected] ~]# sysctl -w net.ipv4.conf.tunl0.rp_filter=0
[[email protected] ~]# vim /var/www/html/index.html
[[email protected] ~]# /etc/init.d/httpd start
[[email protected] ~]# lsmod | grep ipip
test3(与test2步骤相同):
[[email protected] ~]# /etc/init.d/iptables stop
[[email protected] ~]# modprobe ipip
[[email protected] ~]# ip link set up tunl0
[[email protected] ~]# ip addr add 172.25.1.100/24 dev tunl0
[[email protected] ~]# ifconfig -a
[[email protected] ~]# /etc/init.d/arptables_jf start
[[email protected] ~]# vim /var/www/html/index.html
[[email protected] ~]# /etc/init.d/httpd start
[[email protected] ~]# sysctl -w net.ipv4.conf.tunl0.rp_filter=0
[[email protected] ~]# lsmod | grep ipip
客户端测试:
[[email protected] ~]# curl 172.25.1.100