您的位置:首页 > 其它

高性能负载均衡软件 HAProxy

2015-08-09 23:23 741 查看
HAProxy是一个开源的、高性能的、基于TCP(四层)和HTTP(七层)应用的负载均衡软件
HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。它的显著优点如下:
1、可靠性和稳定性好
2、支持多达9种负载均衡算法
3、支持虚拟主机和ACL规则
4、能生成服务器状态监控页面

HAProxy原理



环境:1、HaProxy
eth0:192.168.18.10eth1:192.168.1.10

2、WebSrv1
eth1:192.168.1.11

3、WebSrv2
eth1:192.168.1.12

4、Client
eth1:192.168.18.100


一、安装Haproxy

A、yum安装

yuminstallhaproxy-y

B、编码编译

1、为支持正则,必须安装pcre

wgetftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.bz2tar-jxfpcre-8.36.tar.bz2
cdpcre-8.36
./configure
make&&makeinstall

2、Haproxy官网在国内已被墙,可在github下载wgethttps://github.com/haproxy/haproxy/archive/v1.5.0.tar.gz-Ohaproxy-v1.5.0.tar.gz
tar-zxfhaproxy-v1.5.0.tar.gzcdhaproxy-1.5.0/

makeTARGET=linux2628ARCH=x86_64USE_PCRE=1PREFIX=/usr/local/haproxy#编译
makeinstallPREFIX=/usr/local/haproxy#安装到指定路径
mkdir{/etc/haproxy,/var/haproxy,/var/log/haproxy}#创建相关目录

cpexamples/haproxy.cfg/etc/haproxy/#复制示例配置文件
cp-aexamples/haproxy.init/etc/init.d/haproxy#复制启动脚本
cpdoc/haproxy.1/usr/share/man/man1/
cpdoc/configuration.txt/usr/share/man/man1/haproxy.cfg.1#复制man文件

cp-arexamples/errorfiles//etc/haproxy#复制错误提示文件
chmoda+x/etc/init.d/haproxy
chkconfig--addhaproxychkconfighaproxyon

ln-s/usr/local/haproxy/sbin/haproxy/usr/sbin/haproxy
ln-s/usr/local/lib/libpcre.so.1/usr/lib64/libpcre.so.1

二、负载均衡实例

1、主配置文件

vim/etc/haproxy/haproxy.cfg

global#全局参数
log127.0.0.1local3info#log记录级别
maxconn20480#最大并发连接数
chroot/var/haproxy
uid99
gid99
daemon#后台运行
nbproc3#创建6个进程(建议设置为CPU核数)
pidfile/var/run/haproxy.pid
ulimit-n65535
statssocket/var/tmp/haproxy.stats
#debug
#quiet

defaults#默认参数
logglobal
cookieSRV#向cookie插入SERVERID,会话保持
modehttp#http7层模式,可配置tcp4层模式
optionhttplog#记录http请求日志
optionhttpclose#请求完毕后主动关闭http通道
optiondontlognull#不记录空log
optionforwardfor#记录ClientIP
optionredispatch#自动重定向到健康机器
optionallbackups#服务器宕机时,激活备机
#optionabortonclose#高负载时,自动关闭连接长的请求
statsrefresh30#统计页面刷新间隔
retries3#连接RS超时次数
maxconn20480
timeoutcheck2000ms#检查超时
timeoutconnect5000ms#连接超时
timeoutclient50000ms#客户端连接超时
timeoutserver50000ms#服务端连接超时

listenamin_status#状态管理
bind192.168.18.10:6553#服务IP:端口
modehttp
log127.0.0.1local3info
statsenable
statsrefresh5s#自动刷新间隔
statsrealmHaproxy\Statistics#认证信息
statsuri/admin?stats#URL页面提示符
statsauthadmin01:admin01#认证用户admin01,密码admin01
statsauthadmin02:admin02
statsadminifTRUE#支持启/禁用RS服务器
statshide-version#隐藏版本号

frontendweb_service#前端虚拟节点
bind*:80#提供服务IP:端口
modehttp
logglobal

aclinside_srcsrc10.0.2.0/24#定义ACL,支持正则
aclbad_netsrc192.168.2.0/24
#acltest.org_aclhdr_req(host)-i^(www.test.org|web.test.org)$
http-requestdenyifbad_net#拒绝bad_net
#use_backendexternal_serversiftest.org_acl
use_backendinside_serversifinside_src#如果源主机是inside_src,定向到inside_servers
default_backendexternal_servers#默认服务组

backendexternal_servers#后端对外服务组,名字随便取
modehttp
balanceroundrobin#轮询调度
optionhttpchkGET/index.html#健康检查页面

#主机名IP:端口健康检查频率(ms)连续2次成功则认为正常连续3次失败则认为宕机权重1
serverweb01192.168.1.11:80cookieweb01checkinter2000rise2fall3weight1
serverweb02192.168.1.12:80cookieweb02checkinter2000rise2fall3weight1

backendinside_servers#对内服务组
modehttp
balanceroundrobin
optionhttpchkGET/index.html#启用对RS的状态检测
serverweb01192.168.1.11:80cookieweb01checkinter2000rise2fall3weight1

errorloc502/etc/haproxy/errorfiles/502.http
errorfile503/etc/haproxy/errorfiles/503.http

启动HaProxy
haproxy-f/etc/haproxy/haproxy.cfg或/etc/init.d/haproxystart

2、负载均衡算法

Haproxy支持的负载均衡算法:
1、roundrobin#轮询调度,
2、static-rr#基于权重,静态算法
3、leastconn#最少连接数,适用于长会话连接,如:LDAP,SQL,TSE,etc...
4、first#首次连接,总是使用最小连接
5、source#源地址,再除权重总数,常用6、uri#对URL进行hash运算,再除服务器总权重
7、url_param#根据URl路径中的参数
8、hdr#根据HTTP请求头,不存在则用RR算法
9、rdp-cookie#基于客户端Cookie,hash每次请求

3、日志支持

vim/etc/rsyslog.conf#追加到rsyslog配置

#ProvidesUDPsyslogreception
$ModLoadimudp#载入UDP模块
$UDPServerRun514#监听端口

#Haproxylog
local3.*/var/log/haproxy/haproxy.log#log位置

重启rsyslog
servicersyslogrestart
Haproxy优雅重启
haproxy-f/etc/haproxy/haproxy.cfg-sf`cat/var/run/haproxy.pid`

三、Web服务器配置(以Web1为例)
yuminstallhttpd-y#安装Apache
echo"Web-01">/var/www/html/index.htmlservicehttpdstart#启动httpd服务

四、测试

1、打开浏览器访问:http://192.168.18.10:6553/admin?stats输入认证用户登录





HAProxy的Web监控页面



更多参考:

haproxy-1.5.0/doc/configuration.txt

https://github.com/haproxy/haproxy/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  haproxy