一、LVS-IP TUN集群概述
DR方式是通過MAC,規模是一個交換網絡。而TUN方式,是通過給數據包加上新的IP頭部來實現,這個可以跨整個廣域網。TUN模式可以解決DR模式下不能跨網段的問題,甚至可以跨公網進行
如圖:
異地機房的好處: 容災
但是是否可以保證 邊界 最近訪問到對應的real server呢? 不能,這是最大的問題
DNS view功能實現 邊界最近訪問。 智能DNS 大家nslookup www.baidu.com 每個人地方的ip都是不一樣的
二、實戰:配置LVS-IP TUN集群
精簡版實驗拓撲圖:
A、xuegod63 配置成 Director:
1 配置網絡:
eth0 DIP
eth0:1 VIP
[root@xuegod63 ~]#ifconfig eth0 192.168.1.70
[root@xuegod63 ~]#ifconfig eth0:1 192.168.1.63
[root@xuegod63 ~]#ifconfig
[root@xuegod63 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
#該文件表示是否打開IP轉發。0表示禁止,1表示轉發
2 配置 LVS TUN模式
[root@xuegod63 ~]# rpm -ivh /mnt/Packages/ipvsadm-1.25-9.el6.x86_64.rpm
[root@xuegod63 ~]# ipvsadm -C
[root@xuegod63 ~]# ipvsadm -A -t 192.168.1.63:80 -s rr
[root@xuegod63 ~]# ipvsadm -a -t 192.168.1.63:80 -r 192.168.1.62 -i
[root@xuegod63 ~]# ipvsadm -a -t 192.168.1.63:80 -r 192.168.1.64 -i
# -i 隧道模式
[root@xuegod63 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.63:80 rr
-> 192.168.1.62:80 Tunnel 1 0 0
-> 192.168.1.64:80 Tunnel 1 0 0
B、RealServer: xuegod62
1 配置IP:
配置 eth0的 RIP為: 192.168.1.62
[root@xuegod62 ~] # lsmod | grep ipip 查看
[root@xuegod62 ~]# modprobe ipip #在加載好ipip模塊后就會有默認的tunl0隧道。
注,如果沒有在此處手動加載,那么使用ifconfig tunl0 時,會自動加載ipip隧道模塊。
使用 ifconfig查看。沒有tun0 ,加參數-a 時,查看可以看到tun0
[root@xuegod62 ~]# ifconfig -a #查看。
eth0 Link encap:Ethernet HWaddr 00:0C:29:48:80:95
。。。
lo Link encap:Local Loopback
。。。
tunl0 Link encap:IPIP Tunnel HWaddr
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)
[root@xuegod62 ~]# lsmod | grep ipip
ipip 8435 0
tunnel4 2943 1 ipip
C、配置VIP:
1. 生成ifcfg-tunl0配置文件:
[root@xuegod62 network-scripts]# ifconfig tunl0 192.168.1.63 netmask 255.255.255.255 up
[root@xuegod62 network-scripts]# service network restart
Shutting down interface eth0: [ OK ]
Shutting down interface tunl0: [ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: [ OK ]
Bringing up interface tunl0: [ OK ]
D、 關閉ARP轉發。
echo '0' > /proc/sys/net/ipv4/ip_forward 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 '0' > /proc/sys/net/ipv4/conf/tunl0/rp_filter echo '0' > /proc/sys/net/ipv4/conf/all/rp_filter
# tunl0/rp_filter 默認為1 , 需要改為0,關閉此功能。Linux的rp_filter用於實現反向過濾技術,也即uRPF,它驗證反向數據包的流向,以避免偽裝IP攻擊 。 然而,在LVS TUN 模式中,我們的數據包是有問題的,因為從realserver eth0 出去的IP數據包的源IP地址應該為192.168.1.62,而不是VIP地址。所以必須關閉這一項功能。 DR和TUN在 網絡層實際上使用了一個偽裝IP數據包的功能。讓client收到數據包后,返回的請求再次轉給分發器。
echo '0' > /proc/sys/net/ipv4/conf/all/rp_filter #這個值默認就是0,此功能就是關閉的。所以在LVS DR模式中,不需要修改這一樣內核參數。
永久生效:
[root@xuegod62 ~]# vim /etc/sysctl.conf #以文件最后添加以下內容
net.ipv4.conf.tunl0.arp_ignore = 1 net.ipv4.conf.tunl0.arp_announce = 2 net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.tunl0.rp_filter = 0 net.ipv4.conf.all.rp_filter = 0
[root@xuegod62 ~]# sysctl -p
E、 配置web服務器
[root@xuegod62 ~]# yum install httpd
[root@xuegod62 ~]# echo 192.168.1.65 > /var/www/html/index.html
[root@xuegod62 ~]# service httpd restart
RealServer: xuegod64
1,配置IP地址:
配置eth0 RIP 192.168.1.64
[root@xuegod62 ~]# modprobe ipip
#在加載好ipip模塊后就會有默認的tunl0隧道
配置VIP:
[root@xuegod64 ~]# ifconfig tunl0 192.168.1.63 netmask 255.255.255.255
查看:
[root@xuegod64 ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:EF:3A:EF
。。。
tunl0 Link encap:IPIP Tunnel HWaddr
inet addr:192.168.1.63 Mask:255.255.255.255
UP RUNNING NOARP MTU:1480 Metric:1
生成ifcfg-tunl0配置文件:
[root@xuegod64 network-scripts]# cp ifcfg-lo ifcfg-tunl0
[root@xuegod64 network-scripts]# cat ifcfg-tunl0 #寫入以下內容
DEVICE=tunl0 IPADDR=192.168.1.63 NETMASK=255.255.255.0 # If you're having problems with gated making 127.0.0.0/8 a martian, # you can change this to something else (255.255.255.255, for example) ONBOOT=yes NAME=tunl0
[root@xuegod64 network-scripts]# service network restart
關閉ARP轉發。
echo '0' > /proc/sys/net/ipv4/ip_forward 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 '0' > /proc/sys/net/ipv4/conf/tunl0/rp_filter echo '0' > /proc/sys/net/ipv4/conf/all/rp_filter
永久生效:
[root@xuegod62 ~]# vim /etc/sysctl.conf #以文件最后添加以下內容
net.ipv4.conf.tunl0.arp_ignore = 1 net.ipv4.conf.tunl0.arp_announce = 2 net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.tunl0.rp_filter = 0 net.ipv4.conf.all.rp_filter = 0
[root@xuegod62 ~]# sysctl -p
配置web服務器
[root@xuegod64 ~]# yum install httpd
[root@xuegod64 network-scripts]# echo 192.168.1.64 > /var/www/html/index.html [root@xuegod64 ~]# service httpd restart
測試:
先測試realserver:
正常打開
注:不能在分發器上直接測試,需要去其他機器上測試
[root@xuegod65 ~]# elinks 192.168.1.63 –dump
F、NAT/DR/TUN比較
比較 |
TUN |
NAT |
DR |
配置復雜度 |
復雜 |
簡單 |
復雜 |
拓展性 |
很好 |
差 |
好 |
支持數量 |
>100 |
<20 |
>100 |
特點 |
封裝IP |
地址轉化 |
修改MAC |
支持OS |
需要支持IPIP隧道 |
Any |
大部分 |
三種模式下的簡單壓力測試
簡單的壓力測試采用Apache ab命令,500並發用戶,10w的請求總數。
[root@xuegod62 ~]# rpm -qf `which ab`
httpd-tools-2.2.15-15.el6.x86_64
結果如下:
LVS模式 總耗時(s) TPS(#/sec) TPS(Transaction Per Second)每秒系統處理事務的數量
NAT 22.480 4448.34
TUNNEL 10.707 9339.80
DR 10.177 9825.68
可以看出NAT性能要比后兩種差一倍。
三、實戰2:使用webbench測試網站性能
網站壓力測試工具-Webbench
一.webbench簡介:
Webbench是有名的網站壓力測試工具,它是由 Lionbridge公司(http://www.lionbridge.com)開發的網站壓力測試工具,它能測試處在相同硬件上,不同服務的性能以及不同硬件上同一個服務的運行狀況。webbench不但能具有便准靜態頁面的測試能力,還能對動態頁面(ASP,PHP,JAVA,CGI)進行測試的能力。
官網:http://www.lionbridge.com
二.在xuegod64上下載並安裝webbench:
[root@xuegod64~]# wget http://www.ha97.com/code/webbench-1.5.tar.gz
[root@xuegod64 webbench-1.5]#tar zxvf webbench-1.5.tar.gz
[root@xuegod64 webbench-1.5]#cd webbench-1.5
[root@xuegod64 webbench-1.5]#make
[root@xuegod64 webbench-1.5]# mkdir -p /usr/local/man/man1 #不創建這個執行make install報錯:
install: cannot create regular file `/usr/local/man/man1': No such file or directory
make: *** [install] Error 1
[root@xuegod64 webbench-1.5]#make install
install -s webbench /usr/local/bin
install -m 644 webbench.1 /usr/local/man/man1
install -d /usr/local/share/doc/webbench
install -m 644 debian/copyright /usr/local/share/doc/webbench
install -m 644 debian/changelog /usr/local/share/doc/webbench
三.進行壓力測試:在xuegod64上進行測試
[root@xuegod64 webbench-1.5]# webbench -h
測試參數解釋:
-c為客戶端數,-t為時間(秒)
實戰:當為1個客戶端時,持續訪問1秒。
[root@xuegod64 ~]# webbench -c 1 -t 1 http://192.168.1.63/index.html
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://192.168.1.63/index.html
1 client, running 1 sec.
Speed=38219 pages/min, 178080 bytes/sec.
#當只有一個客戶端時,一分鍾可以響應38219個頁面,1秒可以傳輸178080字節
Requests: 637 susceed, 0 failed.
#1個客戶端,1秒產生了637個請求,0個失敗。
實戰:使用20個客戶端並發訪問並持續訪問10秒
[root@xuegod64 ~]# webbench -c 20 -t 10 http://192.168.1.63/index.html
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://192.168.1.63/index.html
20 clients, running 10 sec.
Speed=397356 pages/min, 1854384 bytes/sec.
Requests: 66226 susceed, 0 failed.
同時查看xuegod64上機器性能:
[root@xuegod64 ~]# vmstat 1 20
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 537392 105636 356068 0 0 21 2 20 16 0 0 99 0 0
0 0 0 537368 105636 356068 0 0 0 0 42 39 0 0 100 0 0
0 0 0 537368 105636 356068 0 0 0 0 45 61 0 0 100 0 0
0 0 0 535796 105636 356068 0 0 0 0 126 101 0 1 99 0 0
9 0 0 535400 105636 356068 0 0 0 4 520 799 1 9 90 0 0
9 0 0 532896 105636 356124 0 0 0 0 6361 18112 14 84 2 0 0
15 0 0 528772 105636 356640 0 0 0 0 7470 23536 23 76 1 0 0
11 0 0 523508 105636 357360 0 0 0 0 7622 25163 21 79 0 0 0
25 0 0 515344 105636 358160 0 0 0 0 7974 27589 15 84 1 0 0
0 0 0 499788 105652 362472 0 0 0 0 122 42 0 0 100 0 0
1 0 0 500416 105652 362472 0 0 0 0 61 42 0 0 100 0 0
例:
當並發為800時:
webbench -c 800 -t 60 http://192.168.1.63/index.html
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://www.linuxidc.com/index.php
800 clients, running 60 sec.
Speed=39571 pages/min, 33104224 bytes/sec.
Requests: 38576 susceed, 995 failed.
四.測試注意事項:
1.壓力測試工作應該放到產品上線之前,而不是上線以后;
2.webbench 做壓力測試時,該軟件自身也會消耗CPU和內存資源,為了測試准確,請將 webbench 安裝在別的服務器上;
3.測試時盡量跨公網進行,而不是內網; 如果帶寬不夠時,可以內網測試。
4.測試時並發應當由小逐漸加大,觀察一下網站負載及打開是否流暢,直到網站打開緩慢甚至網站完全打不開; 可以一邊在linux測試,一個在瀏覽上打開,查看是否流暢。
5.應盡量進行單元測試,如B2C網站可以着重測試購物車、推廣頁面等,因為這些頁面占整個網站訪問量比重較大。