Haproxy安装配置手册

1概述

1.1Haproxy简介

http://haproxy.1wt.eu/

2 安装

2.1 OS版本选择

由于linux内核2.6.25-28兼容haproxy1.4有bug,建议在2.6.28以上版本OS安装,例如centos6.5(2.6.32)。

2.2源码安装

#tar zxvf haproxy-1.4.24.tar.gz
#cd haproxy-1.4.24
#make TARGET=linux2628 PREFIX=/path
#make install PREFIX=/path

注:linux2628表示 内核版本>=2.6.28

2.3 rpm包安装

也可以使用rpmbuild源码包为rpm包,rpmbuild需要使用的haproxy.spec文件在源码包example目录下。

2.4其它说明

安装包中的haproxy.conf为1.2版本,其中一些参数已经在高版本停用。配置参考下面的配置文档,不要在默认的haproxy.conf基础上修改。

2.5监控统计工具包安装

源码包中包含一个stats_haproxy.sh示例脚本,使用脚本需要先安装socat软件包。

3 配置

3.1基本配置

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

3.2其它代理配置项

前端和后端配置项可以在listen配置项中一起设置,配置示例如下。
listen tcpserver
bind :8888
mode tcp
server tcpserver1 192.168.52.48:80 check port 80 inter 2000 rise 2 fall 2

4基本维护

4.1服务管理命令

使用rpm包安装,服务器管理脚本安装在/etc/rc.d/init.d/haproxy,用法如下。
haproxy {start|stop|restart|reload|condrestart|status|check}

4.2配置文件检查

1、使用服务管理脚本检查
#/etc/rc.d/init.d/haproxy check

注:只能检查当前使用的配置文件。

2、使用命令检查
#haproxy –c –f

5监控统计

5.1监控统计页面

在配置文件里面配置了统计页面,可以通过浏览器访问。
修改/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访问,输入用户名和密码进入查看。

5.2监控统计脚本

源码包中包含一个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

5.3监控统计命令

使用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,

5.4 第三方工具hatop

1、源站:http://feurix.org/projects/hatop/
目前最新版本是hatop-0.7.7.tar.gz
2、安装
进入解压后的源码包目录,运行下面命令安装。
#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

3、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

6异常问题处理

6.1访问统计页面导致haproxy异常退出

访问统计页面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
注释掉此行,访问统计页面正常。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值