日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

了解HAProxy原理及参数

發(fā)布時間:2025/5/22 65 豆豆
生活随笔 收集整理的這篇文章主要介紹了 了解HAProxy原理及参数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

haproxy:

4層:并不涉及到用戶空間,也就不需要完成模式轉(zhuǎn)換,不需要套接字注冊等.

????lvs

7層:

????nginx?

????haproxy

????ats

實例:

haproxy 雙網(wǎng)卡: eth0:172.16.0.192 eth1:192.168.30.2

web1:192.168.30.131

web2:192.168.30.129


web1和web2的網(wǎng)關(guān)為haproxy的eth1的ip地址:

web1/web1 :route add default gw 192.168.30.1


配置由兩部分組成:

global settings:對haproxy進(jìn)程自身屬性設(shè)定.

proxies:對代理的設(shè)定

default:默認(rèn)配置

frontend:前端配置

backenbd:后端的設(shè)定

listen:監(jiān)聽

URL

http://host:port/path?queries#fragment


配置實例:

frontend?server?*:80//定義前端frontend?name為server?監(jiān)聽80端口default_backend?servers//定義默認(rèn)的backend?server為servers backend?servers??????//定義后端的backend?name為serversbalance?roundrobin//負(fù)載均衡算法server??node1???192.168.30.129:80???????check//后端的節(jié)點:80一定要添加(嘗試不添加會報錯,1.4版本的貌似繼承上面frontend的端口),開啟健康檢查server??node2???192.168.30.131:80???????check

瀏覽器訪問:http://192.168.30.130/ ? 刷新可以看到負(fù)載均衡輪訓(xùn)


開啟haproxy log:

haproxy.cfg提示開啟haproxy.log 需要在/etc/sysconfig/syslog增加

local2.* ? ? ? ? ? ? ? ? ? ? ? /var/log/haproxy.log

操作:

vim /etc/rsyslog.conf

1、開啟udp協(xié)議514端口

#?Provides?UDP?syslog?reception $ModLoad?imudp $UDPServerRun?514 2、新增: local2.*????????????????????????????????????????????????/var/log/haproxy.log

3、重啟syslog和haproxy服務(wù):

service?rsyslog?restart/etc/init.d/haproxy?restart


4、測試查看日志:

tail?-f?/var/log/haproxy.log


bind參數(shù):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:80check


balance參數(shù):

roundrobin 加權(quán)輪訓(xùn),wgight不指默認(rèn)為1,后續(xù)backend新增的server node會自動識別,慢加載方式加載,

static-rr ?靜態(tài)輪訓(xùn),新增的node節(jié)點除非重啟haproxy否則不會自動識別;基于check健康檢查下線的機(jī)器再次上線會自動識別;不會慢啟動,新增的服務(wù)器立即加入服務(wù)器輪訓(xùn)列表,所有的請求都會立即到這臺服務(wù)器.

leastconn ?保持會話連接,常用的LDAP,SQL,TSE,ect...動態(tài)的。http協(xié)議時無狀態(tài)的,獲取到資源即斷開所以不適用次算法.

source ? ? 保持會話session,基于ip地址做hash算法,同一個用戶的請求將發(fā)往同一臺服務(wù)器.


會話保持機(jī)制:

IP層:source

位于同一個NAT服務(wù)器背后的多個請求都會頂向至同一個upstream server;不利于均衡。

應(yīng)用層:cookie

有更好的負(fù)載均衡效果;


source:一般只有不支持使用cookie插入又需要保持會話時使用.

url:用于后端服務(wù)器時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同時使用,防止后端緩存服務(wù)器緩存cookie降低緩存命中率.

cookie綁定:

基于cookie綁定,訪問后端node節(jié)點

frontend server?

bind *:80

default_backendservers


backend servers

cookie node insert nocache

balance roundrobin//做輪訓(xùn)

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


發(fā)現(xiàn)并未做輪訓(xùn),因為綁定了cookie信息,同一客戶端的訪問,請求將會被發(fā)往同一個后臺node節(jié)點.


指定HAProxy的工作模式:

mode { tcp|http|health }

tcp:mysql、ldap

tcp為HAProxy的默認(rèn)模式,haproxy在客戶端和選定的upstream server之間建立一個全雙工的連接;不會對應(yīng)用層協(xié)議做任何檢查;

SSL/Mysql/SSH等都應(yīng)該使用此模式.


http:web

http協(xié)議

對應(yīng)用層數(shù)據(jù)做深入分析,因此支持7層的過濾、處理、轉(zhuǎn)換等機(jī)制;


health:健康檢查


指定日志:

log global

log <address> [len <length>] <facility> [<level> [<minlevel>]]

no log


defaults、frontend、backend、listen四項都可以使用.

log global:使用全局配置中定義的日志服務(wù)器;

log <address> [len <length>] <facility> [<level> [<minlevel>]]


frontend server?

log 127.0.0.1local3#日志服務(wù)器,在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


捕獲請求首部和相應(yīng)首部:

