负载均衡工具haproxy安装,配置,使用
一,什么是haproxy
HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代 理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
二,安装haproxy
下载列表地址http://haproxy.1wt.eu
wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.8.tar.gz
tar zxvf haproxy-1.4.8.tar.gz
cd haproxy-1.4.8
uname -a //查看linux内核版本
make TARGET=linux26 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
三,配置haproxy
vi /usr/local/haproxy/haproxy.cfg
1 global
2 maxconn 5120
3 chroot /usr/local/haproxy
4 uid 99
5 gid 99
6 daemon
7 quiet
8 nbproc 2
9 pidfile /usr/local/haproxy/haproxy.pid
10 defaults
11 log global
12 mode http
13 option httplog
14 option dontlognull
15 log 127.0.0.1 local3
16 retries 3
17 option redispatch
18 maxconn 2000
19 contimeout 5000
20 clitimeout 50000
21 srvtimeout 50000
22
23 listen webinfo :1080
24 mode http
25 balance roundrobin
26 option httpclose
27 option forwardfor
28 server phpinfo1 192.168.18.2:10000 check weight 1 minconn 1 maxconn 3 check inter 40000
29 server phpinfo2 127.0.0.1:80 check weight 1 minconn 1 maxconn 3 check inter 40000
30
31 listen webmb :1081
32 mode http
33 balance roundrobin
34 option httpclose
35 option forwardfor
36 server webmb1 192.168.1.91:10000 weight 1 minconn 1 maxconn 3 check inter 40000
37 server webmb2 127.0.0.1:10000 weight 1 minconn 1 maxconn 3 check inter 40000
38
39 listen stats :8888
40 mode http
41 transparent
42 stats uri / haproxy-stats
43 stats realm Haproxy \ statistic
44 stats auth zhangy:xtajmd
三,启动HaProxy
#启动haproxy
/usr/local/haproxy/haproxy -f /usr/local/haproxy/haproxy.cfg
#查看是否启动
[zhangy@BlackGhost haproxy]$ ps -e|grep haproxy
4859 ? 00:00:00 haproxy
4860 ? 00:00:00 haproxy
四,压力测试
[root@BlackGhost haproxy]# /usr/local/bin/webbench -c 100 -t 30 http://localhost:1080/phpinfo.php
Webbench – Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://localhost:1080/phpinfo.php
100 clients, running 30 sec.
Speed=26508 pages/min, 20929384 bytes/sec.
Requests: 13254 susceed, 0 failed.
说明:haproxy监听的端口是1080,代理192.168.18.2:10000,127.0.0.1:10000
统计监听的是8888端口 http://localhost:8888/haproxy-stats
配置说明:
1.4系列参考配置文件
http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
1.3系列参考配置文件