ApacheServer2
/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
IPADDR=192.168.100.3
NETMASK=255.255.255.0
ONBOOT=yes
/etc/sysconfig/network-scripts/ifcfg-lo:100
DEVICE=lo:100
IPADDR=192.168.100.200
NETMASK=255.255.255.255
ONBOOT=yes
service network restart
两台ApacheServer都需要关闭 arp
/etc/sysctl.conf中增加
#foripvs
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.rp_filter = 1
#end of ipvs
sysctl -p使其生效
1.9 Ldirectord的配置
chkconfig --add ldirectord
chkconfig –level 35 ldirectord on
vi /etc/ha.d/ldirectord.cf
# Global Directives
checktimeout=3
checkinterval=1
#fallback=127.0.0.1:80
autoreload=yes
#logfile="/var/log/ldirectord.log"
#logfile="local0"
#emailalert="admin@x.y.z"
#emailalertfreq=3600
#emailalertstatus=all
quiescent=yes
# Sample for an http virtual service
virtual=192.168.100.200:80
real=192.168.100.2:80 gate
real=192.168.100.3:80 gate
fallback=127.0.0.1:80 gate
service=http
request=".healthcheck.html"
receive="OKAY"
# virtualhost=some.domain.com.au
scheduler=rr
#persistent=600
#netmask=255.255.255.255
protocol=tcp
checktype=negotiate
checkport=80
# request="index.html"
# receive="Test Page"
# virtualhost=www.x.y.z
ApacheServer上需要增加检查文件
echo "OKAY" > /usr/local/apache2/htdocs/.healthcheck.html
启动ldirectord
/etc/rc.d/init.d/ ldirectord start
如果启动遇到错误starting ldirectord... Can't locate Mail/Send.pm ,则需要安装下面两个包:
rpm -ivh perl-TimeDate-1.16-1.2.el5.rf.noarch.rpm
rpm -ivh perl-MailTools-1.77-1.el5.noarch.rpm
1.10 测试
1.10.1 测试负载均衡
访问http://192.168.100.200/tests.php,第一次显示
This is 100.2!
1261385691
1261387336
eqqrp3g8ko3i9bqkr40tmo9pf1
过一会儿再刷新,显示
This is 100.3! 1261385691
1261387378
eqqrp3g8ko3i9bqkr40tmo9pf1
说明负载均衡正常
1.10.2 测试故障转移
查看LVS当前状态
ipvsadm –Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.100.200:80 rr
-> 192.168.100.2:80 Route 1 0 0
-> 192.168.100.3:80 Route 1 0 0
Weight都为1,说明两个服务器正常
停掉100.3上的apache再看
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.100.200:80 rr
-> 192.168.100.2:80 Route 1 0 0
-> 192.168.100.3:80 Route 0 0 0
100.3的Weight为0,说明100.3已经被LVS从服务器列表中暂时排除掉
启动100.3上的apache再看
IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.100.200:80 rr
-> 192.168.100.2:80 Route 1 0 0
-> 192.168.100.3:80 Route 1 0 0
100.3被重新加入到列表中。
1.10.3 测试性能
单机
ab -n 20000 -c 1000 http://192.168.100.2/tests.php
Requests per second: 1561.95 [#/sec] (mean)
Time per request: 640.224 [ms] (mean)
Time per request: 0.640 [ms] (mean, across all concurrent requests)
Transfer rate: 701.63 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 32 340.0 2 8999
Processing: 1 242 1185.6 32 12740
Waiting: 0 238 1186.1 27 12740
Total: 18 275 1242.3 34 12801
Percentage of the requests served within a certain time (ms)
50% 34
66% 36
75% 37
80% 38
90% 44
95% 660
98% 6272
99% 6331
100% 12801 (longest request)
ab -n 20000 -c 1000 http://192.168.100.3/tests.php
Requests per second: 3142.92 [#/sec] (mean)
Time per request: 318.175 [ms] (mean)
Time per request: 0.318 [ms] (mean, across all concurrent requests)
Transfer rate: 1411.80 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 28 265.4 3 3008
Processing: 1 193 819.5 36 6300
Waiting: 0 186 820.3 28 6299
Total: 22 222 866.8 39 6362
ab -n 20000 -c 1000 http://192.168.100.3/tests.php
Percentage of the requests served within a certain time (ms)
50% 39
66% 41
75% 43
80% 45
90% 242
95% 670
98% 3116
99% 6325
100% 6362 (longest request)
双机 ab -n 20000 -c 1000 http://192.168.100.200/tests.php
Requests per second: 4858.15 [#/sec] (mean)
Time per request: 205.840 [ms] (mean)
Time per request: 0.206 [ms] (mean, across all concurrent requests)
Transfer rate: 2182.28 [Kbytes/sec] received
Connection Times (ms) min mean[+/-sd] median max
Connect: 0 43 320.5 5 3041
Processing: 1 124 322.2 64 3109
Waiting: 0 108 321.7 45 3094
Total: 7 167 456.1 69 3310
Percentage of the requests served within a certain time (ms)
50% 69
66% 86
75% 97
80% 105
90% 274
95% 631
98% 3031
99% 3105
100% 3310 (longest request)
双机的性能基本是单机的总和,说明LVS做负载均衡对性能基本没有消耗。