capture request header <name> len <length>

capture response header <name> len <length>


定義默認(rèn)后端服務(wù)器:在listen或frontend中指定使用的默認(rèn)后端:

default_backend <backend>


use_backend <backend> [{if | unless} <condition>]

在listen或frontend中,定義指明使用哪個后端服務(wù)器.



server <name> <address>[:[port]] [param*]

為backend或listen定義各服務(wù)器.

默認(rèn)server的參數(shù):

backup:設(shè)定備用服務(wù)器,僅在負(fù)載均衡場景中的其他server均不可用于啟用此server;

check:啟動對次server執(zhí)行健康狀態(tài)檢查,其可以借助與額外的其他參數(shù)完成更精細(xì)的設(shè)定.

inter:設(shè)定健康狀態(tài)檢查時間間隔,單位為毫秒,默認(rèn)為2000,也可以使用fastinter和downinter來根據(jù)服務(wù)器端狀態(tài)優(yōu)化此時間延遲.

rise <count>:設(shè)定健康狀態(tài)檢查中,某離線的server從離線轉(zhuǎn)換至正常狀態(tài)需要成功檢查的次數(shù).

fall <count>:確認(rèn)server從正常狀態(tài)轉(zhuǎn)換為不可用狀態(tài)需要檢查的次數(shù).


maxconn <maxcoon>:指定此服務(wù)器接受的最大并發(fā)連接數(shù);如果發(fā)往此服務(wù)器的連接數(shù)高于此值,其將被放置于請求隊列,以等待其他連接被釋放.


maxqueue <maxqueue>:設(shè)定請求隊列的最大長度.


observer <mode>:通過觀察服務(wù)器的通信狀況來判斷其健康狀態(tài),默認(rèn)為禁用,其支持的類型有4層和layer7,"layer7"僅能在http代理場景下使用。


redir <prefix>:啟用重定向功能,將發(fā)往此服務(wù)器的GET和HEAD請求均以302狀態(tài)碼相應(yīng);需要注意的是,在prefix后面不能使用/,且

不能使用相對地址,以免造成循環(huán);例如:server srv1 172.16.0.110 redir http://www.example.com


weight<weight>:權(quán)重,默認(rèn)為1,最大為256,0表示不參與權(quán)重.


check對后端的服務(wù)器做健康狀態(tài)檢測;

擴(kuò)展參數(shù):option httpchk


option httpchk

option httpchk <uri>

option httpchk <method> <uri>

option httpchk <method> <uri> <version>


stats啟用默認(rèn)設(shè)置統(tǒng)計表:

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 ? ?默認(rèn)的url,可以自己指定

?- stats realm : "HAProxy Statistics" 描述和提示

?- stats auth ?: no authentication默認(rèn)的認(rèn)證,銘文密碼隔開

?- 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> 認(rèn)證


stats admin { if | unless } <cond> 啟動管理接口

stats admin if LOCALHOST 表示本機(jī)

stats admin if TRUE 表示認(rèn)證通過


實例:

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,轉(zhuǎn)而記錄client ip。

option forwardfor [ except <network> ] [ header <name> ] [ if-none ]


keepalive 關(guān)閉有兩種原因:

1、當(dāng)前服務(wù)器的連接數(shù)達(dá)到上限。

2、連接超時.


timeout queue <timeout> 默認(rèn)為1m

請求在隊列中等待的最大時間長,一直得不到服務(wù)的響應(yīng),客戶端重新發(fā)送請求.


timeout connect <timeout> 默認(rèn)為10s

通常指haproxy將請求轉(zhuǎn)發(fā)至后臺upstream server時,所等待的超時時常.


timeout client <timeout>

客戶的最大非活動連接的最大時長,指定時長后將斷開連接.


timeout server <timeout>

連接已經(jīng)建立,但是服務(wù)端沒有任何數(shù)據(jù)傳輸?shù)某瑫r時長。


timeout http-keep-alive <timeout>

定義保持連接模式的超時時長


timeout check <timeout>

定義健康狀態(tài)檢測的超時時長


option http-server-clone?

定義了keepalive功能,客戶端和服務(wù)器端之間的會話連接超時,允許server主動關(guān)閉.


客戶端可服務(wù)器端建立連接時,就開始記錄日志。(通常都是服務(wù)器端相應(yīng)完成才記錄日志,方便記錄服務(wù)器的相應(yīng)時間),如果打開此項,會提前記錄日志.

option logasap

no option logasap

Enable or disable early logging of HTTP requests


客戶端和服務(wù)器端連接建立,但是沒有任何的數(shù)據(jù)傳輸,即空連接,此項為不記錄空連接日志.

option dontlognull

no option dontlognull

Enable or disable logging of null connections


HAProxy Acl定義:

支持的值大概有四種:

整數(shù)或整數(shù)范圍/字符串/正則表達(dá)式/ip地址或者網(wǎng)絡(luò)地址


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> ]


總結(jié)

以上是生活随笔為你收集整理的了解HAProxy原理及参数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。