因为linux内核2.6.25-28兼容haproxy1.4有bug,建议在2.6.28以上版本OS安装,例如centos6.5(2.6.32)。linux
#tar zxvf haproxy-1.4.24.tar.gz
#cd haproxy-1.4.24
#make TARGET=linux2628 PREFIX=/path
#make install PREFIX=/pathweb
注:linux2628表示 内核版本>=2.6.28redis
也可使用rpmbuild源码包为rpm包,rpmbuild须要使用的haproxy.spec文件在源码包example目录下。后端
安装包中的haproxy.conf为1.2版本,其中一些参数已经在高版本停用。配置参考下面的配置文档,不要在默认的haproxy.conf基础上修改。centos
源码包中包含一个stats_haproxy.sh示例脚本,使用脚本须要先安装socat软件包。
haproxy配置主要包括全局配置和代理配置两部分,配置具体说明以下。浏览器
####全局设置
global
###进程管理和安全
##设置log,本机syslog需修改配置容许接收远程log。默认不开启,由于log会记录每个请求,影响性能。
#log 127.0.0.1 local3 info
##启动haproxy进程的用户,99为nobody用户
uid 99
##启动haproxy进程的用户,99为nobody组
gid 99
##后台运行haproxy
daemon
##number process,仅在daemon模式使用,官方建议1个。多个进程,调试会比较困难。
nbproc 1
##pidfile存放位置
pidfile /var/run/haproxy.pid
##开启统计接口,可使用hatop或socat得到统计信息。
stats socket /var/run/haproxy.stat安全
###性能优化参数(性能优化参数还有不少,其它参数官方建议使用默认)
##最大链接数
maxconn 65535
##spread-checks参数增长随机性检查的时间间隔,在0 和 +/- 50%之间。官方建议在2和5之间,默认值是0。能够避免并发检查多台服务器,尤为是后端服务器比较多的状况。
spread-checks 3性能优化
####代理设置
###缺省设置
defaults
##采用全局定义的日志
log global
##工做模式,还能够设置tcp等
mode http
##传输大文件 时,提早记录日志。
option logasap
##被动关闭
option httpclose
##不记录空链接,空链接为上层负载均衡设备健康检查的心跳包。
option dontlognull
##HAProxy能够向发往服务器的全部请求中添加HTTP头部字段“X-Forwarded-For”。该头部字段的值表明客户端的IP地址。
option forwardfor
##链接两端tcpkeepalive
option tcpka
##client keepalive
option clitcpka
## server keepalive
option srvtcpka
##链接失败后重试的次数
retries 3
##链接失败从新调度分发会话
option redispatch
##最大链接数
maxconn 30000
##链接超时
timeout connect 5000ms
##timeout client和server的值要保持一致,官方示例为50s。实际使用根据应用程序需求设置。
timeout client 50s
timeout server 50s
##开启监控统计,监控统计访问URI
stats uri /haproxy-stats
##认证密码框提示文本
stats realm haproxy\ statistics
##认证使用的用户和密码
stats auth admin:pasSWord
##隐藏haproxy版本
stats hide-version服务器
###前端设置
frontend public
## 绑定IP和端口
bind :80
##工做模式
mode http
##最大链接数
maxconn 30000
##上层负载均衡设备心跳检查使用的URL, 直接返回HTTP状态代码,不转发给后端的server。
monitor-uri /monitoruri
##关键字定义,根据定义的关键字选择backend组,中间的i是忽略大小写。
#关键字定义泛域名使用符号 .* 匹配
reqisetbe ^Host:\ test.test.com test
reqisetbe ^Host:\ .*.web.test.com web
注:在版本1.5中 “reqisetbe”参数被“use_backend” 替代。
##默认的后端组
default_backend test
###后端设置
backend test
##负载均衡策略 ,按照客户端源地址哈希,客户端会始终访问同一个server。
balance source
##容许插入serverid到cookie中,serverid在后面能够定义
cookie SERVERID insert indirect
##cookie A表示serverid为A;check port 80检查80端口,inter 2000 检查间隔为2000ms,rise 2表示2次正确服务器可用,fall 2表示2次检查失败服务器不可用。
server test1 192.168.52.48:80 cookie A check port 80 inter 2000 rise 2 fall 2
前端和后端配置项能够在listen配置项中一块儿设置,配置示例以下。
listen tcpserver
bind :8888
mode tcp
server tcpserver1 192.168.52.48:80 check port 80 inter 2000 rise 2 fall 2
使用rpm包安装,服务器管理脚本安装在/etc/rc.d/init.d/haproxy,用法以下。
haproxy {start|stop|restart|reload|condrestart|status|check}
一、使用服务管理脚本检查
#/etc/rc.d/init.d/haproxy check
注:只能检查当前使用的配置文件。
二、使用命令检查
#haproxy –c –f
在配置文件里面配置了统计页面,能够经过浏览器访问。
修改/etc/haproxy/haproxy.cfg,增长下面几行:
listen admin_stats
bind 0.0.0.0:1080
mode http
log 127.0.0.1 local0 err
stats uri/admin?stats
#/etc/init.d/haproxy restart
重启服务,而后经过http://ip地址:1080/admin?stats访问或者在配置文件/etc/haproxy/haproxy.cfg全局设置中加入以下几行
stats uri /haproxy-stats
stats realm haproxy\ statistics
stats auth admin:pasSWord
#/etc/init.d/haproxy restart
重启服务,而后经过http://ip地址/haproxy-stats访问,输入用户名和密码进入查看。
源码包中包含一个stats_haproxy.sh脚本,经过调用socat访问统计接口采集数据,功能等同于统计页面。命令用法以下。
Usage : stats_haproxy.sh [options] -s section
--section -s section : section to use ( --list format)
Options :
--socket -S [socket] : socket to use (default: /var/run/haproxy.stat)
--list -l : print available sections
--help -h : print this message
命令示例以下。
#./stats_haproxy.sh –s public
命令输出以下:
50 sessions/s (avg: 50 ) 93 concurrent sessions
53 sessions/s (avg: 51 ) 92 concurrent sessions
34 sessions/s (avg: 45 ) 95 concurrent sessions
33 sessions/s (avg: 42 ) 92 concurrent sessions
33 sessions/s (avg: 40 ) 101 concurrent sessions
使用socat命令,能够直接经过命令行采集数据。命令示例以下。
#echo "help" | socat /var/run/haproxy.stat stdio
命令输出以下,列出了可用的命令参数,其中参数stat能够显示统计信息。
Unknown command. Please enter one of the following commands only :
clear counters : clear max statistics counters (add 'all' for all counters)
help : this message
prompt : toggle interactive mode with prompt
quit : disconnect
show info : report information about the running process
show stat : report counters for each proxy and server
show errors : report last request and response errors for each proxy
show sess [id] : report the list of current sessions or dump this session
get weight : report a server's current weight
set weight : change a server's weight
set timeout : change a timeout setting
disable server : set a server in maintenance mode
enable server : re-enable a server that was previously in maintenance mode
#echo "show stat" | socat /var/run/haproxy.stat stdio
命令输出为csv格式,以下。示例脚本就是格式化
#pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,dreq,dresp,ereq,econ,eresp,wretr,wredis,status,weight,act,bck,chkfail,chkdown,lastchg,downtime,qlimit,pid,iid,sid,throttle,lbtot,tracked,type,rate,rate_lim,rate_max,check_status,check_code,check_duration,hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,req_rate,req_rate_max,req_tot,cli_abrt,srv_abrt,
mm,FRONTEND,,,0,4,30000,124,51741,1426307,0,0,10,,,,,OPEN,,,,,,,,,1,1,0,,,,0,0,0,10,,,,0,111,0,13,0,0,,0,10,124,,,
mm,test,0,0,0,4,,114,51441,1424624,,0,,0,0,0,0,no check,1,1,0,,,,,,1,1,1,,114,,2,0,,10,,,,0,111,0,3,0,0,0,,,,13,0,
mm,BACKEND,0,0,0,4,30000,114,51741,1426307,0,0,,0,0,0,0,UP,1,1,0,,0,13450,0,,1,1,0,,114,,1,0,,10,,,,0,111,0,3,0,0,,,,,13,0,
一、源站:http://feurix.org/projects/hatop/
目前最新版本是hatop-0.7.7.tar.gz
二、安装
进入解压后的源码包目录,运行下面命令安装。
#install -m 755 bin/hatop /usr/local/bin
#install -m 644 man/hatop.1 /usr/local/share/man/man1
#gzip /usr/local/share/man/man1/hatop.1
三、hatop使用
Hatop界面相似top,分为5个子界面(切换分别按 1 2 3 4 5或tab键选择)。
注:按5进入CLI模式,返回其它界面,只能按tab键切换。
hatop界面示例以下,输出信息与web统计界面相似,相关参数含义查看官方文档
http://feurix.org/projects/hatop/readme/
#hatop -s /var/run/haproxy.stat
访问统计页面http://192.168.52.142/admin?stats时,页面无显示,haproxy程序异常退出,系统log显示以下信息。
kernel: haproxy[1183]: segfault at 50 ip 000000000042823b sp 00007fff5a816940 error 4 in haproxy[400000+5d000]
查看配置文件,配置了直接IP访问定位到后端test组,以下所示。 reqisetbe ^Host:\ 192.168.52.142 test 注释掉此行,访问统计页面正常。