HAProxy淺說:
HAProxy響應碼:
200:請求正常,響應正常,也就是正常響應碼
301:配置使用的重定向,以下都是有關于重定向的一些響應碼,不做解釋
302:
303:
307:
308:
400:客戶端錯誤,請求無效或是請求太大
401:身份驗證時的問題所向,如果通過有這個響應碼
403:請求沒有權限,一般為ACL或者是系統的filter功能對請求流進行了流量匹配執行了拒絕
408:請求超時
500:服務端錯誤,當HAProxy遇到不可恢復的內部錯誤時,會響應此響應碼,一般不會出現,因為,如果這么嚴重的錯誤就會變成404了
502:服務端響應為空,響應無效,響應不完整或者rspdeny過濾的問題時會出現這個響應碼
503:沒有服務器可用于處理請求,或者無法連接后端服務器產生的錯誤
504:在服務器響應之前觸發了請求超時
HAProxy的配置
配置文件的格式:
配置文件分為五段
globel:全局配置
defaults:默認配置,這一段的配置可以用于其它幾段的公有配置
listen:用于代理監聽的端口,也就是監聽那一個端口,也可以加入一些其它配置
frontend:前端配置,也就是代理配置
backend:后端配置
時間格式:
通常使用毫秒來表示時間的相關配置
配置試例:
global
daemon
maxconn 256
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind :80
default_backend servers
backend servers
server server1 127.0.0.1:8000 maxconn 32
以上的配置還可以寫成如下的形式
global
daemon
maxconn 256
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
listen http-in
bind *:80
server server1 127.0.0.1:8000 maxconn 32
全局配置指令(Global parameters)
chroot:指定HAProxy的運行根目錄,將HAProxy的權限降低,如有此目錄下的權限,其它目錄下HAProxy是沒有權限的
chroot /var/haproxy //一說是變更程序的特定目錄,那就與selinux有關了,修改其標簽或是關閉
deamon:運行為守護進程
log:日志存儲位置
log 127.0.0.1 local2 //本地日志文件存放位置,這個local2可以在/etc/rsyslog.conf文件中做配置
log <address> [len <length>] <facility> [max level [min level]] //定義日志長度,日志級別等
group:定義HAProxy的啟用進程的組
user:定義HAProxy的啟用進程的用戶
maxconn:最大連接數
maxconnrate:每秒最大連接數
maxcompcpuusage:最大CPU使用率
maxsessrate:每秒開啟的最大會話數
maxsslconn:ssl的最大連接數
maxsslrate:每秒ssl最大連接數
maxpipes:最大打開多少個管道文件
spread-checks:對邏輯服務器或者物理服務器進行健康檢測
Proxies段指令
defaults <name>:配置以下其它部分的公共配置段,也就是配置的默認配置
frontend <name>:前端監聽客戶端的配置
backend <name>:后端為前端提供服務的配置,也就是如何連接后端,有那些后端等
listen <name>:這里可以定義一個完整的代理,它的前端與后端組合在了一起,但是只可以定義TCP流量,所以只能進行偽四層代理
注:在配置文件中名字可以使用_-.:符號,當然也可以使用字母與數字
Proxy部分關鍵字說明
acl
acl <aclname> <criterion> [flags] [operator] <value> …
acl acl的名字 acl定義的標準 [標記] [選項] 值…
acl invalid_src src 0.0.0.0/7 224.0.0.0/3
acl invalid_src src_port 0:1023
acl local_dst hdr(host) -i localhost
返回的數據類型:
boolean:布爾型值
integer:整數
IPv4 or IPv6:IP地址
string:字符串
data block:數據塊
ACL可以匹配識別的數據類型
boolean:布爾型
integer or integer range:整數或者一個整數的范圍
IP or network:IP或者網段
string:字符串
hex block:十六進制塊
ACL的標記:
-i:忽略匹配內容的大小寫
-f:從文件中讀取所要的匹配內空
-m:使用特定的模式匹配
-n:禁止DNS解析
-M:加載一個相當于-f指定的文件,內容為健–值型調用
-u:強制ACL使用的唯一ID號
–:強制結束標記,也就是給相關的字符串進行非標記符進行轉意
選項
匹配整數時可用的運算符
eq,lt,le,gt,ge
字符串匹配:
exact match (-m str):提取的字符串必須完全匹配
substring match (-m sub):在內查找圖片,提取字符串,進行ACL匹配
prefix match (-m beg):對匹配字符串的開頭進行比較,匹配ACL
suffix match (-m end):對匹配字符串的尾部進行比較,匹配ACL
subdir match (-m dir):匹配一個路徑(uri)的其中一段,以/分割取其中的某些段
domain match (-m dom):匹配一個url,以.分割,取其中的特定段
ACL標準:
關于IP與端口的標準:
dst:目標IP
dst_port:目標端口
src:源IP
src_prot:源端口
關于PATH的,也就是URI的相關標準:
path:string //提取請求中的URL路徑,該路徑以/開頭,并在?之前結束
path : exact string match
path_beg : prefix match
path_dir : subdir match
path_dom : domain match
path_end : suffix match
path_len : length match
path_reg : regex match
path_sub : substring match
關于URI的相關標準:
url : exact string match
url_beg : prefix match
url_dir : subdir match
url_dom : domain match
url_end : suffix match
url_len : length match
url_reg : regex match
url_sub : substring match
關于請求頭部的標準:
hdr([<name>[,<occ>]]) : exact string match
hdr_beg([<name>[,<occ>]]) : prefix match
hdr_dir([<name>[,<occ>]]) : subdir match
hdr_dom([<name>[,<occ>]]) : domain match
hdr_end([<name>[,<occ>]]) : suffix match
hdr_len([<name>[,<occ>]]) : length match
hdr_reg([<name>[,<occ>]]) : regex match
hdr_sub([<name>[,<occ>]]) : substring match
示例:
acl valid_method method GET HEAD //定義一個acl,標準為method,方法為GET和HEAD
http-request deny if ! valid_method //對此acl進行操作,可以使用非
blance:算法
roundrobin:動態輪詢
static-rr:靜態輪詢
leastconn:最小連接
first:連接一臺服務器到其負載最大值,再連接下一臺服務器
source:對源IP進行綁定,類似于nginx的hash與ip_hash
uri:對uri左半部分進行hash,并由服務器總權重相除以后派發至某個挑出的服務器上
url_param:對用戶請求的uri的<param>部分參數值做為hash計算,并由服務器總權重相除以后溾發至某個挑出的服務器;通常用于追蹤用戶,以確保來自同一個用戶的請求始終發往同一個Backend Server上
hdr(<name>):對于每個http請求指定的頭部做hash,并由服務器總權重相除后派發至挑選的服務器上,沒有有效值的會被輪詢調度
rdp-chookie(<name>):也就是chookie綁定,對應的用戶只會由對應的服務器進行響就,也只有此一個服務器去響應,當然了,那些進行動靜分離的不算,如果沒有檢查到chookie,那么會進行輪詢對backend進行挑選
hash_type:定義使用的hash算法類型
map-bashed:除權法,也就是靜態算法
consistents:一致性哈希算法,這個是一個動態算法
bind:對端口進行綁定
bind *:80 //對80端口進行綁定
block:如果條件匹配就阻止第七層請求
acl test_name hdr_reg(Status Code) 200
block if ! test_name
default-backend <backend>:設定默認的backend,用于frontend中;
use_backend dynamic if url_dyn
use_backend static if url_css url_img extension_img
default_backend dynamic
default-server:為backend中的各server設定默認選項
default-server inter 1000 weight 13
mode:設定實例的運行模式與協議
tcp:基于四層代理,可代理mysql,ssh,ssl,等協議
http:僅當代理的協議為http時使用
health:工作為健康狀態的響應模式,當連接請求到達時回應OK后即斷開連接
cookie:在backend開啟基于cookie的長連接
cookie <name> [ rewrite | insert | prefix ] [ indirect ] [ nocache ]
[ postonly ] [ preserve ] [ httponly ] [ secure ]
[ domain <domain> ] [ maxidle <idle> ] [ maxlife <life> ]
cookie JSESSIONID prefix
cookie SRV insert indirect nocache
cookie SRV insert postonly indirect
cookie SRV insert indirect nocache maxidle 30m maxlife 8h
compression algo <algorithm>:啟用壓縮,設定壓縮算法
identity:適用于在開發時測試使用
gzip:壓縮為gzip
deflate:類似于gzip
compression algo gzip
compression type text/html text/plain
compression type <mime type>:
compression offload:
enable:將一個代理啟動,默認為啟動,所以此指令通常不使用
disable:將一個代理關閉,默認為啟動,在進行灰度發布時可以將一個代理設置為disable
errorfile <code> <file>:將HAProxy的錯誤返回為指定文件
errorfile 400 /etc/haproxy/errorfiles/400badreq.http
errorfile 408 /dev/null # workaround Chrome pre-connect bug
errorfile 403 /etc/haproxy/errorfiles/403forbid.http
errorfile 503 /etc/haproxy/errorfiles/503sorry.http
errorloc302 <code><url>:將HAProxy的錯誤重定向到指定頁面
errorloc303 <code><url>:
http-check expect [!] <match> <pattern>:在特定的內容或者狀態碼下進行http的健康檢測
status <string>:檢測響應碼確切的字符串
rstatus <regex>:檢測響應碼的匹配字符串
string <string>:檢測響應內空的確切字符串
rstring <regex>檢測響應內容的匹配字符串
http-check expect status 200 //響應狀態碼為200時進行檢測
http-check expect ! string SQL\ Error //響應內容為非”SQL Error”進行檢測
http-check expect ! rstatus ^5 //這個是非以5開頭的響應碼
http-check expect rstring <!–tag:[0-9a-f]</html> //檢測在html之前是否有正確的十六進制標簽
http-request:七層訪問控制
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> ]
http-request replace-header Cookie foo=([^;]);(.) foo=\1;ip=%bi;\2
http-request replace-value X-Forwarded-For ^192.168.(.)$ 172.16.\1
acl nagios src 192.168.129.3
acl local_net src 192.168.0.0/16
acl auth_ok http_auth(L1)
http-request allow if nagios
http-request allow if local_net auth_ok
http-request auth realm Gimme if local_net auth_ok
http-request deny
acl auth_ok http_auth_group(L1) G1
http-request auth unless auth_ok
http_ response:修改響應報文
http-response { allow | deny | add-header <name> <fmt> | set-nice <nice> |
set-header <name> <fmt> | del-header <name> |
replace-header <name> <regex-match> <replace-fmt> |
replace-value <name> <regex-match> <replace-fmt> |
set-log-level <level> | set-mark <mark> | set-tos <tos> |
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> ]
log:日志配置
log global //調用全局配置中的日志存儲
log <address> [len <length>] <facility> [<level> [<minlevel>]] //自定義日志
no log //不啟用日志
log global
log 127.0.0.1:514 local0 notice
log 127.0.0.1:514 local0 notice notice
log ${LOCAL_SYSLOG}:514 local0 notice
maxconn <conns>:指定配置HAProxy前端的最大并發連接
option forwardfor [ except <network> ] [ header <name> ] [ if-none ]:開啟報文頭部的X-Forwarded-For選項
backend www
mode http
option forwardfor header X-Client
redirect location <loc> [code <code>] <option> [{if | unless} <condition>]
redirect prefix <pfx> [code <code>] <option> [{if | unless} <condition>]
redirect scheme <sch> [code <code>] <option> [{if | unless} <condition>]
reqadd:在請求報文頭部添加信息
reqadd <string> [{if | unless} <cond>]
repadd:在響應報文頭部添加信息
reqdel:在請求報文頭部添加信息
repdel:在響應報文頭部刪除信息
server:在backend中申明一個服務器
server <name> <address>[:[port]] [param*]
param:
maxconn:當前server最大連接數
backlog <backlog>:連接達到最大數的后援隊列長度
backup:將此服務器定義為sorryserver
check:地當前server進行健康檢測
addr:對地址進行檢測
port:對端口進行檢測
inter <delay>:檢測時的延遲
rise <count>:連續多少次成功才會標記服務可用,默認為2
fall <count>:連續多少次失敗才會標記失敗,默認為3
cookie <value>:為當前server指定其cookie值,用于實現基于cookie的會話黏性;
disabled:標記為不可用;
redir <prefix>:將發往此server的所有GET和HEAD類的請求重定向至指定的URL;
weight <weight>:權重,默認為1;
server first 10.1.1.1:1080 cookie first check inter 1000
server second 10.1.1.2:1080 cookie second check inter 1000
server transp ipv4@
server backup ${SRV_BACKUP}:1080 backup
server www1_dc1 ${LAN_DC1}.101:80
server www1_dc2 ${LAN_DC2}.101:80
stats enable:開啟狀態頁
stats admin { if | unless } <cond>:定義狀態頁可以由那此主機登陸
stats admin if localhost
stats auth <user>:<passwd>:認證時的賬號和密碼,可使用多次;
stats uri <prefix>:自定義stats page uri
stats refresh <delay>:設定自動刷新時間間隔;
stats realm <string>:配置一個輸入提示
配置示例:
listen stats
bind :9099
stats enable
stats realm HAPorxy\ Stats\ Page
stats auth admin:admin
stats admin if TRUE
HAProxy實現的動靜分離,使用keepalived實現其高可用性,使用stats page對HAProxy進行管理
拓撲圖:
此環境配置分為四大步
配置backend
配置frondend
配置keepalived
配置全棧ssl
配置backend
配置node3:
yum -y install httpd php php-mysql showmount //安裝相關包
vim /etc/httpd/conf/httpd.conf
#DocumentRoot “/var/www/html”
vim /etc/httpd/conf.d/vhost.conf
<Directory /data>
Allowoverride none
require all granted
</Directory>
Listen *:8080
DirectoryIndex index.php index.html
<Virtualhost *:80>
Documentroot /data/static
</Virtualhost>
<Virtualhost *:8080>
Documentroot /data/dynamic
</Virtualhost>
mkdir /data/{static,dynamic} -p
vim /data/static/index.html
<h1>Web1:172.18.250.38</h1>
vim /data/dynamicl/index.php
<?php
echo “172.18.250.38”;
phpinfo();
?>
systemctl start httpd
配置node4
yum -y install httpd php php-mysql mariadb-server //安裝相關包
vim /etc/httpd/conf/httpd.conf
#DocumentRoot “/var/www/html”
vim /etc/httpd/conf.d/vhost.conf
<Directory /data>
Allowoverride none
Require all granted
</Directory>
Listen *:8080
DirectoryIndex index.php index.html
<Virtualhost *:80>
Documentroot /data/static
</Virtualhost>
<Virtualhost *:8080>
Documentroot /data/dynamic
</Virtualhost>
mkdir /data/{static,dynamic} -p
vim /data/static/index.html
<h1>Web2:172.18.251.103</h1>
vim /data/dynamicl/index.php
<?php
echo “172.18.251.103”;
phpinfo();
?>
systemctl start httpd
配置frondend:
配置node1:
yum -y install httpd haproxy
vim /var/www/html/index.html
<h1>I’m sorry</h1>
vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
listen stats_web *:3303
stats enable
stats realm HAProxy\ Basic
stats refresh 5s
stats uri /admin?stats
stats auth admin:admin
stats admin if TRUE
frontend main *:80
acl dynamic_web path_end .php
use_backend static if ! dynamic_web
use_backend dynamic if dynamic_web
backend static
balance roundrobin
server static1 172.18.250.38:80 check
server static2 172.18.251.103:80 check
server sorry_web1 127.0.0.1:8080 check backup
backend dynamic
balance roundrobin
server dynamic1 172.18.250.38:8080 check
server dynamic2 172.18.250.38:8080 check
server sorry_web2 127.0.0.1:8080 check backup
systemctl start haproxy
vim /etc/httpd/conf/httpd.conf
#Listen 80
Listen 8080
配置LVS_RS腳本,如下,兩個節點相同
#!/bin/bash
VIP=”172.18.35.105″
netmask=”255.255.255.255″
device=”enp0s3″
case $1 in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
ifconfig lo:0 $VIP netmask $netmask broadcast $VIM up
route add -host $VIP dev lo:0
;;
stop)
ifconfig lo:0 down
route del -host $VIP dev lo:0
;;
*)
echo “Usage $(basename $0) {start|stop}”
;;
esac
配置node2:
yum -y install haproxy httpd
vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
listen stats_web *:3303
stats enable
stats realm HAProxy\ Basic
stats auth admin:admin
stats uri /admin?stats
stats refresh 5s
stats admin if TRUE
frontend main *:80
acl dynamic_web path_end .php
use_backend dynamic if dynamic_web
use_backend static if ! dynamic_web
backend static
balance roundrobin
server static1 172.18.250.38:80 check
server static2 172.18.251.103:80 check
server sorry_web1 127.0.0.1:8080 check backup
backend dynamic
balance roundrobin
server dynamic1 172.18.250.38:8080 check
server dynamic2 172.18.251.103:8080 check
server sorry_web1 127.0.0.1:8080 check backup
service haproxy start
vim /etc/httpd/conf/httpd.conf
#Listen 80
Listen 8080
service httpd start
配置keepalived:
配置node6
yum -y install keepalived
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
sysadmin@firewall.loc
}
notification_email_from root@localhost.con
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id CentOS7-node5
vrrp_mcast_group4 224.0.0.18
}
vrrp_instance VI_1 {
state MASTER
interface enp0s3
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.18.35.105
}
}
virtual_server 172.18.35.105 80 {
delay_loop 6
lb_algo wrr
lb_kind dr
nat_mask 255.255.255.255
protocol TCP
real_server 172.18.252.96 80 {
weight 1
}
real_server 172.18.250.37 80 {
weight 1
}
}
virtual_server 172.18.35.105 3303 {
delay_loop 6
lb_algo wrr
lb_kind dr
nat_mask 255.255.255.255
protocol TCP
real_server 172.18.252.96 3303 {
weight 1
}
real_server 172.18.250.37 3303 {
weight 1
}
}
配置node6:
yum -y install keepalived
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
}
notification_email_from root@localhost.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id CentOS7-node6
vrrp_mcast_group4 224.0.0.18
}
vrrp_instance VI_1 {
state BACKUP
interface enp0s3
virtual_router_id 51
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.18.35.105
}
}
virtual_server 172.18.35.105 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.255
protocol TCP
real_server 172.18.252.96 80 {
weight 1
}
real_server 172.18.250.37 80 {
weight 1
}
}
virtual_server 172.18.35.105 3303 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.255
protocol TCP
real_server 172.18.252.96 3303 {
weight 1
}
real_server 172.18.250.37 3303 {
weight 1
}
}
原創文章,作者:gaomei,如若轉載,請注明出處:http://www.www58058.com/76267