HAProxy调度算法
HAProxy通过固定参数balance指明对后端服务器的调度算法,该参数可以配置在listen或backend选项中。
HAProxy的调度算法分为静态和动态调度算法,但是有些算法可以根据参数在静态和动态算法中相互转换。
一、静态算法
静态算法:按照事先定义好的规则轮询公平调度,不关心后端服务器的当前负载、链接数和相应速度等,且无法实时修改权重,只能靠重启HAProxy生效。
服务器动态权重调整
root@z1:~# echo "show info" | socat stdio /var/lib/haproxy/haproxy.sock
Name: HAProxy
Version: 2.0.13
Release_date: 2020/02/13
Nbthread: 2
Nbproc: 1
Process_num: 1
root@z1:~# echo "get weight web_host/web1" | socat stdio /var/lib/haproxy/haproxy.sock
1 (initial 1)
无法进行对权重进行设置,只能开启或关闭
root@z1:~# echo "set weight web_host/web1 2" | socat stdio /var/lib/haproxy/haproxy.sock
Backend is using a static LB algorithm and only accepts weights '0%' and '100%'. (无法进行对权重进行设置,只能开启或关闭)
1.1:static-rr:
static-rr:基于权重的轮询调度,不支持权重的运行时调整及后端服务器慢启动,其后端主机数量没有限制
listen web_host
bind 192.168.1.101:80,:8801-8810,192.168.1.101:9001-9010
mode http
log global
balance static-rr
server web1 192.168.1.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.1.104:80 weight 2 check inter 3000 fall 2 rise 5
测试访问效果
root@z1:~# curl 192.168.1.101:80
103 index
root@z1:~# curl 192.168.1.101:8801
104 index
root@z1:~# curl 192.168.1.101:8803
104 index
root@z1:~# curl 192.168.1.101:9009
103 index
1.2:first
first:根据服务器在列表中的位置,自上而下进行调度,但是其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务,因此会忽略服务器的权重设置。
listen web_host
bind 192.168.1.101:80,:8801-8810,192.168.1.101:9001-9010
mode http
log global
balance first
server web1 192.168.1.103:80 maxconn 2 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.1.104:80 weight 1 check inter 3000 fall 2 rise 5
二、动态算法
动态算法:基于后端服务器 状态进行调度适当调整,比如优先调度至当前负载较低的服务器,且权重可以在haproxy运行时动态调整无需重启。
2.1:roundrobin
roundrobin:基于权重的轮询动态调度算法,支持权重的运行时调整,不完全等于lvs中的rr轮训模式,HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),其每个后端backend中最多支持4095个realserver,roundrobin为默认调度算法,且支持对real server权重动态调整。
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.1.101:9001-9010
mode http
log global
balance roundrobin
server web1 192.168.1.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.1.104:80 weight 2 check inter 3000 fall 2 rise 5
动态调整权限:
注意:修改只是临时生效,永久保存得写文件
root@z1:~# echo "get weight web_host/web1" | socat stdio /var/lib/haproxy/haproxy.sock
1 (initial 1)
root@z1:~# echo "set weight web_host/web1 3" | socat stdio /var/lib/haproxy/haproxy.sock
root@z1:~# echo "get weight web_host/web1" | socat stdio /var/lib/haproxy/haproxy.sock
3 (initial 1)
2.2:leastconn
leastconn加权的最少连接的动态,支持权重的运行时调整和慢启动,即当前后端服务器连接最少的优先调度(新客户端连接),比较适合长连接的场景使用,比如MySQL等场景。
listen web_host
bind 192.168.1.101:80,:8801-8810,192.168.1.101:9001-9010
mode http
log global
balance leastconn
server web1 192.168.1.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.1.104:80 weight 1 check inter 3000 fall 2 rise 5
三、其他算法
其他部分算法即可作为静态算法,又可以通过选项成为动态算法。一致性hash 是动态算法
3.1:source
源地址hash,基于用户源地址hash并将请求转发到后端服务器,默认为静态即取模方式,但是可以通过hash-type支持的选项更改,后续同一个源地址请求将被转发至同一个后端web服务器,比较适用于session保持/缓存业务等场景。
hash具体介绍参考: https://www.cnblogs.com/jkqiang/p/11003569.html
3.1.1:map-base取模法
map-based:取模法,基于服务器总权重的hash数组取模,该hash是静态的即不支持在线调整权重,不支持慢启动,其对后端服务器调度均衡,缺点是当服务器的总权重发生变化时,即有服务器上线或下线,都会因权重发生变化而导致调度结果整体改变。
因为被除数不变(原数据得到的hash值),除数发生改变时,所有取模所得结果都得变
取模法配置示例
listen web_host
bind 192.168.1.101:80,:8801-8810,192.168.1.101:9001-9010
mode tcp
log global
balance source
hash-type consistent
server web1 192.168.1.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.1.104:80 weight 1 check inter 3000 fall 2 rise 5
3.1.2:一致性hash
一致性哈希,该hash是动态的,支持在线调整权重,支持慢启动,优点在于当服务器的总权重发生变化时,对调度结果影响是局部的,不会引起大的变动。
listen web_host
bind 192.168.1.101:80,:8801-8810,192.168.1.101:9001-9010
mode tcp
log global
balance source
hash-type consistent
server web1 192.168.1.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.1.104:80 weight 1 check inter 3000 fall 2 rise 5
3.2:uri
基于对用户请求的uri做hash并将请求转发到后端指定服务器,也可以通过map-based和consistent定义使用取模法还是一致性hash。
uri 与url 的区别主要在于是否加了协议
http://example.org/absolute/URI/with/absolute/path/to/resource.txt #URI/URL
ftp://example.org/resource.txt #URI/URL
/relative/URI/with/absolute/path/to/resource.txt #URI
3.2.1:uri 取模法配置示例
listen web_host
bind 192.168.1.101:80,:8801-8810,192.168.1.101:9001-9010
mode http
log global
balance uri
server web1 192.168.1.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.1.104:80 weight 1 check inter 3000 fall 2 rise 5
3.2.2:uri 一致性hash配置示例
listen web_host
bind 192.168.1.101:80,:8801-8810,192.168.1.101:9001-9010
mode http
log global
balance uri
hash-type consistent
server web1 192.168.1.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.1.104:80 weight 1 check inter 3000 fall 2 rise 5
访问测试
root@z1:~# curl http://192.168.1.101/index.html
104 index
root@z1:~# curl http://192.168.1.101/index1.html
103 index1
3.3:url_param
url_param对用户请求的url中的 params 部分中的参数name作hash计算,并由服务器总权重相除以后派发至某挑出的服务器;通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个real server
假设url = http://www.magedu.com/foo/bar/index.php?k1=v1&k2=v2
则:
host = "www.magedu.com"
url_param = "k1=v1&k2=v2"
3.3.1:url_param取模法配置示例
listen web_host
bind 192.168.1.101:80,:8801-8810,192.168.1.101:9001-9010
mode http
log global
balance url_param name,age #支持对单个及多个url_param 值hash
server web1 192.168.1.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.1.104:80 weight 1 check inter 3000 fall 2 rise 5
3.3.2:url_param一致性hash配置示例
listen web_host
bind 192.168.1.101:80,:8801-8810,192.168.1.101:9001-9010
mode http
log global
balance url_param name,age #支持对单个及多个url_param 值hash
hash-type consistent
server web1 192.168.1.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.1.104:80 weight 1 check inter 3000 fall 2 rise 5
测试访问
curl http://192.168.1.101/app/index.html?name=NAME #单个参数访问
curl http://192.168.1.101/app/index.html?age=AGE
curl http://192.168.1.101/app/index.html?age=AGE&&name=NAME #多个参数访问
3.4:hdr
针对用户每个http头部(header)请求中的指定信息做hash,此处由 name 指定的http首部将会被取出并做hash计算,然后由服务器总权重相除以后派发至某挑出的服务器,假如无有效的值,则会使用默认的轮询调度。
3.4.1:hdr取模法配置示例:
listen web_host
bind 192.168.1.101:80,:8801-8810,192.168.1.101:9001-9010
mode http
log global
balance hdr(User-Agent)
server web1 192.168.1.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.1.104:80 weight 1 check inter 3000 fall 2 rise 5
3.4.2:一致性hash配置示例
listen web_host
bind 192.168.1.101:80,:8801-8810,192.168.1.101:9001-9010
mode http
log global
balance hdr(User-Agent)
hash-type consistent
server web1 192.168.1.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.1.104:80 weight 1 check inter 3000 fall 2 rise 5
测试访问
不同的浏览器,结果不一样
3.5:rdp-cookie
rdp-cookie对远程桌面的负载,使用cookie保持会话
3.5.1:rdp-cookie取模法配置示例
listen RDP
bind 192.168.1.101:3389
balance rdp-cookie
mode tcp
server rdp0 172.18.132.20:3389 check fall 3 rise 5 inter 2000 weight 1
3.5.2:rdp-cookie一致性hash配置示例
listen RDP
bind 192.168.7.101:3389
balance rdp-cookie
hash-type consistent
mode tcp
server rdp0 172.18.132.20:3389 check fall 3 rise 5 inter 2000 weight 1
测试访问
3.5.3:基于iptables实现
net.ipv4.ip_forward = 1
# iptables -t nat -A PREROUTING -d 192.168.1.101 -p tcp --dport 3389 -j DNAT --to destination 172.18.139.20:3389
# iptables -t nat -A POSTROUTING -s 192.168.0.0/21 -j SNAT --to-source 192.168.1.101
3.6:random
在1.9版本开始增加一个叫做random的负载平衡算法,其基于一个随机数作为一致性hash的key,随机负载平衡对于大型服务器场或经常添加或删除服务器非常有用,因为它可以避免在这种情况下由roundrobin或leastconn导致的锤击效应
random配置实例
listen web_host
bind 192.168.1.101:80,:8801-8810,192.168.1.101:9001-9010
mode http
log global
balance random
server web1 192.168.1.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.1.104:80 weight 1 check inter 3000 fall 2 rise 5
四、算法总结
动静态分类:
static-rr--------->tcp/http 静态
first------------->tcp/http 静态
roundrobin-------->tcp/http 动态
leastconn--------->tcp/http 动态
random------------>tcp/http 动态
source------------>tcp/http
Uri--------------->http
url_param--------->http 取决于hash_type是否consistent
hdr--------------->http
rdp-cookie-------->tcp
使用场景:
first #使用较少
static-rr #做了session共享的web集群
roundrobin
random
leastconn #数据库
source #基于客户端公网IP的会话保持
Uri--------------->http #缓存服务器,CDN服务商,蓝汛、百度、阿里云、腾讯
url_param--------->http
hdr #基于客户端请求报文头部做下一步处理
rdp-cookie #很少使用