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
配置執行個體:
1
2
3
4
5
6
<code>frontend server *:80</code><code>//</code><code>定義前端frontend name為server 監聽80端口</code>
<code> </code><code>default_backend servers</code><code>//</code><code>定義預設的backend server為servers</code>
<code>backend servers </code><code>//</code><code>定義後端的backend name為servers</code>
<code> </code><code>balance roundrobin</code><code>//</code><code>負載均衡算法</code>
<code> </code><code>server node1 192.168.30.129:80 check</code><code>//</code><code>後端的節點:80一定要添加(嘗試不添加會報錯,1.4版本的貌似繼承上面frontend的端口),開啟健康檢查</code>
<code> </code><code>server node2 192.168.30.131:80 check</code>
浏覽器通路: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端口
<code># Provides UDP syslog reception</code>
<code>$ModLoad imudp</code>
<code>$UDPServerRun 514</code>
<code>2、新增:</code>
<code>local2.* </code><code>/var/log/haproxy</code><code>.log</code>
3、重新開機syslog和haproxy服務:
<code>service rsyslog restart</code>
<code>/etc/init</code><code>.d</code><code>/haproxy</code> <code>restart</code>
4、測試檢視日志:
<code>tail</code> <code>-f </code><code>/var/log/haproxy</code><code>.log</code>
bind參數:bind [<adresses>]:<port_range> [,...]
例子:
7
8
9
10
<code>bind :80,:443</code>
<code>bind 10.10.0.1:10080,10.10.0.1:10043</code>
<code>可以使用在listen和frontend子產品中:</code>
<code>frontend server </code>
<code>bind *:80</code>
<code>default_backendservers</code>
<code>backend servers</code>
<code>balanceroundrobin</code>
<code>servernode1192.168.30.129:80check</code>
<code>servernode2192.168.30.131:80check</code>
balance參數:
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節點
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 127.0.0.1local3#日志伺服器,在rsyslog.conf中定義,指定的frontend日志.
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 表示認證通過
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>
}
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層通路控制:
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> ]
本文轉自青衫解衣 51CTO部落格,原文連結:http://blog.51cto.com/215687833/1954400