HAProxy基礎配置-haproxy常見的調度算法


        HAProxy基礎配置-haproxy常見的調度算法

                                   作者:尹正傑

版權聲明:原創作品,謝絕轉載!否則將追究法律責任。

 

  我們知道nginx有四種(rr,ip_hash,uri hash,lastconn)常用的調度算法,LVS有10種(常用的也就3-4種,而且生產環境種使用lvs的場景相對較少,畢竟大多數公司沒有淘寶,天貓那個訪問量)調度算法,而haproxy的調度算法相對nginx還是比較多的。

  除此之外,haproxy還支持通過socket的方式實現服務器動態的上下限,而nginx和lvs卻不支持該功能,接下來我們一起學習一下haproxy吧~

 

 

一.HAProxy調度算法需要使用balance指令來聲明

  balance:
    指明對后端服務器的調度算法,配置在listen或backend

1>.編輯haproxy的配置文件(開啟socket功能,便於下面測試動態調度算法與通過該socket和haproxy服務在不重啟的情況下動態變更配置)

[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg 
global
maxconn 100000
chroot /yinzhengjie/softwares/haproxy
#如果需要使用動態調度算法需要將socket功能打開 stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
nbthread 2
pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
log 127.0.0.1 local5 info

defaults
option http-keep-alive
option  forwardfor
option redispatch
option abortonclose
maxconn 100000
mode http
timeout connect 300000ms
timeout client  300000ms
timeout server  300000ms

listen stats
 mode http
 bind 0.0.0.0:9999
 stats enable
 log global
 stats uri     /haproxy-status
 stats auth    haadmin:q1w2e3r4ys

listen WEB_PORT_80
    bind 172.30.1.102:80
    mode http
    server web01 172.30.1.106:80 check inter 3000 fall 3 rise 5
    server web02 172.30.1.107:80 check inter 3000 fall 3 rise 5
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/haproxy/haproxy.sock 
srw------- 1 root root 0 Jan  3 14:36 /yinzhengjie/softwares/haproxy/haproxy.sock
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/haproxy/haproxy.pid 
-rw-r--r-- 1 root root 6 Jan  3 14:36 /yinzhengjie/softwares/haproxy/haproxy.pid
[root@node102.yinzhengjie.org.cn ~]# 

2>.安裝socat命令(下面在測試動態修改權重案例時會用到該工具)

[root@node102.yinzhengjie.org.cn ~]# yum -y install socat
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                                                                      | 9.1 kB  00:00:00     
 * base: mirrors.aliyun.com
 * epel: mirrors.yun-idc.com
 * extras: mirror.bit.edu.cn
 * updates: mirrors.aliyun.com
http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86_64/repodata/repomd.xml: [Errno 12] Timeout on http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86
_64/repodata/repomd.xml: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')Trying other mirror.
base                                                                                                                      | 3.6 kB  00:00:00     
extras                                                                                                                    | 2.9 kB  00:00:00     
updates                                                                                                                   | 2.9 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package socat.x86_64 0:1.7.3.2-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================================
 Package                         Arch                             Version                                   Repository                      Size
=================================================================================================================================================
Installing:
 socat                           x86_64                           1.7.3.2-2.el7                             base                           290 k

Transaction Summary
=================================================================================================================================================
Install  1 Package

Total download size: 290 k
Installed size: 1.1 M
Downloading packages:
socat-1.7.3.2-2.el7.x86_64.rpm                                                                                            | 290 kB  00:00:07     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : socat-1.7.3.2-2.el7.x86_64                                                                                                    1/1 
  Verifying  : socat-1.7.3.2-2.el7.x86_64                                                                                                    1/1 

Installed:
  socat.x86_64 0:1.7.3.2-2.el7                                                                                                                   

Complete!
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# yum -y install socat
[root@node102.yinzhengjie.org.cn ~]# echo "help" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock       #查看haproxy的socket使用的幫助信息
Unknown command. Please enter one of the following commands only :
  help           : this message
  prompt         : toggle interactive mode with prompt
  quit           : disconnect
  show tls-keys [id|*]: show tls keys references or dump tls ticket keys when id specified
  set ssl tls-key [id|keyfile] <tlskey>: set the next TLS key for the <id> or <keyfile> listener to <tlskey>
  show errors    : report last request and response errors for each proxy
  disable agent  : disable agent checks (use 'set server' instead)
  disable health : disable health checks (use 'set server' instead)
  disable server : disable a server for maintenance (use 'set server' instead)
  enable agent   : enable agent checks (use 'set server' instead)
  enable health  : enable health checks (use 'set server' instead)
  enable server  : enable a disabled server (use 'set server' instead)
  set maxconn server : change a server's maxconn setting
  set server     : change a server's state, weight or address
  get weight     : report a server's current weight
  set weight     : change a server's weight (deprecated)
  show sess [id] : report the list of current sessions or dump this session
  shutdown session : kill a specific session
  shutdown sessions server : kill sessions on a server
  clear table    : remove an entry from a table
  set table [id] : update or create a table entry's data
  show table [id]: report table usage stats or dump this table's contents
  clear counters : clear max statistics counters (add 'all' for all counters)
  show info      : report information about the running process
  show stat      : report counters for each proxy and server
  show schema json : report schema used for stats
  show startup-logs : report logs emitted during HAProxy startup
  show resolvers [id]: dumps counters from all resolvers section and
                     associated name servers
  set maxconn global : change the per-process maxconn setting
  set rate-limit : change a rate limiting value
  set severity-output [none|number|string] : set presence of severity level in feedback information
  set timeout    : change a timeout setting
  show env [var] : dump environment variables known to the process
  show cli sockets : dump list of cli sockets
  show fd [num] : dump list of file descriptors in use
  show activity : show per-thread activity stats (for support/developers)
  disable frontend : temporarily disable specific frontend
  enable frontend : re-enable specific frontend
  set maxconn frontend : change a frontend's maxconn setting
  show servers state [id]: dump volatile server information (for backend <id>)
  show backend   : list backends in the current running config
  shutdown frontend : stop a specific frontend
  set dynamic-cookie-key backend : change a backend secret key for dynamic cookies
  enable dynamic-cookie backend : enable dynamic cookies on a specific backend
  disable dynamic-cookie backend : disable dynamic cookies on a specific backend
  show cache     : show cache status
  add acl        : add acl entry
  clear acl <id> : clear the content of this acl
  del acl        : delete acl entry
  get acl        : report the patterns matching a sample for an ACL
  show acl [id]  : report available acls or dump an acl's contents
  add map        : add map entry
  clear map <id> : clear the content of this map
  del map        : delete map entry
  get map        : report the keys and values matching a sample for a map
  set map        : modify map entry
  show map [id]  : report available maps or dump a map's contents
  show pools     : report information about the memory pools usage

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# 

 

二.HAProxy的靜態調度算法

  靜態算法:
    按照事先定義好的規則輪詢公平調度,不關心后端服務器的當前負載、鏈接數和相應速度等,且無法實時修改權重,只能重啟后生效。

  static
-rr:
    基於權重的輪詢調度,不支持權重的運行時調整及后端服務器慢啟動,其后端主機數量沒有限制。
  first:(生產環境使用場景較少)
    根據服務器在列表中的位置,自上而下進行調度,但是其只會當第一台服務器的連接數達到上限,新請求才會分配給下一台服務,因此會忽略服務器的權重設置。

1>.測試static-rr靜態調度算法

[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg                   #指定static-rr靜態調度算法
global
maxconn 100000
chroot /yinzhengjie/softwares/haproxy
#如果需要使用動態調度算法需要將socket功能打開
stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
nbthread 2
pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
log 127.0.0.1 local5 info

defaults
option http-keep-alive
option  forwardfor
option redispatch
option abortonclose
maxconn 100000
mode http
timeout connect 300000ms
timeout client  300000ms
timeout server  300000ms

listen stats
 mode http
 bind 0.0.0.0:9999
 stats enable
 log global
 stats uri     /haproxy-status
 stats auth    haadmin:q1w2e3r4ys

listen WEB_PORT_80
    bind 172.30.1.102:80
    mode http
    balance static-rr
    server web01 172.30.1.106:80 maxconn 3 check inter 3000 fall 3 rise 5
    server web02 172.30.1.107:80 check inter 3000 fall 3 rise 5
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg                   #指定static-rr靜態調度算法(別忘記重啟haproxy)
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web01" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web02" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "set weight WEB_PORT_80/web01 3" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock       #我們發現static-rr靜態調度算法不支持動態修改權重。
Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web01" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 

2>.測試first靜態調度算法(生產環境很少使用這種調度算法)

[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg                           #指定first調度算法
global
maxconn 100000
chroot /yinzhengjie/softwares/haproxy
#如果需要使用動態調度算法需要將socket功能打開
stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
nbthread 2
pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
log 127.0.0.1 local5 info

defaults
option http-keep-alive
option  forwardfor
option redispatch
option abortonclose
maxconn 100000
mode http
timeout connect 300000ms
timeout client  300000ms
timeout server  300000ms

listen stats
 mode http
 bind 0.0.0.0:9999
 stats enable
 log global
 stats uri     /haproxy-status
 stats auth    haadmin:q1w2e3r4ys

listen WEB_PORT_80
    bind 172.30.1.102:80
    mode http
    balance first
    server web01 172.30.1.106:80 check inter 3000 fall 3 rise 5
    server web02 172.30.1.107:80 check inter 3000 fall 3 rise 5
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg      #指定first調度算法且不指定服務器連接數上線(別忘記重啟haproxy)
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 20`;do curl http://node102.yinzhengjie.org.cn/;done      #很明顯所有的請求都達到了web01的節點上。
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 20`;do curl http://node102.yinzhengjie.org.cn/;done      #很明顯所有的請求都達到了web01的節點上。
[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg                             #使用first調度算法且指定服務器連接數上線為3
global
maxconn 100000
chroot /yinzhengjie/softwares/haproxy
#如果需要使用動態調度算法需要將socket功能打開
stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
nbthread 2
pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
log 127.0.0.1 local5 info

defaults
option http-keep-alive
option  forwardfor
option redispatch
option abortonclose
maxconn 100000
mode http
timeout connect 300000ms
timeout client  300000ms
timeout server  300000ms

listen stats
 mode http
 bind 0.0.0.0:9999
 stats enable
 log global
 stats uri     /haproxy-status
 stats auth    haadmin:q1w2e3r4ys

listen WEB_PORT_80
    bind 172.30.1.102:80
    mode http
    balance first
    server web01 172.30.1.106:80 maxconn 3 check inter 3000 fall 3 rise 5
    server web02 172.30.1.107:80 check inter 3000 fall 3 rise 5
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg       #使用first調度算法且指定服務器連接數上線為3(別忘記重啟haproxy)
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web01" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web02" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "set weight WEB_PORT_80/web01 3" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock       #很明顯,first靜態調度算法是支持動態修改權重的。

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web01" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
3 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 

 

三.HAProxy的動態調度算法

  動態算法:
    基於后端服務器狀態進行調度適當調整,比如優先調度至當前負載較低的服務器,且權重可以在haproxy運行時動態調整無需重啟,若重啟后,運行時配置的動態策略會被清空,依舊使用配置文件默認的配置喲~
  roundrobin:
    基於權重的輪詢動態調度算法,支持權重的運行時調整,不等於lvs的rr,支持慢啟動即新加的服務器會逐漸增加轉發數,每個后端backend中最多支持4095個server,此為默認調度算法,server權重設置weight默認為1。     一般工作在四層做session共享用的最多的調度算法。    

  leastconn:
    加權的最少連接的動態,支持權重的運行時調整和慢啟動,即當前后端服務器連接最少的優先調度,比較適合長連接的場景使用,比如:LDAP,MySQL等場景。
    一般工作在四層用於后端服務器MySQL,LDAP等場景。

1>.基於roundrobin動態調度算法調整權重案例

[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg               #在配置文件中無需使用"balance"指定調度算法,因為默認就是"roundrobin"動態調度算法
global
maxconn 100000
chroot /yinzhengjie/softwares/haproxy
#如果需要使用動態調度算法需要將socket功能打開
stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
nbthread 2
pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
log 127.0.0.1 local5 info

defaults
option http-keep-alive
option  forwardfor
option redispatch
option abortonclose
maxconn 100000
mode http
timeout connect 300000ms
timeout client  300000ms
timeout server  300000ms

listen stats
 mode http
 bind 0.0.0.0:9999
 stats enable
 log global
 stats uri     /haproxy-status
 stats auth    haadmin:q1w2e3r4ys

listen WEB_PORT_80
    bind 172.30.1.102:80
    mode http
    server web01 172.30.1.106:80 check inter 3000 fall 3 rise 5
    server web02 172.30.1.107:80 check inter 3000 fall 3 rise 5
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg          #在配置文件中無需使用"balance"指定調度算法,因為默認就是"roundrobin"動態調度算法
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/;done          #未修改權重之前,訪問haproxy服務器
<h1>node106.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/;done       #未修改權重之前,訪問haproxy服務器,基本上時響應均勻的
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web01" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock     #默認的權重為1  
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web02" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock     
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web03" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock      #由於我在配置文件中並沒有定義web03節點,因此會返回"No such server"
No such server.

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "set weight WEB_PORT_80/web01 3" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock     #此處我將web01的權重動態修改為"3"

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web01" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock      #再次查看web01的權重果真變為3啦。
3 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/;done      #修改權重之后,再次訪問haproxy服務器,很明顯偏向"web01"節點
<h2>node107.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/;done      #修改權重之后,再次訪問haproxy服務器,很明顯偏向"web01"節點

2>.服務器動態上下線案例(在haproxy 1.8多進程情況下貌似不支持,盼望官網早日修復該bug,但在haproxy 1.5確實是支持的)

[root@node102.yinzhengjie.org.cn ~]# echo "disable server WEB_PORT_80/web01" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock    #使節點下線,該方法在HAProxy1.5版本親測是好使的,但是在HAProxy1.8卻不太好使!希望官網早日修復吧

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "enable server WEB_PORT_80/web01" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock    #使節點上線,如果非要測試效果,可以使用yum方式安裝haproxy1.5的版本來測試

[root@node102.yinzhengjie.org.cn ~]#

3>.leastconn調度算法配置案例

[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg                 #使用balance指定leastconn動態調度算法
global
maxconn 100000
chroot /yinzhengjie/softwares/haproxy
#如果需要使用動態調度算法需要將socket功能打開
stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
nbthread 2
pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
log 127.0.0.1 local5 info

defaults
option http-keep-alive
option  forwardfor
option redispatch
option abortonclose
maxconn 100000
mode http
timeout connect 300000ms
timeout client  300000ms
timeout server  300000ms

listen stats
 mode http
 bind 0.0.0.0:9999
 stats enable
 log global
 stats uri     /haproxy-status
 stats auth    haadmin:q1w2e3r4ys

listen WEB_PORT_80
    bind 172.30.1.102:80
    mode http
    balance leastconn
    server web01 172.30.1.106:80 check inter 3000 fall 3 rise 5
    server web02 172.30.1.107:80 check inter 3000 fall 3 rise 5
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg                 #使用balance指定leastconn動態調度算法(別忘記重啟haproxy)
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 20`;do curl http://node102.yinzhengjie.org.cn/;done        
<h1>node106.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 20`;do curl http://node102.yinzhengjie.org.cn/;done   
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web01" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web02" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "set weight WEB_PORT_80/web01 3" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock       #很明顯,leastconn動態調度算法也是支持動態修改權重的。

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web01" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
3 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# 

 

四.source調度算法

  source:
    源地址hash,基於用戶源地址hash並將請求轉發到后端服務器,默認為靜態(即取模方式),但是可以通過hash-type支持的選項更改,后續同一個源地址請求將被轉發至同一個后端web服務器,比較適用於后端服務器沒有做session共享,但是還要實現session保持/緩存業務等場景。
      map-based:
        取模法,基於服務器權重的hash數組取模,該hash是靜態的即不支持在線調整權重,不支持慢啟動,其對后端服務器調度均衡,缺點是當服務器的總權重發生變化時,即有服務器上線或下線,都會因權重發生變化而導致調度結果整體改變。
      consistent:
        一致性哈希,該hash是動態的,支持在線調整權重,支持慢啟動,優點在於當服務器的總權重發生變化時,對調度結果影響是局部的,不會引起大的變動。

    source這種調度算法有一個致命缺點就是調度不均衡的問題,我們知道在一個公司局域網中所有的私網地址都會被NAT成一個公網IP地址,假設該公司有5萬員工,那么該公司的五萬員工的連接都會通過一個公網IP地址訪問haproxy,由於這5萬個主機都通過同一個公網IP地址訪問haproxy,基於source調度算法會將這玩5萬個主機的所有請求同時打到同一台后端web服務器,從而驗證導致負載不均衡。

1>.source調度算法配置基於取模法(map-based,靜態調度算法)案例

[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg                   #使用balance只當source調度算法
global
maxconn 100000
chroot /yinzhengjie/softwares/haproxy
#如果需要使用動態調度算法需要將socket功能打開
stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
nbthread 2
pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
log 127.0.0.1 local5 info

defaults
option http-keep-alive
option  forwardfor
option redispatch
option abortonclose
maxconn 100000
mode http
timeout connect 300000ms
timeout client  300000ms
timeout server  300000ms

listen stats
 mode http
 bind 0.0.0.0:9999
 stats enable
 log global
 stats uri     /haproxy-status
 stats auth    haadmin:q1w2e3r4ys

listen WEB_PORT_80
    bind 172.30.1.102:80
    mode http
    balance source
    server web01 172.30.1.106:80 check inter 3000 fall 3 rise 5
    server web02 172.30.1.107:80 check inter 3000 fall 3 rise 5
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg      #使用balance指定source調度算法,無需使用hash-type指令,因為默認就是map-based(別忘記重啟haproxy)
[root@node102.yinzhengjie.org.cn ~]# for _ in `seq 20`;do curl http://node102.yinzhengjie.org.cn/;done      #使用haproxy本機測試訪問,發現所有的請求都被調度到同一台服務器了
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# for _ in `seq 20`;do curl http://node102.yinzhengjie.org.cn/;done    #使用haproxy本機測試訪問,發現所有的請求都被調度到同一台服務器了
[root@node107.yizhengjie.org.cn ~]# for _ in `seq 20`;do curl http://node102.yinzhengjie.org.cn/;done     #使用172.30.1.107測試訪問haproxy,發現所有的請求依舊被調度到同一台服務器了
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# for _ in `seq 20`;do curl http://node102.yinzhengjie.org.cn/;done    #使用172.30.1.107測試訪問,發現所有的請求依舊被調度到同一台服務器了
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 20`;do curl http://node102.yinzhengjie.org.cn/;done    #結果同上,所有的請求都被同一台web服務器響應了。
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 20`;do curl http://node102.yinzhengjie.org.cn/;done    #結果同上,所有的請求都被同一台web服務器響應了。
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web01" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web02" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "set weight WEB_PORT_80/web01 3" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock     #很顯然,source調度算法默認是靜態算法,因為它不支持動態修改權重。
Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web01" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# 

2>.source調度算法配置基於一致性哈希(consistent,動態調度算法)案例

[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg       #使用balance指定只當source調度算法並使用hash-type指定基於consistent(一致性哈希),別忘記重啟haproxy喲
global
maxconn 100000
chroot /yinzhengjie/softwares/haproxy
#如果需要使用動態調度算法需要將socket功能打開
stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
nbthread 2
pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
log 127.0.0.1 local5 info

defaults
option http-keep-alive
option  forwardfor
option redispatch
option abortonclose
maxconn 100000
mode http
timeout connect 300000ms
timeout client  300000ms
timeout server  300000ms

listen stats
 mode http
 bind 0.0.0.0:9999
 stats enable
 log global
 stats uri     /haproxy-status
 stats auth    haadmin:q1w2e3r4ys

listen WEB_PORT_80
    bind 172.30.1.102:80
    mode http
    balance source
    hash-type consistent
    server web01 172.30.1.106:80 check inter 3000 fall 3 rise 5
    server web02 172.30.1.107:80 check inter 3000 fall 3 rise 5
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg    #使用balance指定只當source調度算法並使用hash-type指定基於consistent(一致性哈希),別忘記重啟haproxy喲

 

五.uri(uniform resource identifier,統一資源標識符,是一個用於標識某一互聯網資源名稱的字符串)調度算法

  uri:
    基於對用戶請求的uri做hash並將請求轉發到后端指定服務器,適用於緩存業務場景。換句話說,同一個節點訪問不同的uri可能會被調度到不同的后端服務器,但是不同的瀏覽器訪問相同的uri則會被調度到同一台服務器,因為基於uri調度是在服務端完成的而非客戶端。這種調度算法多用在類似CDN緩存場景。
    和source調度算法類似,uri調度算法也支持取模法和一致性哈希,配置方式也一樣,默認都是取模法,如下所示:
      map-based:
        取模法,基於服務器權重的hash數組取模,該hash是靜態的即不支持在線調整權重,不支持慢啟動,其對后端服務器調度均衡,缺點是當服務器的總權重發生變化時,即有服務器上線或下線,都會因權重發生變化而導致調度結果整體改變。
      consistent:
        一致性哈希,該hash是動態的,支持在線調整權重,支持慢啟動,優點在於當服務器的總權重發生變化時,對調度結果影響是局部的,不會引起大的變動。

1>.uri調度算法使用一致性哈希(consistent)案例

[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg       #使用balance指定haproxy調度算法為uri並使用hash-type指定為一致性哈希算法(別忘記重啟haproxy)
global
maxconn 100000
chroot /yinzhengjie/softwares/haproxy
#如果需要使用動態調度算法需要將socket功能打開
stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
nbthread 2
pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
log 127.0.0.1 local5 info

defaults
option http-keep-alive
option  forwardfor
option redispatch
option abortonclose
maxconn 100000
mode http
timeout connect 300000ms
timeout client  300000ms
timeout server  300000ms

listen stats
 mode http
 bind 0.0.0.0:9999
 stats enable
 log global
 stats uri     /haproxy-status
 stats auth    haadmin:q1w2e3r4ys

listen WEB_PORT_80
    bind 172.30.1.102:80
    mode http
    balance uri
    hash-type consistent
    server web01 172.30.1.106:80 check inter 3000 fall 3 rise 5
    server web02 172.30.1.107:80 check inter 3000 fall 3 rise 5
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg       #使用balance指定haproxy調度算法為uri並使用hash-type指定為一致性哈希算法(別忘記重啟haproxy)
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app01/index.html;done      #很明顯,訪問不同的uri會被調度到不同的節點
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app02/index.html;done
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app01/index.html;done      #很明顯,訪問不同的uri會被調度到不同的節點
[root@node107.yizhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app01/index.html;done      #很明顯,訪問相同的uri無論是哪個客戶端都被調度到相同的節點。
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app02/index.html;done
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app01/index.html;done      #很明顯,訪問相同的uri無論是哪個客戶端都被調度到相同的節點。

 2>.uri調度算法配置基於取模法(map-based,靜態調度算法)案例

[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg       #使用balance指定haproxy調度算法為uri,無需使用hash-type,因為默認就是map-based(別忘記重啟haproxy)
global
maxconn 100000
chroot /yinzhengjie/softwares/haproxy
#如果需要使用動態調度算法需要將socket功能打開
stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
nbthread 2
pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
log 127.0.0.1 local5 info

defaults
option http-keep-alive
option  forwardfor
option redispatch
option abortonclose
maxconn 100000
mode http
timeout connect 300000ms
timeout client  300000ms
timeout server  300000ms

listen stats
 mode http
 bind 0.0.0.0:9999
 stats enable
 log global
 stats uri     /haproxy-status
 stats auth    haadmin:q1w2e3r4ys

listen WEB_PORT_80
    bind 172.30.1.102:80
    mode http
    balance uri
    #若不配置hash-type,則模式使用"hash-type map-based"
    #hash-type consistent
    server web01 172.30.1.106:80 check inter 3000 fall 3 rise 5
    server web02 172.30.1.107:80 check inter 3000 fall 3 rise 5
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg       #使用balance指定haproxy調度算法為uri,無需使用hash-type,因為默認就是map-based(別忘記重啟haproxy)
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app01/index.html;done      #客戶端訪問結果和上面一樣
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app02/index.html;done
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app01/index.html;done      #客戶端訪問結果和上面一樣
[root@node107.yizhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app01/index.html;done
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
172.30.1.107 app01
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app02/index.html;done
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app01/index.html;done       #同上
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web01" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web02" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "set weight WEB_PORT_80/web01 3" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock       #靜態調度算法不支持動態修改權重
Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web01" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# 

 

六.url_param調度算法(生產環境使用的較少)

  url_param:
    對用戶請求的url中的<params>部分中的參數name作hash計算,並由服務器總權重相除以后派發至某挑出的服務器;通常用於追蹤用戶,以確保來自同一個用戶的請求始終發往同一個Backend Server。這種調度算法也多用在CDN的緩存場景。

1>.實戰案例

[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg               #使用balance指定url_param調度算法,需要指定一個參數名稱喲(別忘記重啟haproxy)
global
maxconn 100000
chroot /yinzhengjie/softwares/haproxy
#如果需要使用動態調度算法需要將socket功能打開
stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
nbthread 2
pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
log 127.0.0.1 local5 info

defaults
option http-keep-alive
option  forwardfor
option redispatch
option abortonclose
maxconn 100000
mode http
timeout connect 300000ms
timeout client  300000ms
timeout server  300000ms

listen stats
 mode http
 bind 0.0.0.0:9999
 stats enable
 log global
 stats uri     /haproxy-status
 stats auth    haadmin:q1w2e3r4ys

listen WEB_PORT_80
    bind 172.30.1.102:80
    mode http
    #基於user參數來調度
    balance url_param user
    server web01 172.30.1.106:80 check inter 3000 fall 3 rise 5
    server web02 172.30.1.107:80 check inter 3000 fall 3 rise 5
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg           #使用balance指定url_param調度算法,需要指定一個參數名稱喲(別忘記重啟haproxy)
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app02/index.html;done    #我們在url不傳參數的話默認調度類似於輪詢。
172.30.1.106 app02
172.30.1.107 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.107 app02
172.30.1.106 app02
172.30.1.107 app02
172.30.1.106 app02
172.30.1.107 app02
172.30.1.107 app02
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app02/index.html;done    #我們在url不傳參數的話默認調度類似於輪詢。
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app02/index.html?user=yinzhengjie;done    #我們傳參數后調度到同一台服務器了
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
172.30.1.106 app02
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app02/index.html?user=yinzhengjie;done  #我們傳參數后調度到同一台服務器了
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app02/index.html?user=jason;done      #我們傳遞的參數不一致發現可能會調度到不同的節點
172.30.1.107 app02
172.30.1.107 app02
172.30.1.107 app02
172.30.1.107 app02
172.30.1.107 app02
172.30.1.107 app02
172.30.1.107 app02
172.30.1.107 app02
172.30.1.107 app02
172.30.1.107 app02
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app02/index.html?user=jason;done      #我們傳遞的參數不一致發現可能會調度到不同的節點
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app02/index.html?name=jason;done      #我們不適用服務端定義的user傳參時發現並沒有效果。
172.30.1.106 app02
172.30.1.106 app02
172.30.1.107 app02
172.30.1.106 app02
172.30.1.107 app02
172.30.1.106 app02
172.30.1.107 app02
172.30.1.107 app02
172.30.1.106 app02
172.30.1.107 app02
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# for _ in `seq 10`;do curl http://node102.yinzhengjie.org.cn/app02/index.html?name=jason;done      #我們不適用服務端定義的user傳參時發現並沒有效果。可以看到調度的現象很類似隨機性的
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web01" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web02" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "set weight WEB_PORT_80/web01 3" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock     #很顯然,url_param調度算法也是靜態的,並不支持動態修改權重。
Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# echo "get weight WEB_PORT_80/web01" | socat stdio /yinzhengjie/softwares/haproxy/haproxy.sock 
1 (initial 1)

[root@node102.yinzhengjie.org.cn ~]# 

 

七.hdr調度算法(生產環境使用較少)

  hdr(<name>):
    針對用戶每個http頭部(header)請求中的指定信息做hash,此處由<name>指定的http首部將會被取出並做hash計算,然后由服務器總權重相除以后派發至某挑出的服務器,假如無有效的值,則會被輪詢調度。
    和source調度算法類似,hdr調度算法也支持取模法和一致性哈希,配置方式也一樣,默認都是取模法,如下所示:
      map-based:
        取模法,基於服務器權重的hash數組取模,該hash是靜態的即不支持在線調整權重,不支持慢啟動,其對后端服務器調度均衡,缺點是當服務器的總權重發生變化時,即有服務器上線或下線,都會因權重發生變化而導致調度結果整體改變。
      consistent:
        一致性哈希,該hash是動態的,支持在線調整權重,支持慢啟動,優點在於當服務器的總權重發生變化時,對調度結果影響是局部的,不會引起大的變動。

1>.實戰案例

[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg 
global
maxconn 100000
chroot /yinzhengjie/softwares/haproxy
#如果需要使用動態調度算法需要將socket功能打開
stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
nbthread 2
pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
log 127.0.0.1 local5 info

defaults
option http-keep-alive
option  forwardfor
option redispatch
option abortonclose
maxconn 100000
mode http
timeout connect 300000ms
timeout client  300000ms
timeout server  300000ms

listen stats
 mode http
 bind 0.0.0.0:9999
 stats enable
 log global
 stats uri     /haproxy-status
 stats auth    haadmin:q1w2e3r4ys

listen WEB_PORT_80
    bind 172.30.1.102:80
    mode http
    #基於流覽器客戶端來調度
    balance hdr(User-Agent)
    hash-type consistent
    server web01 172.30.1.106:80 check inter 3000 fall 3 rise 5
    server web02 172.30.1.107:80 check inter 3000 fall 3 rise 5
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg        #指定基於客戶端訪問的瀏覽器類型來進行調度
[root@node105.yinzhengjie.org.cn ~]# curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36' http://node102.yinzhengjie.org.cn/
<h1>node106.yinzhengjie.org.cn</h1>
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0'  http://node102.yinzhengjie.org.cn/
<h1>node106.yinzhengjie.org.cn</h1>
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# curl -A 'UCWEB7.0.2.37/28/999'  http://node102.yinzhengjie.org.cn/    #這是模擬的UC瀏覽器
<h2>node107.yinzhengjie.org.cn</h1>
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# curl  http://node102.yinzhengjie.org.cn/                    #這里就沒有使用任何瀏覽器模擬,而是直接使用curl命令工具訪問。
<h1>node106.yinzhengjie.org.cn</h1>
[root@node105.yinzhengjie.org.cn ~]# 

 

八.rdp-cookie調度算法(生產環境使用較少)

rdp-cookie:
  對遠程桌面的負載,使用cookie保持會話
  和source調度算法類似,rdp-cookie調度算法也支持取模法和一致性哈希,配置方式也一樣,默認都是取模法,如下所示:
    map-based:
      取模法,基於服務器權重的hash數組取模,該hash是靜態的即不支持在線調整權重,不支持慢啟動,其對后端服務器調度均衡,缺點是當服務器的總權重發生變化時,即有服務器上線或下線,都會因權重發生變化而導致調度結果整體改變,hash(o)modn 。
    consistent:
      一致性哈希,該hash是動態的,支持在線調整權重,支持慢啟動,優點在於當服務器的總權重發生變化時,對調度結果影響是局部的,不會引起大的變動。

1>.修改haproxy支持window遠程桌面功能

[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg 
global
maxconn 100000
chroot /yinzhengjie/softwares/haproxy
#如果需要使用動態調度算法需要將socket功能打開
stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
nbthread 2
pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
log 127.0.0.1 local5 info

defaults
option http-keep-alive
option  forwardfor
option redispatch
option abortonclose
maxconn 100000
mode http
timeout connect 300000ms
timeout client  300000ms
timeout server  300000ms

listen stats
 mode http
 bind 0.0.0.0:9999
 stats enable
 log global
 stats uri     /haproxy-status
 stats auth    haadmin:q1w2e3r4ys

listen WEB_PORT_80
   #注意,Windows的默認遠程端口是3389喲,咱們haproxy監聽端口也需要跟着變化,否則客戶端還得顯式輸入端口才能訪問。 bind 172.30.1.102:3389 #注意,如果代理的是windows遠程桌面就不要使用http啦,一定要改為tcp模式 mode tcp #基於流覽器客戶端來調度 balance rdp-cookie hash-type consistent #注意,windows默認的遠程端口是3389(除非你自己手動修改過window遠程服務的默認端口),千萬別寫錯了哈 server windows 172.30.1.254:3389 check inter 3000 fall 3 rise 5 #server web01 172.30.1.106: check inter 3000 fall 3 rise 5 #server web02 172.30.1.107:80 check inter 3000 fall 3 rise 5 [root@node102.yinzhengjie.org.cn ~]# [root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy [root@node102.yinzhengjie.org.cn ~]#

2>.確保window是開啟了遠程桌面的功能,如下圖所示。

3>.使用宿主機遠程haproxy服務器的3389服務,會彈出一個連接窗口,登錄成功則會訪問通過訪問haproxy的端口從而遠程了目標window主機喲,如下圖所示。

 

九.算法總結

動態調度算法(tcp/http):
  roundrobin  
  leastconn 
靜態調度算法(tcp/http)
  static-rr 
  first 

若指定hash-type的值為一致性哈希算法("consistent"),即動態調度算法,若不指定hash-type的值,則默認為取模法("map-based"),即靜態調度算法。
  source(支持tcp/http)
  uri(支持http)
  url_param(支持http) 
  hdr(支持http)
  rdp-cookie(支持tcp)

如何選擇haproxy工作在哪一層呢?
  haproxy工作在第四層性能明顯要高於haproxy工作在第七層,因為工作在第四層只涉及到數據報文的轉發問題;而工作在第七層則需要對客戶端的數據報文進行解封裝操作。
  關於HAProxy的四層與七層的區別博主推薦閱讀:https://www.cnblogs.com/yinzhengjie/p/12127959.html.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM