HAProxy 算法

HAProxy通过固定参数balance指明对后端服务器的调度算法,该参数可以配置在listen或backend选项中。

HAproxy的调度算法分为静态和动态调度算法

官方文档:http://cbonte.github.io/haproxy-dconv/2.3/configuration.html#3.3

静态算法

按照事先定义好的规则轮询调度,不关系后端服务器负载,链接数和响应速度,且无法实现修改权重,只能靠重启HAProxy生效

static-rr

基于权重的轮询调度,不支持权重的运行是调整及后端服务器慢启动,其他后端主机量没有限制

listen test
    bind 192.168.248.200:80
    mode http
    log global
    balance static-rr
    server apacheRS1  192.168.248.201:80 weight 1 check inter 2s fall 3 rise 5
    server apacheRS2  192.168.248.202:80 weight 2 check inter 2s fall 3 rise 5

#测试访问
[root@LB ~]# for i in `seq 6`; do curl 192.168.248.200;done
apacheRS1
apacheRS2
apacheRS2
apacheRS1
apacheRS2
apacheRS2

 

first

根据服务器在列表中的位置,之上而下进行调度,但是其只会当第一台服务器的连接数达到上限,新的请求才会分配给下一台服务去,因此会忽略服务器权重设置。

listen test
    bind 192.168.248.200:80
    mode http
    log global
    balance first
    server apacheRS1  192.168.248.201:80 maxconn 1  check inter 2s fall 3 rise 5
    server apacheRS2  192.168.248.202:80 weight 4 check inter 2s fall 3 rise 5
    
#测试
[root@LB ~]# while true;do curl 192.168.248.200;sleep 0.1;done

 

动态算法

动态算法: 基于后端服务器状态进行调整,无需重启haproxy

服务器动态调整权重:

[root@LB ~]# yum -y install socat
[root@LB ~]# echo "help" | socat stdio /var/lib/haproxy/haproxy.sock 
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>
  add ssl crt-list <filename> <certfile> [options] : add a line <certfile> to a crt-list <filename>
  del ssl crt-list <filename> <certfile[:line]> : delete a line <certfile> in a crt-list <filename>
  show ssl crt-list [-n] [<filename>] : show the list of crt-lists or the content of a crt-list <filename>
  new ssl cert <certfile> : create a new certificate file to be used in a crt-list or a directory
  set ssl cert <certfile> <payload> : replace a certificate file
...........................

 

roundrobin

基于权重的轮询动态调度算法,支持权重运行时调整,最多支持4095个RS,为默认的调度算法

listen test
    bind 192.168.248.200:80
    log global
    mode http
    balance roundrobin
    server apacheRS1  192.168.248.201:80  weight 1  check inter 2s fall 3 rise 5
    server apacheRS2  192.168.248.202:80  weight 1 check inter 2s fall 3 rise 5

 动态调整权重

[root@LB ~]# echo "set weight test/apacheRS1 4" | socat stdio  /var/lib/haproxy/haproxy.sock 

[root@LB ~]# echo "get weight test/apacheRS1 " | socat stdio  /var/lib/haproxy/haproxy.sock 
4 (initial 1)

 

leastconn

leastconn加权的最少连接的动态,支持权重运行时调整和慢启动,当前服务器连接最少的优先调度(新连接)

listen test
    bind 192.168.248.200:80
    log global
    mode http
    balance leastconn
    server apacheRS1  192.168.248.201:80  weight 1  check inter 2s fall 3 rise 5
    server apacheRS2  192.168.248.202:80  weight 1 check inter 2s fall 3 rise 5

 

其他算法

其他部分算法既可以作为静态算法,又可以通过选项成为动态

source

源地址hash,基于用户源地址hash并将请求转发到后端服务器,默认为静态取模方式,可以通过hash-type选项更改,后续同一个源地址请求将被转发至同一个后端服务器。

两种转发计算方式:一致性hash和取模

  • map-base取模法

基于服务器总权重的hash数组取模,该hash时静态的不支持在线权重调整,不知道慢启动,其对后端服务器调度均衡,缺点是当服务器的总权重发生变化时(服务器上下线),都会因其权重发生变化而导致调度结果改变

取模运算:计算两数相除之后的余数,10%6=4,7%4=3,基于权重取模:(2^32-1)%(1+1+2)

listen test
    bind 192.168.248.200:80
    log global
    mode http
    balance source
    server apacheRS1  192.168.248.201:80  weight 1 check inter 2s fall 3 rise 5
    server apacheRS2  192.168.248.202:80  weight 1 check inter 2s fall 3 rise 5
    
#访问
[root@LB ~]# for i in `seq 6`;do curl 192.168.248.200;done
apacheRS1
apacheRS1
apacheRS1
apacheRS1
apacheRS1
apacheRS1

 

  • 一致性hash

一致性hash,该hash是动态,支持在线调整权重,支持慢启动,优点在于当服务器的总权重发生变化时,对调度的结果是局部的,不会引起大的变动,hash(o)mod n.

listen test
    bind 192.168.248.200:80
    log global
    mode http
    balance source
    hash-type consistent
    server apacheRS1  192.168.248.201:80  weight 1 check inter 2s fall 3 rise 5
    server apacheRS2  192.168.248.202:80  weight 1 check inter 2s fall 3 rise 5

 

uri

基于uri的算法会对用户请求的uri进行hash运算,根据用户访问的资源来调度,无论是哪个用户从哪个ip访问, 只要是访问同一个资源,就会被调度到同一个服务器。

url_param

url_param对用户请求的url中的params部分中的参数name或其他自定义的参数。

hdr

针对http请求报文头部中的指定字段对应的值做hash。

rdp-cookie

rdp-cookie使⽤客户端cookie保持会话,实现对windows远程桌⾯的负载等,rdp-cookie只支持tcp负载。

posted @ 2021-05-09 19:39  第七爻  阅读(179)  评论(0编辑  收藏  举报