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)。linux

2.2源碼安裝

#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

2.3 rpm包安裝

也可使用rpmbuild源碼包爲rpm包,rpmbuild須要使用的haproxy.spec文件在源碼包example目錄下。後端

2.4其它說明

安裝包中的haproxy.conf爲1.2版本,其中一些參數已經在高版本停用。配置參考下面的配置文檔,不要在默認的haproxy.conf基礎上修改。centos

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配置文件檢查

一、使用服務管理腳本檢查
#/etc/rc.d/init.d/haproxy check

注:只能檢查當前使用的配置文件。

二、使用命令檢查
#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

一、源站: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

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 註釋掉此行,訪問統計頁面正常。

相關文章
相關標籤/搜索