haproxy配置詳解

一:haproxy簡介:

HAProxy提供高可用性、負載均衡以及基於TCP和HTTP應用的代 理,支持虛擬主機,它是免費、快速並且可靠的一種解決方案。HAProxy特別適用於那些負載特大的web站點,這些站點通常又需要會話保持或七層處理。HAProxy運行在當前的硬件上,完全可以支持數以萬計的併發連接。並且它的運行模式使得它可以很簡單安全的整合進您當前的架構中, 同時可以保護你的web服務器不被暴露到網絡上。

搭建環境:

node1.magedu.com

eth0:172.16.2.1

eth1:192.168.10.10

node2.magedu.com eth0:192.168.10.11

node3.magedu.com eth0:192.168.10.12

注意:node2和node3的默認網關指向192.168.10.10命令爲route add default gw 192.168.10.10

其次:在node1節點上要打開網卡之間的轉發功能

[root@node1 ~]# sysctl -a | grep ip_forward
[root@node1 ~]# vim /etc/sysctl.conf 將
net.ipv4.ip_forward = 1
[root@node1 ~]# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296


安裝haproxy:

首先在node1上安裝haproxy,查看生成的文件列表

[root@node1 ~]# rpm -ql haproxy
/etc/haproxy
/etc/haproxy/haproxy.cfg #配置文件
/etc/logrotate.d/haproxy #日誌輪轉
/etc/rc.d/init.d/haproxy #運行腳本
/usr/bin/halog           #日誌分析工具
/usr/sbin/haproxy


其次:分析haproxy的配置文件/etc/haproxy/haproxy.cfg

global#全局配置,定義haproxy進程的工作特性以及全局配置
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy #chroot運行路徑,增加安全性
    pidfile     /var/run/haproxy.pid#haproxy的pid存放路徑
    maxconn     4000#默認的最大連接數
    user        haproxy #運行haproxy的用戶
    group       haproxy#運行haproxy用戶所屬的組
    daemon#以守護進程的方式工作於後臺
    stats socket /var/lib/haproxy/stats


defaults

mode                    http #默認使用協議,可以爲{http|tcp|health}http:是七層協議,tcp是四層, health:只返回ok
log                     global
option                  httplog #詳細記錄http日誌
option                  dontlognull#不記錄健康檢查的日誌信息
option http-server-close
option forwardfor       except 127.0.0.0/8
option                  redispatch#ServerID對應的服務器宕機後,強制定向到其他運行正常的服務器
retries                 3 #3次連接失敗則認爲服務不可用
timeout http-request    10s #默認http請求超時時間
timeout queue           1m#默認隊列超時時間
timeout connect         10s#默認連接超時時間
timeout client          1m#默認客戶端超時時間
timeout server          1m#默認服務器端超時時間
timeout http-keep-alive 10s#默認持久連接超時時間
timeout check           10s#心跳檢測超時
maxconn                 3000#默認最大的連接數


二:haproxy負載均衡實例:

修改配置文件/etc/haproxy/haproxy.cfg

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  webserver
bind *:80
    default_backend             appservs
#---------------------------------------------------------------------
# static backend for serving up p_w_picpaths, stylesheets and such
#---------------------------------------------------------------------
backend appservs
    server      node2.magedu.com 192.168.10.11:80 check
    server      node3.magedu.com 192.168.10.12:80 check

然後重啓haproxy即:service haproxy restart

也可以直接定義一個listen

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
listen webservers
bind *:80
server node2.magedu.com 192.168.10.11:80 check
server node3.magedu.com 192.168.10.12:80 check

listen和frontend,backend可以同時使用,基於不同端口的虛擬主機

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
listen webservers
bind *:80
server node2.magedu.com 192.168.10.11:80 check
frontend imgservers
bind *:8080
default_backend imgservs
backend imgservs
server node3.magedu.com 192.168.10.12:80 check


啓用狀態:stats enable

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
listen webservers
bind *:80
server node2.magedu.com 192.168.10.11:80 check
stats enable
frontend imgservers
bind *:8080
default_backend imgservs
backend imgservs
server node3.magedu.com 192.168.10.12:80 check

進行訪問172.16.2.1/haproxy?stats

202842725.png

haproxy的狀態頁面一般不允許別人隨意查看,因此通過認證進行訪問是必要的

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
listen webservers
bind *:80
server node2.magedu.com 192.168.10.11:80 check
stats enable#顯示狀態頁面
stats hide-version #隱藏haproxy的版本號
stats realm HAProxy\ Stats #提示信息
stats auth admin:admin #登錄狀態頁面的帳號和密碼
frontend imgservers
bind *:8080
default_backend imgservs
backend imgservs
server node3.magedu.com 192.168.10.12:80 check

203007175.png

203051352.png

基於頁面的管理功能:

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
listen webservers
bind *:80
server node2.magedu.com 192.168.10.11:80 check
listen stats
bind *:1088 #僞裝的端口號
stats enable
stats hide-version
stats realm HAProxy\ Stats
stats auth admin:admin
stats admin if TRUE #狀態頁面出現管理功能
stats uri /admin?admin #訪問入口
frontend imgservers
bind *:8080
default_backend imgservs
backend imgservs
server node3.magedu.com 192.168.10.12:80 check

203218313.png

203253758.png

支持權重:

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  webservers
bind *:80
default_backend webservs
listen stats
bind *:1088
stats enable
stats hide-version
stats realm HAProxy\ Stats
stats auth admin:admin
stats admin if TRUE
stats uri /admin?admin
backend webservs
server node3.magedu.com 192.168.10.12:80 check weight 3
server node2.magedu.com 192.168.10.11:80 check weight 1

