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端:
[root@test4 ~]# /etc/init.d/iptables stop //關掉防火墻 iptables
[root@test4 ~]# ip addr add 172.25.1.100 dev eth0
[root@test4 ~]# ip addr
[root@test4 ~]# modprobe ipip //利用ipip建立tunnel(隧道)
[root@test4 ~]# ip addr
[root@test4 ~]# ip link set up tunl0
[root@test4 ~]# ip addr add 172.25.1.100/24 dev tunl0
[root@test4 ~]# ip addr
[root@test4 ~]# yum install ipvsadm.x86_64 -y //下載服務在Director服務器上安裝ipvsadmin工具,此處我們使用RedHat自帶的rpm包進行安裝
[root@test4 ~]# /etc/init.d/ipvsadm start //開啟服務
//將test2和test3加入Web集群服務
[root@test4 ~]# ipvsadm -A -t 172.25.1.100:80 -s rr
[root@test4 ~]# ipvsadm -a -t 172.25.1.100:80 -r 172.25.1.2:80 -i
[root@test4 ~]# ipvsadm -a -t 172.25.1.100:80 -r 172.25.1.3:80 -i
[root@test4 ~]# ipvsadm -l
test2端:
[root@test2 ~]# /etc/init.d/iptables stop
[root@test2 ~]# modprobe ipip
[root@test2 ~]# ip link set up tunl0
[root@test2 ~]# ip addr add 172.25.1.100/24 dev tunl0
[root@test2 ~]# /etc/init.d/arptables_jf start
[root@test2 ~]# sysctl -a|grep rp_filter
[root@test2 ~]# sysctl -w net.ipv4.conf.tunl0.rp_filter=0
[root@test2 ~]# vim /var/www/html/index.html
[root@test2 ~]# /etc/init.d/httpd start
[root@test2 ~]# lsmod | grep ipip
test3(與test2步驟相同):
[root@test3 ~]# /etc/init.d/iptables stop
[root@test3 ~]# modprobe ipip
[root@test3 ~]# ip link set up tunl0
[root@test3 ~]# ip addr add 172.25.1.100/24 dev tunl0
[root@test3 ~]# ifconfig -a
[root@test3 ~]# /etc/init.d/arptables_jf start
[root@test3 ~]# vim /var/www/html/index.html
[root@test3 ~]# /etc/init.d/httpd start
[root@test3 ~]# sysctl -w net.ipv4.conf.tunl0.rp_filter=0
[root@test3 ~]# lsmod | grep ipip
客戶端測試:
[root@foundation1 ~]# curl 172.25.1.100