了解HAProxy原理及参数
haproxy:
4層:并不涉及到用戶空間,也就不需要完成模式轉換,不需要套接字注冊等.
????lvs
7層:
????nginx?
????haproxy
????ats
實例:
haproxy 雙網卡: eth0:172.16.0.192 eth1:192.168.30.2
web1:192.168.30.131
web2:192.168.30.129
web1和web2的網關為haproxy的eth1的ip地址:
web1/web1 :route add default gw 192.168.30.1
配置由兩部分組成:
global settings:對haproxy進程自身屬性設定.
proxies:對代理的設定
default:默認配置
frontend:前端配置
backenbd:后端的設定
listen:監聽
URL
http://host:port/path?queries#fragment
配置實例:
frontend?server?*:80//定義前端frontend?name為server?監聽80端口default_backend?servers//定義默認的backend?server為servers backend?servers??????//定義后端的backend?name為serversbalance?roundrobin//負載均衡算法server??node1???192.168.30.129:80???????check//后端的節點:80一定要添加(嘗試不添加會報錯,1.4版本的貌似繼承上面frontend的端口),開啟健康檢查server??node2???192.168.30.131:80???????check瀏覽器訪問:http://192.168.30.130/ ? 刷新可以看到負載均衡輪訓
開啟haproxy log:
haproxy.cfg提示開啟haproxy.log 需要在/etc/sysconfig/syslog增加
local2.* ? ? ? ? ? ? ? ? ? ? ? /var/log/haproxy.log
操作:
vim /etc/rsyslog.conf
1、開啟udp協議514端口
#?Provides?UDP?syslog?reception $ModLoad?imudp $UDPServerRun?514 2、新增: local2.*????????????????????????????????????????????????/var/log/haproxy.log3、重啟syslog和haproxy服務:
service?rsyslog?restart/etc/init.d/haproxy?restart4、測試查看日志:
tail?-f?/var/log/haproxy.logbind參數:bind [<adresses>]:<port_range> [,...]
例子:
bind???:80,:443 bind?10.10.0.1:10080,10.10.0.1:10043 可以使用在listen和frontend模塊中: frontend?server? bind?*:80 default_backendservers backend?servers balanceroundrobin servernode1192.168.30.129:80check servernode2192.168.30.131:80checkbalance參數:
roundrobin 加權輪訓,wgight不指默認為1,后續backend新增的server node會自動識別,慢加載方式加載,
static-rr ?靜態輪訓,新增的node節點除非重啟haproxy否則不會自動識別;基于check健康檢查下線的機器再次上線會自動識別;不會慢啟動,新增的服務器立即加入服務器輪訓列表,所有的請求都會立即到這臺服務器.
leastconn ?保持會話連接,常用的LDAP,SQL,TSE,ect...動態的。http協議時無狀態的,獲取到資源即斷開所以不適用次算法.
source ? ? 保持會話session,基于ip地址做hash算法,同一個用戶的請求將發往同一臺服務器.
會話保持機制:
IP層:source
位于同一個NAT服務器背后的多個請求都會頂向至同一個upstream server;不利于均衡。
應用層:cookie
有更好的負載均衡效果;
source:一般只有不支持使用cookie插入又需要保持會話時使用.
url:用于后端服務器時cache server的場景,保證緩存命中率的.
cookie:
cookie <name> [ rewrite | insert | prefix ] [ indirect ] [ nocache ]
? ? ? ? ? ? ?[ postonly ] [ preserve ] [ httponly ] [ secure ]
? ? ? ? ? ? ?[ domain <domain> ]* [ maxidle <idle> ] [ maxlife <life> ]
frontend server?
bind *:80
default_backendservers
backend servers
cookie node insert nocache
balancesource
servernode1192.168.30.129:80check
servernode2192.168.30.131:80check
backend下使用cookie cookie名稱為node,客戶端每次訪問都插入cookie,一般insert會與nocache同時使用,防止后端緩存服務器緩存cookie降低緩存命中率.
cookie綁定:
基于cookie綁定,訪問后端node節點
frontend server?
bind *:80
default_backendservers
backend servers
cookie node insert nocache
balance roundrobin//做輪訓
servernode1192.168.30.129:80checkcookie node1#定義node1的cookie為node1
servernode2192.168.30.131:80checkcookie node2#定義node2的cookie為node2
訪問瀏覽器:http://192.168.30.130/index.html
發現并未做輪訓,因為綁定了cookie信息,同一客戶端的訪問,請求將會被發往同一個后臺node節點.
指定HAProxy的工作模式:
mode { tcp|http|health }
tcp:mysql、ldap
tcp為HAProxy的默認模式,haproxy在客戶端和選定的upstream server之間建立一個全雙工的連接;不會對應用層協議做任何檢查;
SSL/Mysql/SSH等都應該使用此模式.
http:web
http協議
對應用層數據做深入分析,因此支持7層的過濾、處理、轉換等機制;
health:健康檢查
指定日志:
log global
log <address> [len <length>] <facility> [<level> [<minlevel>]]
no log
defaults、frontend、backend、listen四項都可以使用.
log global:使用全局配置中定義的日志服務器;
log <address> [len <length>] <facility> [<level> [<minlevel>]]
frontend server?
log 127.0.0.1local3#日志服務器,在rsyslog.conf中定義,指定的frontend日志.
bind *:80
default_backendservers
backend servers
cookie node insert nocache
balance roundrobin
servernode1192.168.30.129:80checkcookie node1
servernode2192.168.30.131:80checkcookie node2
rsyslog.conf的配置:
local3.* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?/var/log/haproxyweb.log
捕獲請求首部和相應首部:
capture request header <name> len <length>
capture response header <name> len <length>
定義默認后端服務器:在listen或frontend中指定使用的默認后端:
default_backend <backend>
use_backend <backend> [{if | unless} <condition>]
在listen或frontend中,定義指明使用哪個后端服務器.
server <name> <address>[:[port]] [param*]
為backend或listen定義各服務器.
默認server的參數:
backup:設定備用服務器,僅在負載均衡場景中的其他server均不可用于啟用此server;
check:啟動對次server執行健康狀態檢查,其可以借助與額外的其他參數完成更精細的設定.
inter:設定健康狀態檢查時間間隔,單位為毫秒,默認為2000,也可以使用fastinter和downinter來根據服務器端狀態優化此時間延遲.
rise <count>:設定健康狀態檢查中,某離線的server從離線轉換至正常狀態需要成功檢查的次數.
fall <count>:確認server從正常狀態轉換為不可用狀態需要檢查的次數.
maxconn <maxcoon>:指定此服務器接受的最大并發連接數;如果發往此服務器的連接數高于此值,其將被放置于請求隊列,以等待其他連接被釋放.
maxqueue <maxqueue>:設定請求隊列的最大長度.
observer <mode>:通過觀察服務器的通信狀況來判斷其健康狀態,默認為禁用,其支持的類型有4層和layer7,"layer7"僅能在http代理場景下使用。
redir <prefix>:啟用重定向功能,將發往此服務器的GET和HEAD請求均以302狀態碼相應;需要注意的是,在prefix后面不能使用/,且
不能使用相對地址,以免造成循環;例如:server srv1 172.16.0.110 redir http://www.example.com
weight<weight>:權重,默認為1,最大為256,0表示不參與權重.
check對后端的服務器做健康狀態檢測;
擴展參數:option httpchk
option httpchk
option httpchk <uri>
option httpchk <method> <uri>
option httpchk <method> <uri> <version>
stats啟用默認設置統計表:
stats enable
四項都可以使用:defaults、frontend、listen、backend
如果啟用此配置,不自己
This statement enables statistics reporting with default settings defined
at build time. Unless stated otherwise, these settings are used :
?- stats uri ? : /haproxy?stats ? ?默認的url,可以自己指定
?- stats realm : "HAProxy Statistics" 描述和提示
?- stats auth ?: no authentication默認的認證,銘文密碼隔開
?- stats scope : no restriction指定訪問位置的
瀏覽器訪問haproxy代理:http://192.168.30.130/haproxy?stats
haproxy的status的訪問控制:
stats http-request { allow | deny | auth [realm <realm>] }
? ? ? ? ? ? [ { if | unless } <condition> ]
stats refresh <delay> 指定stats多久自動刷新
stats hide-version ?隱藏haproxy版本.
stats auth <user>:<passwd> 認證
stats admin { if | unless } <cond> 啟動管理接口
stats admin if LOCALHOST 表示本機
stats admin if TRUE 表示認證通過
實例:
backend servers
? ? ? ?cookie node insert nocache
? ? ? ?balance roundrobin
? ? ? ?stats enable
? ? ? ?stats refresh 20s
? ? ? ?stats hide-version
? ? ? ?stats admin if TRUE
? ? ? ?stats auth admin:admin
? ? ? ?stats uri /hastatus
? ? ? ?server ?node1 ? 192.168.30.129:80 ? ? ? check ? cookie node1
? ? ? ?server ?node2 ? 192.168.30.131:80 ? ? ? check ? cookie node2 ?
自定義錯誤頁面:
errorfile?
errorloc302
errorloc303
errorfile <code> <file>
http-request { allow | deny | tarpit | auth [realm <realm>] | redirect <rule> |
? ? ? ? ? ? ?add-header <name> <fmt> | set-header <name> <fmt> |
? ? ? ? ? ? ?del-header <name> | set-nice <nice> | set-log-level <level> |
? ? ? ? ? ? ?replace-header <name> <match-regex> <replace-fmt> |
? ? ? ? ? ? ?replace-value <name> <match-regex> <replace-fmt> |
? ? ? ? ? ? ?set-tos <tos> | set-mark <mark> |
? ? ? ? ? ? ?add-acl(<file name>) <key fmt> |
? ? ? ? ? ? ?del-acl(<file name>) <key fmt> |
? ? ? ? ? ? ?del-map(<file name>) <key fmt> |
? ? ? ? ? ? ?set-map(<file name>) <key fmt> <value fmt>
? ? ? ? ? ? }
? ? ? ? ? ? [ { if | unless } <condition> ]
haproxy封裝客戶端ip,新增x-forward-for,避免總是記錄haproxy,轉而記錄client ip。
option forwardfor [ except <network> ] [ header <name> ] [ if-none ]
keepalive 關閉有兩種原因:
1、當前服務器的連接數達到上限。
2、連接超時.
timeout queue <timeout> 默認為1m
請求在隊列中等待的最大時間長,一直得不到服務的響應,客戶端重新發送請求.
timeout connect <timeout> 默認為10s
通常指haproxy將請求轉發至后臺upstream server時,所等待的超時時常.
timeout client <timeout>
客戶的最大非活動連接的最大時長,指定時長后將斷開連接.
timeout server <timeout>
連接已經建立,但是服務端沒有任何數據傳輸的超時時長。
timeout http-keep-alive <timeout>
定義保持連接模式的超時時長
timeout check <timeout>
定義健康狀態檢測的超時時長
option http-server-clone?
定義了keepalive功能,客戶端和服務器端之間的會話連接超時,允許server主動關閉.
客戶端可服務器端建立連接時,就開始記錄日志。(通常都是服務器端相應完成才記錄日志,方便記錄服務器的相應時間),如果打開此項,會提前記錄日志.
option logasap
no option logasap
Enable or disable early logging of HTTP requests
客戶端和服務器端連接建立,但是沒有任何的數據傳輸,即空連接,此項為不記錄空連接日志.
option dontlognull
no option dontlognull
Enable or disable logging of null connections
HAProxy Acl定義:
支持的值大概有四種:
整數或整數范圍/字符串/正則表達式/ip地址或者網絡地址
4層訪問控制:
tcp-request content <action> [{if | unless} <condition>]
tcp-request inspect-delay <timeout>
7層訪問控制:
http-request { allow | deny | tarpit | auth [realm <realm>] | redirect <rule> |
? ? ? ? ? ? ? add-header <name> <fmt> | set-header <name> <fmt> |
? ? ? ? ? ? ? del-header <name> | set-nice <nice> | set-log-level <level> |
? ? ? ? ? ? ? replace-header <name> <match-regex> <replace-fmt> |
? ? ? ? ? ? ? replace-value <name> <match-regex> <replace-fmt> |
? ? ? ? ? ? ? set-tos <tos> | set-mark <mark> |
? ? ? ? ? ? ? add-acl(<file name>) <key fmt> |
? ? ? ? ? ? ? del-acl(<file name>) <key fmt> |
? ? ? ? ? ? ? del-map(<file name>) <key fmt> |
? ? ? ? ? ? ? set-map(<file name>) <key fmt> <value fmt>
? ? ? ? ? ? ?}
? ? ? ? ? ? ?[ { if | unless } <condition> ]
總結
以上是生活随笔為你收集整理的了解HAProxy原理及参数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第3章 NFS基本应用
- 下一篇: firefox不激活新标签页