調度方法:

roundrobin 動態

static-rr 靜態

source取決於hash-type

hash-type:map-based 靜態調度

hash-type:consistent 動態調度

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  webservers
bind *:80
default_backend webservs
listen stats
bind *:1088
stats enable
stats hide-version
stats realm HAProxy\ Stats
stats auth admin:admin
stats admin if TRUE
stats uri /admin?admin
backend webservs
balance source
hash-type consistent
server node3.magedu.com 192.168.10.12:80 check weight 3
server node2.magedu.com 192.168.10.11:80 check weight 1

使用balance uri可以使訪問固定到一個後端的服務器上

frontend  webservers
        bind *:80
        default_backend webservs
listen stats
        bind *:1088
        stats enable
        stats hide-version
        stats realm HAProxy\ Stats
        stats auth admin:admin
        stats admin if TRUE
        stats uri /admin?admin
backend webservs
        balance uri
        hash-type consistent
        server node3.magedu.com 192.168.10.12:80 check weight 3
        server node2.magedu.com 192.168.10.11:80 check weight 1


轉發功能redir

backend webservs
balance roundrobin
server node3.magedu.com 192.168.10.12:80 check weight 3 redir http://172.16.0.1
server node2.magedu.com 192.168.10.11:80 check weight 1

203532530.png

backend webservs
balance roundrobin
server node3.magedu.com 192.168.10.12:80 check weight 3 backup
server node2.magedu.com 192.168.10.11:80 check weight 1

203709744.png

backend webservs
balance roundrobin
server node3.magedu.com 192.168.10.12:80 check weight 3
server node2.magedu.com 192.168.10.11:80 check weight 1
server backup.magedu.com 127.0.0.1:8008 check weight 1 backup

只有當node2和node3都停掉時才使用backup

203824292.png

203845980.png

性能相關參數中maxconn的使用

backend webservs
balance roundrobin
server node3.magedu.com 192.168.10.12:80 check weight 3 maxconn 3000
server node2.magedu.com 192.168.10.11:80 check weight 1 maxconn 2000
server backup.magedu.com 127.0.0:8008 check weight 1 backup


三:haproxy中ACL的使用

定義ACL的語法格式爲:

acl <aclname> <criterion> [flags] [operator] <value> ...

<aclname>:ACL名稱,區分字符大小寫,且其只能包含大小寫字母、數字、-(連接線)、_(下劃線)、.(點號)和:(冒號);haproxy中,acl可以重名,這可以把多個測試條件定義爲一個共同的acl;

<criterion>:測試標準,即對什麼信息發起測試;測試方式可以由[flags]指定的標誌進行調整;而有些測試標準也可以需要爲其在<value>之前指定一個操作符[operator];

[flags]:目前haproxy的acl支持的標誌位有3個:

-i:不區分<value>中模式字符的大小寫;

-f:從指定的文件中加載模式;

--:標誌符的強制結束標記,在模式中的字符串像標記符時使用;

<value>:acl測試條件支持的值有以下四類:

整數或整數範圍:如1024:65535表示從1024至65535;僅支持使用正整數(如果出現類似小數的標識,其爲通常爲版本測試),且支持使用的操作符有5個,分別爲eq、ge、gt、le和lt;

字符串:支持使用“-i”以忽略字符大小寫,支持使用“\”進行轉義;如果在模式首部出現了-i,可以在其之前使用“--”標誌位;

正則表達式:其機制類同字符串匹配;

IP地址及網絡地址

同一個acl中可以指定多個測試條件,這些測試條件需要由邏輯操作符指定其關係。條件間的組合測試關係有三種:“與”(默認即爲與操作)、“或”(使用“||”操作符)以及“非”(使用“!”操作符)。


用例:

用法一:
將源IP爲172.16.253.254的用戶禁止、將403的錯誤重定向到其他服務器;
frontend  webservers
bind *:80
default_backend webservs
acl badguy src 172.16.253.254
block if badguy
errorloc 403 http://www.baidu.com
用法二:
當用戶訪問地址爲172.16.2.1時,將訪問頁面重定向http://www.baidu.com
frontend  webservers
bind *:80
default_backend webservs
acl dstipaddr hdr(Host) 172.16.2.1
redirect location http://www.baidu.com if dstipaddr
用法三:
acl中path的使用
frontend  webservers
bind *:80
default_backend webservs
acl badguy src 172.16.253.254
acl denyfile path /1.html
http-request deny if badguy denyfile
用法四:
讀寫分離:
acl  read method GET
acl  read method HEAD
acl write method PUT
acl write method POST
use_backend imgservers if read
use_backend uploadservers if write

用法五:

動靜分離

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  webservers
bind *:80
acl static path_end .html
use_backend staticservs if static
default_backend appservs
listen stats
bind *:1088
stats enable
stats hide-version
stats realm HAProxy\ Stats
stats auth admin:admin
stats admin if TRUE
stats uri /admin?admin
backend staticservs
balance roundrobin
server node2.magedu.com 192.168.10.11:80 check weight 1 maxconn 3000
server backup.magedu.com 127.0.0:8008 check weight 1 backup
backend appservs
balance roundrobin
server node3.magedu.com 192.168.10.12:80 check maxconn 2000

204130644.png


204148842.png

更多信息請參考官方主站:http://cbonte.github.io/haproxy-dconv/configuration-1.4.html



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章