MySQL社区

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 13903|回复: 0

02- HAProxy应用举例及性能对比

[复制链接]
发表于 2010-9-6 15:24:57 | 显示全部楼层 |阅读模式
应用举例

WEB 均衡负载 & 虚拟主机
重新打开配置文件 haproxy.cfg,留意最下部分的均衡主机选项
listen  localhost 0.0.0.0:1080                   #运行的端口及主机名
   mode    http
   option  httpchk GET /index.htm              #用于健康检测的后端页面
   server  s1 127.0.0.1:3121 weight 3 check    #后端的主机 IP &权衡
   server  s2 127.0.0.1:3122 weight 3 check    #后端的主机 IP &权衡
在实验中,我们的的后端是 squid 分开了2个端口在同一台服务器上。
以其中一项为例:
server  s1 127.0.0.1:3121 weight 3 check
s1             是可自己定义的服务器别名
127.0.0.1:3121   服务器的IP地址以及端口号
weight 3        所能分配到请求的高低权衡,数字越大分配到的请求数就越高
check          接受 haproxy 的定时检查,以确定后端服务器的健康情况。
如需配置虚拟主机,相当简单,紧需修改 localhost 为你虚拟主机的的域名,加到haproxy配置中, 再为其分配后端服务器的参数即可。

例:
listen  www.x1.com 0.0.0.0:1080                    #运行的端口及主机名
   mode    http
   option  httpchk GET /index.htm              #用于健康检测的后端页面
   server  s1 127.0.0.1:3121 weight 3 check  #后端的主机 IP &权衡
   server  s2 127.0.0.1:3122 weight 3 check  #后端的主机 IP &权衡
listen  www.x2.com 0.0.0.0:1080                     #运行的端口及主机名
   mode    http
   option  httpchk GET /index.htm                     #用于健康检测的后端页面
   server  s1 127.0.0.1:3121 weight 3 check       #后端的主机 IP &权衡
   server  s2 127.0.0.1:3122 weight 3 check       #后端的主机 IP &权衡

保存配置后重新加载,即可生效,刷新管理页面也可看到新的虚拟主机。
性能对比
在此,我们用最近最火红的 http 兼前端WEB均衡负载服务器 Nginx Haproxy 做个简单的性能对比。
测试环境:
CPU:Xeon2.8G X2
RAM:4G
OS:RedHat As5.3 X64
工具:apache ab
参数:ab -i -c 500 -n 100000
(500并发,1W请求
)
最终服务端:2个squid 需实现均衡负载
成绩如下:
####### Nginx + haproxy :  (由Nginx通过反向代理发送请求至haproxy, 并由其进行均衡负载)
Concurrency Level:      500
Time taken for tests:   53.758 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      38600386 bytes
HTML transferred:       0 bytes

Requests per second:    1860.19 [#/sec] (mean)
Time per request:       268.790 [ms] (mean)
Time per request:       0.538 [ms] (mean, across all concurrent requests)
Transfer rate:          701.21 [Kbytes/sec] received
####### haproxy :  (单独由haproxy进行均衡负载)
Concurrency Level:      500
Time taken for tests:   32.562 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      36606588 bytes
HTML transferred:       0 bytes
Requests per second:    3071.02 [#/sec] (mean)
Time per request:       162.812 [ms] (mean)
Time per request:       0.326 [ms] (mean, across all concurrent requests)
Transfer rate:          1097.85 [Kbytes/sec] received
####### nginx : (单独由nginx进行均衡负载)
Concurrency Level:      500
Time taken for tests:   36.539 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      38600000 bytes
HTML transferred:       0 bytes
Requests per second:    2736.82 [#/sec] (mean)
Time per request:       182.694 [ms] (mean)
Time per request:       0.365 [ms] (mean, across all concurrent requests)
Transfer rate:          1031.65 [Kbytes/sec] received

反复测试,得出其结果:
Haproxy 单独进行均衡负载的性能最强,超过了Nginx。
然而 Nginx + Haproxy 的搭配性能最弱,应该是跟通过了2层反向代理有关。
所以想用 Haproxy 替代 Nginx 所自带的均衡负载功能将会令性能打折。
但虽然如此 Haproxy 对均衡负载功能远比 Nginx 成熟,例如session粘贴,cookies 引导等都是 nginx 所没有的。
可根据需要而选择搭配。
相关启动参数介绍
相关启动参数介绍

#./haproxy –help //haproxy
相关命令参数介绍.

haproxy
-f
<
配置文件>
[-n 最大并发连接总数] [-N 每个侦听的最大并发数] [-d] [-D] [-q] [-V] [-c] [-p <pid文件>] [-s] [-l] [-dk]

[-ds] [-de] [-dp] [-db] [-m <
内存限制M>] [{-sf|-st} pidlist...]

-d
前台,debug模式

-D
daemon
模式启动

-q
安静模式,不输出信息

-V
详细模式

-c
对配置文件进行语法检查

-s
显示统计数据

-l
显示详细统计数据

-dk
不使用kqueue

-ds
不使用speculative epoll

-de
不使用epoll

-dp
不使用poll

-db
禁用后台模式,程序跑在前台

-sf <pidlist>

程序启动后向pidlist里的进程发送FINISH信号,这个参数放在命令行的最后

-st <pidlist>

程序启动后向pidlist里的进程发送TERMINATE信号,这个参数放在命令行的最后
参考资源 (resources)
本文仅作为引子,Haproxy 配置以其功能远远不止这些。更多资料可到以下网站中获取。

·
Haproxy 中文 http://cn.haproxy.org
·
Haproxy 英文 http://www.haproxy.org
·
中国开源社区 http://www.oschina.net

本帖被以下淘专辑推荐:

QQ|申请友链|小黑屋|Archiver|手机版|MySQL社区 ( 京ICP备07012489号   
联系人:周生; 联系电话:13911732319

GMT+8, 2024-3-29 23:07 , Processed in 0.073413 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表