Haproxy安裝及配置詳解與算法
HAProxy 安裝及配置詳解與算法 HAProxy 提供高可用性、負(fù)載均衡以及基于TCP 和HTTP 應(yīng)用的代理,支持虛擬主機(jī),它是免費(fèi)、快速并且可靠的一種解決方案。根據(jù)官方數(shù)據(jù),其最高極限支持1
HAProxy 安裝及配置詳解與算法 HAProxy 提供
高可用性、負(fù)載均衡以及基于TCP 和HTTP 應(yīng)用的代理,支持虛擬主機(jī),它是免費(fèi)、快速并且可靠的一種解決方案。根據(jù)官方數(shù)據(jù),其最高極限支持10G 的并發(fā)。
HAProxy 特別適用于那些負(fù)載特大的web 站點(diǎn),這些站點(diǎn)通常又需要會(huì)話保持或七層處理。HAProxy 運(yùn)行在當(dāng)前的硬件上,完全可以支持?jǐn)?shù)以萬計(jì)的并發(fā)連接。并且它的運(yùn)行模式使得它可以很簡(jiǎn)單安全的整合進(jìn)您當(dāng)前的架構(gòu)中, 同時(shí)可以保護(hù)你的web 服務(wù)器不被暴露到網(wǎng)絡(luò)上。
其支持從4層至7層的網(wǎng)絡(luò)交換,即覆蓋所有的TCP 協(xié)議。就是說,Haproxy 甚至還支持 Mysql 的均衡負(fù)載。如果說在功能上,能以proxy 反向代理方式實(shí)現(xiàn) WEB均衡負(fù)載,這樣的產(chǎn)品有很多。包括Nginx ,ApacheProxy ,lighttpd ,Cheroke 等。
但要明確一點(diǎn)的,Haproxy 并不是 Http 服務(wù)器。以上提到所有帶反向代理均衡負(fù)載的產(chǎn)品,都清一色是 WEB 服務(wù)器。簡(jiǎn)單說,就是他們能自個(gè)兒提供靜態(tài)(html,jpg,gif..)或動(dòng)態(tài)(php,cgi..)文件的傳輸以及處理。而Haproxy 僅僅,而且專門是一款的用于均衡負(fù)載的應(yīng)用代理。其自身并不能提供http 服務(wù)。
,但其配置簡(jiǎn)單,擁有非常不錯(cuò)的服務(wù)器健康檢查功能還有專門的系統(tǒng)狀態(tài)監(jiān)控頁(yè)面,當(dāng)其代理的后端服務(wù)器出現(xiàn)故障, HAProxy會(huì)自動(dòng)將該服務(wù)器摘除,故障恢復(fù)后再自動(dòng)將該服務(wù)器加入。自1.3版本開始還引入了frontend,backend,frontend 根據(jù)任意HTTP 請(qǐng)求頭內(nèi)容做規(guī)則匹配,然后把請(qǐng)求定向到相關(guān)的backend 。
1. 安裝
官方版本獲取地址:http://haproxy.1wt.eu/,不過官方頁(yè)面已經(jīng)打不開了,請(qǐng)自行搜索! 上面中的26是linux 系統(tǒng)內(nèi)核,通過命令# uname -a可查看,我使用的是CentOS
# tar xzvf haproxy-1.4.24.tar.gz
# cd haproxy-1.4.24
# make TARGET=linux26 PREFIX=/usr/local/haproxy
# make install PREFIX=/usr/local/haproxyb
2. 配置
安裝完畢后,進(jìn)入安裝目錄配置文件,默認(rèn)情況下目錄里是沒有.cfg 配置文件的,可以回到安裝文件目錄下將examples 下的haproxy.cfg 拷貝到usr/local/haproxy下。
# cd /usr/local/haproxy
# vi haproxy.cfg
默認(rèn)文件內(nèi)容如下:(注意??!標(biāo)示開始的為默認(rèn)的配置文件沒有的)
,CODE:
# this config needs haproxy-1.1.28 or haproxy-1.2.1
####################全局配置信息####################################
##########參數(shù)為進(jìn)程級(jí),通常和操作系統(tǒng)相關(guān)##########################
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info #日志
maxconn 4096 #默認(rèn)最大連接數(shù)
chroot /usr/share/haproxy #chroot運(yùn)行路徑
uid 99 #所屬運(yùn)行的用戶uid
gid 99 #所屬運(yùn)行的用戶組
daemon #以后臺(tái)形式運(yùn)行haproxy
?。bproc 2 #進(jìn)程數(shù)量(可以設(shè)置多個(gè)進(jìn)程提高性能) !!pidfile /usr/local/haproxy/haproxy.pid #haproxy的pid 存放路徑, 啟動(dòng)進(jìn)程的用戶必須有權(quán)限訪問此文件
#debug #
#quiet
####################默認(rèn)的全局設(shè)置####################################
##########參數(shù)可以被利用配置到frontend,backend,listen 組件#############
defaults
log global
mode http #所處理的類別 (#7層 http;4層tcp )
option httplog #日志類別http 日志格式
!!option httpclose #每次請(qǐng)求完畢后主動(dòng)關(guān)閉http 通道
option dontlognull #不記錄健康檢查的日志信息
??! option forwardfor #后端服務(wù)器需要獲得客戶端真實(shí)ip 需配置的參數(shù),可從Http Header中獲得ip ??!option redispatch #serverId對(duì)應(yīng)的服務(wù)器掛掉后, 強(qiáng)制定向到其他健康的服務(wù)器
?。ption abortonclose #當(dāng)服務(wù)器負(fù)載很高的時(shí)候,自動(dòng)結(jié)束掉當(dāng)前隊(duì)列處理比較久的連接 !!stats refresh 30 #統(tǒng)計(jì)頁(yè)面刷新間隔
retries 3 #3次連接失敗就認(rèn)為服務(wù)不可用,也可以通過后面設(shè)置
redispatch #當(dāng)serverid 對(duì)應(yīng)的服務(wù)器掛掉后,強(qiáng)制定向到其他健康服務(wù)器 ??!balance roundrobin #默認(rèn)的負(fù)載均衡的方式, 輪詢方式
?。alance source #默認(rèn)的負(fù)載均衡的方式, 類似nginx 的ip_hash
?。alance leastconn #默認(rèn)的負(fù)載均衡的方式, 最小連接
maxconn 2000 #最大連接數(shù)
contimeout 5000 #連接超時(shí)
clitimeout 50000 #客戶端超時(shí)
srvtimeout 50000 #服務(wù)器超時(shí)
####################監(jiān)控頁(yè)面設(shè)置####################################
listen appli1-rewrite 0.0.0.0:10001
cookie SERVERID rewrite !! timeout check 2000 #心跳檢測(cè)超時(shí)
,balance roundrobin
server app1_1 192.168.34.23:8080 cookie app1inst1 check inter 2000 rise 2 fall 5 server app1_2 192.168.34.32:8080 cookie app1inst2 check inter 2000 rise 2 fall 5 server app1_3 192.168.34.27:8080 cookie app1inst3 check inter 2000 rise 2 fall 5 server app1_4 192.168.34.42:8080 cookie app1inst4 check inter 2000 rise 2 fall 5
listen appli2-insert 0.0.0.0:10002
option httpchk
balance roundrobin
cookie SERVERID insert indirect nocache
server inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3
server inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3
capture cookie vgnvisitor= len 32
option httpclose # disable keep-alive
rspidel ^Set-cookie: IP= # do not let this cookie tell our internal IP address
listen appli3-relais 0.0.0.0:10003
dispatch 192.168.135.17:80
listen appli4-backup 0.0.0.0:10004
option httpchk /index.html
option persist
balance roundrobin
server inst1 192.168.114.56:80 check inter 2000 fall 3
server inst2 192.168.114.56:81 check inter 2000 fall 3 backup
listen ssl-relay 0.0.0.0:8443
option ssl-hello-chk
balance source
server inst1 192.168.110.56:443 check inter 2000 fall 3
server inst2 192.168.110.57:443 check inter 2000 fall 3
server back1 192.168.120.58:443 backup
listen appli5-backup 0.0.0.0:10005
option httpchk *
balance roundrobin
cookie SERVERID insert indirect nocache
server inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3
server inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3
server inst3 192.168.114.57:80 backup check inter 2000 fall 3
capture cookie ASPSESSION len 32
srvtimeout 20000
option httpclose # disable keep-alive
option checkcache # block response if set-cookie & cacheable
,rspidel ^Set-cookie: IP= # do not let this cookie tell our internal IP address
errorloc 502 http://192.168.114.58/error502.html
errorfile 503 /etc/haproxy/errors/503.http
####################監(jiān)控頁(yè)面的設(shè)置#######################
listen admin_status #Frontend和Backend 的組合體, 監(jiān)控組的名稱,按需自定義名稱 bind 0.0.0.0:65532 #監(jiān)聽端口
mode http #http的7層模式
log 127.0.0.1 local3 err #錯(cuò)誤日志記錄
stats refresh 5s #每隔5秒自動(dòng)刷新監(jiān)控頁(yè)面
stats uri /admin?stats #監(jiān)控頁(yè)面的url
stats realm itnihao itnihao #監(jiān)控頁(yè)面的提示信息
stats auth admin:admin #監(jiān)控頁(yè)面的用戶和密碼admin, 可以設(shè)置多個(gè)用戶名
stats auth admin1:admin1 #監(jiān)控頁(yè)面的用戶和密碼admin1
stats hide-version #隱藏統(tǒng)計(jì)頁(yè)面上的HAproxy 版本信息
stats admin if TRUE #手工啟用/禁用, 后端服務(wù)器(haproxy-1.4.9以后版本)
errorfile 403 /etc/haproxy/errorfiles/403.http
errorfile 500 /etc/haproxy/errorfiles/500.http
errorfile 502 /etc/haproxy/errorfiles/502.http
errorfile 503 /etc/haproxy/errorfiles/503.http
errorfile 504 /etc/haproxy/errorfiles/504.http
#################HAProxy的日志記錄內(nèi)容設(shè)置###################
capture request header Host len 40
capture request header Content-Length len 10
capture request header Referer len 200
capture response header Server len 40
capture response header Content-Length len 10
capture response header Cache-Control len 8
#######################網(wǎng)站監(jiān)測(cè)listen 配置#####################
###########此用法主要是監(jiān)控haproxy 后端服務(wù)器的監(jiān)控狀態(tài)############
listen site_status
bind 0.0.0.0:1081 #監(jiān)聽端口
mode http #http的7層模式
log 127.0.0.1 local3 err #[err warning info debug]
monitor-uri /site_status #網(wǎng)站健康檢測(cè)URL ,用來檢測(cè)HAProxy 管理的網(wǎng)站是否可以用,正常返回200,不正常返回503
acl site_dead nbsrv(server_web) lt 2 #定義網(wǎng)站down 時(shí)的策略當(dāng)掛在負(fù)載均衡上的指定backend 的中有效機(jī)器數(shù)小于1臺(tái)時(shí)返回true
acl site_dead nbsrv(server_blog) lt 2
acl site_dead nbsrv(server_bbs) lt 2
,monitor fail if site_dead #當(dāng)滿足策略的時(shí)候返回503,網(wǎng)上文檔說的是500,實(shí)際測(cè)試為 monitor-net 192.168.16.2/32 #來自192.168.16.2的日志信息不會(huì)被記錄和轉(zhuǎn)發(fā)
monitor-net 192.168.16.3/32
########frontend配置可以定義多個(gè)acl 進(jìn)行匹配操作########
frontend http_80_in
bind 0.0.0.0:80 #監(jiān)聽端口,即haproxy 提供web 服務(wù)的端口,和lvs 的vip 端口類似
mode http #http的7層模式
log global #應(yīng)用全局的日志配置
option httplog #啟用http 的log
option httpclose #每次請(qǐng)求完畢后主動(dòng)關(guān)閉http 通道,HA-Proxy 不支持keep-alive 模式
option forwardfor #如果后端服務(wù)器需要獲得客戶端的真實(shí)IP 需要配置次參數(shù),將可以從Http Header中獲得客戶端IP
########acl策略配置#############
acl itnihao_web hdr_reg(host) -i ^(www.itnihao.cn|ww1.itnihao.cn)$
#如果請(qǐng)求的域名滿足正則表達(dá)式中的2個(gè)域名返回true -i是忽略大小寫
acl itnihao_blog hdr_dom(host) -i blog.itnihao.cn
#如果請(qǐng)求的域名滿足www.itnihao.cn 返回true -i是忽略大小寫
#acl itnihao hdr(host) -i itnihao.cn
#如果請(qǐng)求的域名滿足itnihao.cn 返回true -i是忽略大小寫
#acl file_req url_sub -i killall=
#在請(qǐng)求url 中包含killall=,則此控制策略返回true, 否則為false
#acl dir_req url_dir -i allow
#在請(qǐng)求url 中存在allow 作為部分地址路徑,則此控制策略返回true, 否則返回false
#acl missing_cl hdr_cnt(Content-length) eq 0
#當(dāng)請(qǐng)求的header 中Content-length 等于0時(shí)返回true
########acl策略匹配相應(yīng)#############
#block if missing_cl
#當(dāng)請(qǐng)求中header 中Content-length 等于0阻止請(qǐng)求返回403
#block if !file_req || dir_req
#block表示阻止請(qǐng)求,返回403錯(cuò)誤,當(dāng)前表示如果不滿足策略file_req,或者滿足策略dir_req,則阻止請(qǐng)求 use_backend server_web if itnihao_web
#當(dāng)滿足itnihao_web的策略時(shí)使用server_web的backend
use_backend server_blog if itnihao_blog
#當(dāng)滿足itnihao_blog的策略時(shí)使用server_blog的backend
#redirect prefix http://blog.itniaho.cn code 301 if itnihao
#當(dāng)訪問itnihao.cn 的時(shí)候,用http 的301挑轉(zhuǎn)到http://192.168.16.3
default_backend server_bbs
#以上都不滿足的時(shí)候使用默認(rèn)server_bbs的backend
##########backend的設(shè)置##############
######################backend server_web#############################
backend server_web
,mode http #http的7層模式
balance roundrobin #負(fù)載均衡的方式,roundrobin 平均方式
cookie SERVERID #允許插入serverid 到cookie 中,serverid 后面可以定義
option httpchk GET /index.html #心跳檢測(cè)的文件
server web1 192.168.16.2:80 cookie web1 check inter 1500 rise 3 fall 3 weight 1
#服務(wù)器定義,cookie 1表示serverid 為web1,check inter 1500是檢測(cè)心跳頻率rise 3是3次正確認(rèn)為服務(wù)器可用,
#fall 3是3次失敗認(rèn)為服務(wù)器不可用,weight 代表權(quán)重
server web2 192.168.16.3:80 cookie web2 check inter 1500 rise 3 fall 3 weight 2
#服務(wù)器定義,cookie 1表示serverid 為web2,check inter 1500是檢測(cè)心跳頻率rise 3是3次正確認(rèn)為服務(wù)器可用,
#fall 3是3次失敗認(rèn)為服務(wù)器不可用,weight 代表權(quán)重
#########################backend server_blog######################################## backend server_blog
mode http #http的7層模式
balance roundrobin #負(fù)載均衡的方式,roundrobin 平均方式
cookie SERVERID #允許插入serverid 到cookie 中,serverid 后面可以定義
option httpchk GET /index.html #心跳檢測(cè)的文件
server blog1 192.168.16.2:80 cookie blog1 check inter 1500 rise 3 fall 3 weight 1 #服務(wù)器定義,cookie 1表示serverid 為web1,check inter 1500是檢測(cè)心跳頻率rise 3是3次正確認(rèn)為服務(wù)器可用,fall 3是3次失敗認(rèn)為服務(wù)器不可用,weight 代表權(quán)重
server blog2 192.168.16.3:80 cookie blog2 check inter 1500 rise 3 fall 3 weight 2
#服務(wù)器定義,cookie 1表示serverid 為web2,check inter 1500是檢測(cè)心跳頻率rise 3是3次正確認(rèn)為服務(wù)器可用,fall 3是3次失敗認(rèn)為服務(wù)器不可用,weight 代表權(quán)重
###################################backend erver_bbs####################################
backend server_bbs
mode http #http的7層模式
balance roundrobin #負(fù)載均衡的方式,roundrobin 平均方式
cookie SERVERID #允許插入serverid 到cookie 中,serverid 后面可以定義
option httpchk GET /index.html #心跳檢測(cè)的文件
server bbs1 192.168.16.2:80 cookie bbs1 check inter 1500 rise 3 fall 3 weight 1
#服務(wù)器定義,cookie 1表示serverid 為web1,check inter 1500是檢測(cè)心跳頻率rise 3是3次正確認(rèn)為服務(wù)器可用,fall 3是3次失敗認(rèn)為服務(wù)器不可用,weight 代表權(quán)重
server bbs2 192.168.16.3:80 cookie bbs2 check inter 1500 rise 3 fall 3 weight 2
#服務(wù)器定義,cookie 1表示serverid 為web2,check inter 1500是檢測(cè)心跳頻率rise 3是3次正確認(rèn)為服務(wù)器可用,fall 3是3次失敗認(rèn)為服務(wù)器不可用,weight 代表權(quán)重
根據(jù)實(shí)際需求,更改配置文件,我的配置如下
3. 加上日志支持
,是根據(jù)上面的global 和default 里的log 配置 # vim /etc/syslog.conf
在最下邊增加
local3.* /var/log/haproxy.log
local0.* /var/log/haproxy.log
#vim /etc/sysconfig/syslog
修改: SYSLOGD_OPTIONS="-r -m 0"
啟日志服務(wù)service syslog restart
然后就可查看日志了
# tail –f /var/log/harpoxy.log
4. 設(shè)置開機(jī)啟動(dòng)
為了方便系統(tǒng)在開機(jī)時(shí)加載,還可以創(chuàng)建啟動(dòng)腳本:
# vim /etc/rc.d/init.d/haproxy
{
echo -n "Stopping $DESC: $PROGNAME"
haproxy_pid=cat $PIDFILE
kill $haproxy_pid
echo "."
}
restart()
{
echo -n "Restarting $DESC: $PROGNAME"
$DAEMON -f $CONFIG -p $PIDFILE -sf $(cat $PIDFILE)
echo "."
}
case "$1" in
start)
start
;; stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
exit 1
;;
esac
exit 0
保存后賜予可執(zhí)行權(quán)限
# chmod x /etc/rc.d/init.d/haproxy
就可以使用 service haproxy start|stop|restart 來控制服務(wù)的啟動(dòng)停止跟重啟。
并通過以下命令加載到開機(jī)服務(wù)啟動(dòng)列表
# chkconfig –add haproxy
5. 啟動(dòng)參數(shù)
haproxy -f <配置文件>
[-n 最大并發(fā)連接總數(shù)] [-N 每個(gè)偵聽的最大并發(fā)數(shù)] [-d] [-D] [-q] [-V] [-c] [-p
[-ds] [-de] [-dp] [-db] [-m <內(nèi)存限制M>] [{-sf|-st} pidlist...] -d 前臺(tái),debug 模式
,-D daemon模式啟動(dòng)
-q 安靜模式, 不輸出信息
-V 詳細(xì)模式
-c 對(duì)配置文件進(jìn)行語法檢查
-s 顯示統(tǒng)計(jì)數(shù)據(jù)
-l 顯示詳細(xì)統(tǒng)計(jì)數(shù)據(jù)
-dk 不使用kqueue
-ds 不使用speculative epoll
-de 不使用epoll
-dp 不使用poll
-db 禁用后臺(tái)模式,程序跑在前臺(tái)
-sf
6. 啟動(dòng)服務(wù)
啟動(dòng)服務(wù):
#/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
重啟服務(wù):
#/usr/local/haproxy/sbin/haproxy
/usr/local/haproxy/logs/haproxy.pid` -f /usr/local/haproxy/haproxy.cfg -st `cat
停止服務(wù):
# killall haproxy
6. 查看狀態(tài)
在瀏覽器中輸入haproxy 監(jiān)控地址:http://ipaddress:8080/admin_status