实验环境:
主机名 | IP | 所需软件 |
master |
eth0==>192.168.30.160(RIP) eth0:1==>192.168.30.130(VIP) |
ipvsadm |
node-1 | eth0==>192.168.30.131 | httpd |
node-2 | eth0==>192.168.30.132 |
在master上
安装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)
[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
配置VIP
[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
使立即生效
[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