實驗環境:html
主機名 | IP | 所需軟件 |
master | eth0==>192.168.30.160(RIP)node eth0:1==>192.168.30.130(VIP)linux |
ipvsadm |
node-1 | eth0==>192.168.30.131 | httpd |
node-2 | eth0==>192.168.30.132 |
在master上web
安裝ipvsadm [root@master ~]# yum install -y ipvsadm 配置網卡 [root@master ~]# cd /etc/sysconfig/network-scripts/ [root@master network-scripts]# cp ifcfg-eth0 ifcfg-eth0:1 [root@master network-scripts]# vim ifcfg-eth0:1 DEVICE=eth0:1 TYPE=Ethernet UUID=a186cc9d-8d79-4027-a180-a1b17d884a7b ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none HWADDR=00:0C:29:4D:47:50 IPADDR=192.168.30.130 PREFIX=24 GATEWAY=192.168.30.2 DNS1=119.29.29.29 DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME="System eth0:1" 重啓網絡服務並查看 [root@master ~]# service network restart Shutting down interface eth0: Device state: 3 (disconnected) [ OK ] Shutting down loopback interface: [ OK ] Bringing up loopback interface: [ OK ] Bringing up interface eth0: Active connection state: activated Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/6 [ OK ] [root@master ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:4D:47:50 inet addr:192.168.30.160 Bcast:192.168.30.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe4d:4750/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2254 errors:0 dropped:0 overruns:0 frame:0 TX packets:1860 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:211141 (206.1 KiB) TX bytes:170852 (166.8 KiB) eth0:1 Link encap:Ethernet HWaddr 00:0C:29:4D:47:50 inet addr:192.168.30.130 Bcast:192.168.30.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:16 errors:0 dropped:0 overruns:0 frame:0 TX packets:16 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:960 (960.0 b) TX bytes:960 (960.0 b) 添加規則 [root@master ~]# ipvsadm -C [root@master ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn [root@master ~]# ipvsadm -A -t 192.168.30.130:80 -s rr [root@master ~]# ipvsadm -a -t 192.168.30.130:80 -r 192.168.30.131 -i [root@master ~]# ipvsadm -a -t 192.168.30.130:80 -r 192.168.30.132 -i [root@master ~]# /etc/init.d/ipvsadm save ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm: [ OK ] [root@master ~]# 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.30.130:80 rr -> 192.168.30.131:80 Tunnel 1 0 0 -> 192.168.30.132:80 Tunnel 1 0 0
在realserver上(node-1和node-2)apache
[root@node-2 ~]# modprobe ipip #加載ipip模塊後使用ifconfig會看到默認的tunl0隧道 [root@node-2 ~]# ifconfig -a eth0 Link encap:Ethernet HWaddr 00:50:56:3B:FA:94 inet addr:192.168.30.131 Bcast:192.168.30.255 Mask:255.255.255.0 inet6 addr: fe80::250:56ff:fe3b:fa94/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:699 errors:0 dropped:0 overruns:0 frame:0 TX packets:368 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:54921 (53.6 KiB) TX bytes:33282 (32.5 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:12 errors:0 dropped:0 overruns:0 frame:0 TX packets:12 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:720 (720.0 b) TX bytes:720 (720.0 b) lo:1 Link encap:Local Loopback inet addr:192.168.30.130 Mask:255.255.255.255 UP LOOPBACK RUNNING MTU:16436 Metric:1 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@node-2 ~]# lsmod | grep ipip ipip 8371 0 tunnel4 2943 1 ipip
配置VIPvim
[root@node-1 ~]# cd /etc/sysconfig/network-scripts/ [root@node-1 network-scripts]# cp ifcfg-lo ifcfg-tunl0 [root@node-1 network-scripts]# vim ifcfg-tunl0 DEVICE=tunl0 IPADDR=192.168.30.130 NETMASK=255.255.255.250 # 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 #這裏遇到一個問題不知道各位在作的過程當中有沒有遇到過,就是這裏配置完VIP後,直接ifconfig 就會看到結果,可是不能夠重啓網絡服務,一旦重啓,VIP就沒有了,尷尬。。。
查看VIP瀏覽器
[root@node-1 ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:50:56:3B:FA:94 inet addr:192.168.30.131 Bcast:192.168.30.255 Mask:255.255.255.0 inet6 addr: fe80::250:56ff:fe3b:fa94/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:58 errors:0 dropped:0 overruns:0 frame:0 TX packets:77 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:7455 (7.2 KiB) TX bytes:8386 (8.1 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:4 errors:0 dropped:0 overruns:0 frame:0 TX packets:4 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:240 (240.0 b) TX bytes:240 (240.0 b) tunl0 Link encap:IPIP Tunnel HWaddr inet addr:192.168.30.130 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)
關閉ARP轉發服務器
[root@node-1 ~]# 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
使當即生效cookie
[root@node-1 ~]# sysctl -p net.ipv4.ip_forward = 0 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.default.accept_source_route = 0 kernel.sysrq = 0 kernel.core_uses_pid = 1 net.ipv4.tcp_syncookies = 1 error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key error: "net.bridge.bridge-nf-call-iptables" is an unknown key error: "net.bridge.bridge-nf-call-arptables" is an unknown key kernel.msgmnb = 65536 kernel.msgmax = 65536 kernel.shmmax = 68719476736 kernel.shmall = 4294967296 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
部署web服務器網絡
[root@node-1 ~]# yum install -y httpd #安裝httpd服務 建立測試頁面 [root@node-1 ~]# cat /var/www/html/index.html <h1>This is node-1 WebServer:192.168.30.131</h1> 啓動httpd服務 [root@node-1 ~]# service httpd start Starting httpd: [ OK ] 設置開機啓動 [root@node-1 ~]# chkconfig httpd on
在node-2上作相同操做;
而後在物理機上使用物理機瀏覽器首先測試node1和node-2是否能夠正常訪問
而後測試director(master:192.168.30.130)
按F5刷新
網站壓測:(ab命令:須要http-tools支持)
ab命令經常使用參數:
用法: ab [options] [http[s]://]hostname[:port]/path 參數: -A auth-username:password 對服務器提供BASIC認證信任。 用戶名和密碼由一個:隔開,並以base64編碼形式發送。 不管服務器是否須要(即, 是否發送了401認證需求代碼),此字符串都會被髮送。 -c concurrency 一次產生的請求個數。默認是一次一個。 -C cookie-name=value 對請求附加一個Cookie:行。 其典型形式是name=value的一個參數對。 此參數能夠重複。 -d 顯示"percentage served within XX [ms] table"的消息(爲之前的版本提供支持)。 -e csv-file 產生一個以逗號分隔的(CSV)文件, 其中包含了處理每一個相應百分比的請求所須要(從1%到100%)的相應百分比的(以微妙爲單位)時間。 因爲這種格式已經「二進制化」,因此比'gnuplot'格式更有用。 -g gnuplot-file 把全部測試結果寫入一個'gnuplot'或者TSV (以Tab分隔的)文件。 此文件能夠方便地導入到Gnuplot, IDL, Mathematica, Igor甚至Excel中。 其中的第一行爲標題。 -h 顯示使用方法。 -H custom-header 對請求附加額外的頭信息。 此參數的典型形式是一個有效的頭信息行,其中包含了以冒號分隔的字段和值的對 (如, "Accept-Encoding: zip/zop;8bit"). -i 執行HEAD請求,而不是GET。 -k 啓用HTTP KeepAlive功能,即, 在一個HTTP會話中執行多個請求。 默認時,不啓用KeepAlive功能. -n requests 在測試會話中所執行的請求個數。 默認時,僅執行一個請求,但一般其結果不具備表明意義。 -p POST-file 包含了須要POST的數據的文件. -P proxy-auth-username:password 對一箇中轉代理提供BASIC認證信任。 用戶名和密碼由一個:隔開,並以base64編碼形式發送。 不管服務器是否須要(即, 是否發送了401認證需求代碼),此字符串都會被髮送。 -q 若是處理的請求數大於150, ab每處理大約10%或者100個請求時,會在stderr輸出一個進度計數。 此-q標記能夠抑制這些信息。 -s 用於編譯中(ab -h會顯示相關信息)使用了SSL的受保護的https, 而不是http協議的時候。此功能是實驗性的,也是很簡陋的。最好不要用。 -S 不顯示中值和標準背離值, 並且在均值和中值爲標準背離值的1到2倍時,也不顯示警告或出錯信息。 默認時,會顯示 最小值/均值/最大值等數值。(爲之前的版本提供支持). -t timelimit 測試所進行的最大秒數。其內部隱含值是-n 50000。 它能夠使對服務器的測試限制在一個固定的總時間之內。默認時,沒有時間限制。 -T content-type POST數據所使用的Content-type頭信息。 -v verbosity 設置顯示信息的詳細程度 - 4或更大值會顯示頭信息, 3或更大值能夠顯示響應代碼(404, 200等), 2或更大值能夠顯示警告和其餘信息。 -V 顯示版本號並退出。 -w 以HTML表的格式輸出結果。默認時,它是白色背景的兩列寬度的一張表。 -x <table>-attributes 設置<table>屬性的字符串。 此屬性被填入<table 這裏 >. -X proxy[:port] 對請求使用代理服務器。 -y <tr>-attributes 設置<tr>屬性的字符串. -z <td>-attributes 設置<td>屬性的字符串. 該段內容摘自:http://linux.51yip.com/search/ab
單獨對node-1直接進行
[root@xueji ~]# ab -c 10 -n 1000 http://192.168.30.131/ This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 192.168.30.131 (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Completed 1000 requests Finished 1000 requests Server Software: Apache/2.2.15 Server Hostname: 192.168.30.131 Server Port: 80 Document Path: / Document Length: 49 bytes Concurrency Level: 10 Time taken for tests: 0.123 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 317317 bytes HTML transferred: 49049 bytes Requests per second: 8121.56 [#/sec] (mean) Time per request: 1.231 [ms] (mean) Time per request: 0.123 [ms] (mean, across all concurrent requests) Transfer rate: 2516.71 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.2 0 2 Processing: 0 1 0.6 1 4 Waiting: 0 1 0.5 1 4 Total: 1 1 0.7 1 5 Percentage of the requests served within a certain time (ms) 50% 1 66% 1 75% 1 80% 2 90% 2 95% 2 98% 4 99% 4 100% 5 (longest request)
對master(Director進行測試)
[root@xueji ~]# ab -c 1000 -n 1000 -q http://192.168.30.130/ #對192.168.30.130進行1000次請求,併發用戶1000 This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 192.168.30.130 (be patient).....done Server Software: Apache/2.2.15 Server Hostname: 192.168.30.130 Server Port: 80 Document Path: / Document Length: 49 bytes #測試的站點文件大小(即index.html的文件大小) Concurrency Level: 1000 #併發請求總數 Time taken for tests: 1.514 seconds #測試總用時1.514秒 Complete requests: 1000 #測試的總請求數 Failed requests: 0 #失敗的請求數 Write errors: 0 Total transferred: 317000 bytes #測試共傳輸的總字節數 HTML transferred: 49000 bytes #測試共傳輸的HTML字節數 Requests per second: 660.30 [#/sec] (mean) #每秒處理的請求數 Time per request: 1514.463 [ms] (mean) #每一個請求平均花費的時間 Time per request: 1.514 [ms] (mean, across all concurrent requests) #平均每一個請求花費的時間 Transfer rate: 204.41 [Kbytes/sec] received #平均每秒傳輸字節數 Connection Times (ms) min mean[+/-sd] median max #左-->右 最小值 平均值 中位數 最大值 Connect: 1 20 88.1 13 1003 #鏈接 Processing: 0 427 411.4 235 1495 #傳輸中 Waiting: 0 427 411.4 235 1495 #等待 Total: 16 447 413.1 248 1510 #總計 Percentage of the requests served within a certain time (ms) #每一個請求完成的百分進度 50% 248 66% 650 75% 656 80% 670 90% 677 95% 1506 98% 1508 99% 1509 100% 1510 (longest request)
使用webbench測試
安裝過程 [root@node-1 ~]# yum install -y lrzsz [root@node-1 ~]# rz [root@node-1 ~]# ll -h webbench-1.5.tar.gz -rw-r--r-- 1 root root 7.5K Sep 26 2017 webbench-1.5.tar.gz [root@node-1 ~]# tar -xf webbench-1.5.tar.gz -C /usr/local/src/ [root@node-1 ~]# cd /usr/local/src/webbench-1.5/ [root@node-1 webbench-1.5]# ls ChangeLog COPYRIGHT debian Makefile socket.c webbench.1 webbench.c [root@node-1 webbench-1.5]# mkdir -p /usr/local/man/man1 #不建立該目錄會報錯 [root@node-1 webbench-1.5]# make -j 4;make install [root@node-1 webbench-1.5]# cd [root@node-1 ~]# webbench #敲下web直接能夠tab出來
webbench參數解釋
[root@node-1 ~]# webbench --help webbench [option]... URL -f|--force 不等待服務器的回覆 -r|--reload 發送從新加載請求 -t|--time <sec> 在<秒>秒內運行基準測試。 默認30。 -p|--proxy <server:port> 使用代理服務器進行請求。 -c|--clients <n> 當即運行<n>個 HTTP客戶端。 默認一個. -9|--http09 使用HTTP / 0.9樣式請求。 -1|--http10 使用HTTP / 1.0樣式請求。 -2|--http11 使用HTTP / 1.1樣式請求。 --get 使用GET請求方法。 --head 使用HEAD請求方法。 --options 使用OPTIONS請求方法. --trace 使用TRACE請求方法. -?|-h|--help 幫助信息. -V|--version 程序版本
測試
[root@node-1 ~]# webbench -c 1 -t 1 http://192.168.30.130/index.html Webbench - Simple Web Benchmark 1.5 Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software. Benchmarking: GET http://192.168.30.130/index.html 1 client, running 1 sec. Speed=494039 pages/min, 2609861 bytes/sec. Requests: 8234 susceed, 0 failed.
說明:speed:只有一個客戶端時,每分鐘能夠響應404039個頁面,1秒能夠傳輸2609861字節
使用100個客戶端併發訪問持續20秒
[root@node-1 ~]# webbench -c 100 -t 20 http://192.168.30.130/index.html Webbench - Simple Web Benchmark 1.5 Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software. Benchmarking: GET http://192.168.30.130/index.html 100 clients, running 20 sec. Speed=1847763 pages/min, 9762538 bytes/sec. Requests: 615921 susceed, 0 failed 同時在另個終端查看主機性能 [root@node-1 ~]# 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 1 0 0 3482756 40532 297936 0 0 19 5 44 209 0 0 99 0 0 3 0 0 3481460 40532 298804 0 0 0 0 20403 41605 0 17 82 0 0 5 0 0 3478560 40532 300036 0 0 0 0 19808 59306 1 35 64 0 0 8 0 0 3473568 40532 301708 0 0 0 0 18151 77742 1 51 48 0 0 16 0 0 3463204 40540 304988 0 0 0 16 28181 139514 2 93 6 0 0 45 0 0 3432956 40540 308656 0 0 0 0 19593 153860 12 88 0 0 0 45 1 0 3429020 40540 312336 0 0 0 0 8867 157282 32 68 0 0 0 32 0 0 3436584 40540 316224 0 0 0 0 9355 163362 29 71 0 0 0 34 0 0 3432944 40540 319652 0 0 0 0 27214 148942 7 93 0 0 0 3 0 0 3453884 40548 322820 0 0 0 12 25399 137045 6 91 3 0 0 5 0 0 3452728 40548 324660 0 0 0 0 32391 86854 1 53 46 0 0 9 0 0 3447364 40548 326532 0 0 0 0 20493 85889 1 55 44 0 0 17 0 0 3437380 40548 329708 0 0 0 0 26920 134748 15 77 8 0 0 48 0 0 3407976 40548 332460 0 0 0 0 17127 118638 21 79 1 0 0 80 0 0 3376820 40556 335548 0 0 0 12 5116 110276 30 69 0 0 0 34 0 0 3371488 40556 339068 0 0 0 0 8281 140303 25 75 1 0 0 73 0 0 3370604 40556 342424 0 0 0 0 7113 147097 25 75 0 0 0 61 0 0 3376876 40556 346212 0 0 0 0 7174 160293 27 73 0 0 0 65 0 0 3374080 40556 350324 0 0 0 0 6490 162785 32 68 0 0 0 0 0 0 3381008 40564 353284 0 0 0 12 6521 128456 25 53 22 0 0
逐漸增大併發量,當併發量1000時
[root@node-1 ~]# webbench -c 1000 -t 20 http://192.168.30.130/index.html Webbench - Simple Web Benchmark 1.5 Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software. Benchmarking: GET http://192.168.30.130/index.html 1000 clients, running 20 sec. Speed=1635798 pages/min, 8641974 bytes/sec. Requests: 545233 susceed, 33 failed. [root@node-1 ~]# 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 1 0 0 3313028 40628 356276 0 0 18 9 67 372 0 1 99 0 0 6 0 0 3308004 40628 357628 0 0 0 0 20419 70283 1 42 57 0 0 2 0 0 3304516 40628 359020 0 0 0 0 21071 70183 1 45 54 0 0 9 0 0 3289708 40628 360908 0 0 0 0 13852 70751 1 46 53 0 0 17 0 0 3273424 40636 364116 0 0 0 24 25322 139485 18 82 1 0 0 37 0 0 3264180 40636 367232 0 0 0 0 22773 137549 17 83 0 0 0 17 0 0 3248696 40636 370704 0 0 0 0 19246 136027 13 87 0 0 0 41 0 0 3234320 40636 373816 0 0 0 0 16732 128157 12 88 0 0 0 36 0 0 3228832 40636 376724 0 0 0 0 19354 132201 6 94 0 0 0 38 0 0 3212460 40644 380184 0 0 0 12 14808 137315 13 87 0 0 0 40 0 0 3198872 40644 383476 0 0 0 0 9758 144304 26 74 0 0 0 68 0 0 3190896 40644 386720 0 0 0 0 12924 134807 23 77 0 0 0 48 0 0 3192704 40644 390388 0 0 0 7700 8086 151370 27 73 0 0 0 24 0 0 3195392 40652 393660 0 0 8 28084 8579 139409 24 76 0 0 0 28 0 0 3182800 40660 396880 0 0 0 4660 20907 131704 8 91 1 0 0 19 0 0 3222080 40660 399756 0 0 0 0 22909 128567 4 96 0 0 0 1 0 0 3232284 40660 402016 0 0 0 0 30377 105320 10 65 25 0 0 9 0 0 3228392 40660 403744 0 0 0 0 26083 81361 4 47 49 0 0 19 0 0 3218392 40660 406228 0 0 0 0 29428 114541 5 76 19 0 0 0 0 0 3305920 40668 407160 0 0 0 16 5679 29488 2 21 76 0 0