HAProxy调度算法 (static-rr、first)
HAProxy通过固定参数 balance 指明对后端服务器的调度算法,该参数可以配置在listen或backend选项中. HAProxy的调度算法分为静态和动态调度算法,但是有些算法可以根据参数在静态和动态算法中相互转换.
官方文档:http://cbonte.github.io/haproxy-dconv/2.4/configuration.html#4-balance
静态算法
静态算法:按照事先定义好的规则轮询进行调度,不关心后端服务器的当前负载,连接数和响应速度 等,且无法实时动态修改权重(只能为0和1,不支持其它值)或者修改后不生效,如果需要修改只能靠重启 HAProxy生效.
Socat工具(不重启后端服务器的情况下,用命令行直接把后端服务器的权重做修改)
范例:利用socat对服务器动态权重调整
安装socat
[root@ubuntu2004 ~]#apt install socat
查看haproxy支持的socat命令
[root@ubuntu2004 ~]#echo "help" | socat stdio /var/lib/haproxy/haproxy.sock
可以用于实现Zabbix监控
[root@ubuntu2004 ~]#echo "show info" | socat stdio /var/lib/haproxy/haproxy.sock
获取当前连接数
[root@ubuntu2004 ~]#echo "show info" | socat stdio /var/lib/haproxy/haproxy.sock | awk '/CurrConns/{print $2}'
查看服务器状态
[root@ubuntu2004 ~]#echo "show servers state" | socat stdio /var/lib/haproxy/haproxy.sock
查看权重
[root@ubuntu2004 ~]#echo "get weight WEB_PORT_80/web2" | socat stdio /var/lib/haproxy/haproxy.sock
1 (initial 1)
修改权重
[root@ubuntu2004 ~]#echo "set weight WEB_PORT_80/web2 2" | socat stdio /var/lib/haproxy/haproxy.sock
将后端服务器禁用,注意只针对单进程有效
[root@ubuntu2004 ~]#echo "disable server WEB_PORT_80/web2" | socat stdio /var/lib/haproxy/haproxy.sock
新写法
[root@ubuntu2004 ~]#echo "set server www.wang.org_nginx/web1 state maint" | socat stdio /var/lib/haproxy/haproxy.sock
将后端服务器启用
[root@ubuntu2004 ~]#echo "enable server WEB_PORT_80/web2" | socat stdio /var/lib/haproxy/haproxy.sock
新写法
[root@ubuntu2004 ~]#echo "set server www.wang.org_nginx/web1 state ready" | socat stdio /var/lib/haproxy/haproxy.sock
将后端服务器软下线,即weight设为0
[root@ubuntu2004 ~]#echo "set weight WEB_PORT_80/web2 0" | socat stdio /var/lib/haproxy/haproxy.sock
上线和下线后端服务器脚本 、容器的上线和下线脚本
static-rr 算法
基于权重的轮询调度,不支持运行时利用socat进行权重的动态调整(只支持0和1,不支持其它值)及后端服务器慢启动,其后端主机数量没有限制,相当于LVS中的 wrr
例如:
listen WEB_PORT_80
balance static-rr
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 2
server web2 10.0.0.102:80 weight 1
[root@ubuntu2004 ~]#systemctl restart haproxy.service
访问
[root@ubuntu2004 ~]#curl www.wang.org
web1.meng.org 10.0.0.101
[root@ubuntu2004 ~]#curl www.wang.org
web1.meng.org 10.0.0.101
[root@ubuntu2004 ~]#curl www.wang.org
web2.meng.org 10.0.0.102
first算法
根据服务器在列表中的位置,自上而下进行调度,但是其只会当第一台服务器的连接数达到上 限,新请求才会分配给下一台服务,因此会忽略服务器的权重设置,此方式使用较少
不支持用socat进行动态修改权重,可以设置0和1,可以设置其它值但无效
例如:
listen WEB_PORT_80
balance first
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 2 maxconn 2 #101限制连接两个,多的去连接102
server web2 10.0.0.102:80 weight 1
[root@ubuntu2004 ~]#systemctl restart haproxy.service
测试结果
在101上面放置一个文件
[root@ubuntu2004 ~]#dd if=/dev/zero of=/var/www/html/f1.img bs=1M count=10
记录了10+0 的读入
记录了10+0 的写出
10485760字节(10 MB,10 MiB)已复制,0.00739726 s,1.4 GB/s
用访问机器192.168.10.123限速访问,开启两个页面同时执行以下命令
[root@ubuntu2004 ~]#wget --limit-rate 1024 www.meng.org/f1.img
然后再开个页面192.168.10.123访问后端服务器
[root@ubuntu2004 ~]#curl www.meng.org
web2.meng.org 10.0.0.102
[root@ubuntu2004 ~]#curl www.meng.org
web2.meng.org 10.0.0.102
结果是会一直调度到102机器上
如果断开一个,再去curl,会访问到101上面
动态算法
基于后端服务器状态进行调度适当调整,新请求将优先调度至当前负载较低的服务器,且权 重可以在haproxy运行时动态调整无需重启.
roundrobin 算法
基于权重的轮询动态调度算法,支持权重的运行时调整,不同于lvs中的rr轮训模式, HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),其每个后端backend中最多支持4095个real server,支持对real server权重动态调整,roundrobin为默认调度算法,此算法使用广泛
[root@ubuntu2004 ~]#vim /etc/haproxy/conf.d/test.cfg
listen WEB_PORT_80
balance roundrobin
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 1
server web2 10.0.0.102:80 weight 1
[root@ubuntu2004 ~]#systemctl restart haproxy.service
socat改权重
[root@ubuntu2004 ~]#echo "set weight WEB_PORT_80/web2 2" | socat stdio /var/lib/haproxy/haproxy.sock
[root@ubuntu2004 ~]#echo "get weight WEB_PORT_80/web2" | socat stdio /var/lib/haproxy/haproxy.sock
2 (initial 1)
查看效果,去192.168.10.123访问
[root@ubuntu2004 ~]#curl www.wang.org
web2.meng.org 10.0.0.102
[root@ubuntu2004 ~]#curl www.wang.org
web2.meng.org 10.0.0.102
[root@ubuntu2004 ~]#curl www.wang.org
web1.meng.org 10.0.0.101
leastconn 算法
leastconn 加权的最少连接的动态,支持权重的运行时调整和慢启动,即根据当前连接最少的后端服务器而非权重进行优先调度(新客户端连接),比较适合长连接的场景使用,比如:MySQL等场景.相当于lvs中的WLC算法
[root@ubuntu2004 ~]#vim /etc/haproxy/conf.d/test.cfg
listen WEB_PORT_80
balance leastconn
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 1
server web2 10.0.0.102:80 weight 1
[root@ubuntu2004 ~]#systemctl restart haproxy.service
查看效果,去192.168.10.123访问
[root@ubuntu2004 ~]#curl www.wang.org
web2.meng.org 10.0.0.102
[root@ubuntu2004 ~]#curl www.wang.org
web1.meng.org 10.0.0.101
socat改权重
[root@ubuntu2004 ~]#echo "set weight WEB_PORT_80/web2 3" | socat stdio /var/lib/haproxy/haproxy.sock
[root@ubuntu2004 ~]#echo "get weight WEB_PORT_80/web2" | socat stdio /var/lib/haproxy/haproxy.sock
3 (initial 1)
random 算法(随机算法)
在1.9版本开始增加 random的负载平衡算法,其基于随机数作为一致性hash的key,随机负载平衡对于大型服务器场或经常添加或删除服务器非常有用,支持weight的动态调整,weight较大的主机有更大概 率获取新请求
[root@ubuntu2004 ~]#vim /etc/haproxy/conf.d/test.cfg
listen WEB_PORT_80
balance random
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 1
server web2 10.0.0.102:80 weight 1
[root@ubuntu2004 ~]#systemctl restart haproxy.service
socat改权重
[root@ubuntu2004 ~]#echo "set weight WEB_PORT_80/web2 4" | socat stdio /var/lib/haproxy/haproxy.sock
[root@ubuntu2004 ~]#echo "get weight WEB_PORT_80/web2" | socat stdio /var/lib/haproxy/haproxy.sock
4 (initial 1)
其他算法(即可作为静态算法,又可以哦那通过选项成为动态算法)
source 算法
源地址hash,基于用户源地址hash并将请求转发到后端服务器,后续同一个源地址请求将被转发至同一 个后端web服务器.此方式当后端服务器数据量发生变化时,会导致很多用户的请求转发至新的后端服务器,默认为静态方式,但是可以通过hash-type选项进行更改
这个算法一般是在不插入Cookie的TCP模式下使用,也可给不支持会话cookie的客户提供会话粘性,适用于需要session会话保持但不支持cookie和缓存的场景 源地址有两种转发客户端请求到后端服务器的服务器选取计算方式,分别是取模法和一致性hash
map-base 取膜法
map-based:取模法,对source地址进行hash计算,再基于服务器总权重的取模,最终结果决定将此 请求转发至对应的后端服务器.此方法是静态的,即不支持在线调整权重,不支持慢启动,可实现对后 端服务器均衡调度.缺点是当服务器的总权重发生变化时,即有服务器上线或下线,都会因总权重发生 变化而导致调度结果整体改变,hash-type 指定的默认值为此算法
所谓取模运算,就是计算两个数相除之后的余数,10%7=3, 7%4=3
map-based算法:基于权重取模,hash(source_ip)%所有后端服务器相加的总权重
取膜法配置案例
(静态算法,根据客户端源地址调度,不持支动态调整权重,无法用socat改权重,只能动态上线下线)
[root@ubuntu2004 ~]#vim /etc/haproxy/conf.d/test.cfg
listen WEB_PORT_80
balance source
hash-type map-based
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 1
server web2 10.0.0.102:80 weight 1
[root@ubuntu2004 ~]#systemctl restart haproxy.service
一致性hash法
一致性哈希,当服务器的总权重发生变化时,对调度结果影响是局部的,不会引起大的变动, hash(o)mod n ,该hash算法是动态的,支持使用 socat等工具进行在线权重调整,支持慢启动
算法:
1、key1=hash(source_ip)%(2^32)[0——4294967295]
2、keyA=hash(后端服务器虚拟ip)%(2^32)
3、将key1和keyA都放在hash环上,将用户请求调度到离key1最近的keyA对应的后端服务器
一致性hash配置示例(动态算法)
[root@ubuntu2004 ~]#vim /etc/haproxy/conf.d/test.cfg
listen WEB_PORT_80
balance source
hash-type consistent
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 1
server web2 10.0.0.102:80 weight 1
[root@ubuntu2004 ~]#systemctl restart haproxy.service
uri 算法
基于对用户请求的URI的左半部分或整个uri做hash,再将hash结果对总权重进行取模后,根据最终结果 将请求转发到后端指定服务器,适用于后端是缓存服务器场景,默认是静态算法,也可以通过hash-type 指定map-based和consistent,来定义使用取模法还是一致性hash.
注意:此算法基于应用层,所以只支持 mode http ,不支持 mode tcp
uri 取模法配置示例(静态算法)
[root@ubuntu2004 ~]#vim /etc/haproxy/conf.d/test.cfg
listen WEB_PORT_80
mode http #可省略,默认为http
balance uri
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 1
server web2 10.0.0.102:80 weight 1
[root@ubuntu2004 ~]#systemctl restart haproxy.service
uri 一致性hash配置示例(动态算法)
[root@ubuntu2004 ~]#vim /etc/haproxy/conf.d/test.cfg
listen WEB_PORT_80
mode http #可省略,默认为http
balance uri
hash-type consistent
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 1
server web2 10.0.0.102:80 weight 1
[root@ubuntu2004 ~]#systemctl restart haproxy.service
测试:在10.0.0.101和102 机器上做10个页面
101机器
[root@ubuntu2004 html]#for i in {1..10};do echo test$i.html on 10.0.0.101 > test$i.html;done
[root@ubuntu2004 html]#ls
f1.img test10.html test3.html test6.html test9.html
index.html test1.html test4.html test7.html
index.nginx-debian.html test2.html test5.html test8.html
102机器
[root@ubuntu2004 html]#for i in {1..10};do echo test$i.html on 10.0.0.102 > test$i.html;done
[root@ubuntu2004 html]#ls
index.html test10.html test2.html test4.html test6.html test8.html
index.nginx-debian.html test1.html test3.html test5.html test7.html test9.html
去192.168.10.123上访问,访问不同的uri,确认可以将用户同样的请求转发至相同的服务器
[root@ubuntu2004 ~]#curl www.wang.org/test1.html
test1.html on 10.0.0.102
[root@ubuntu2004 ~]#curl www.wang.org/test2.html
test2.html on 10.0.0.101
url_param 算法(根据值不同,访问不同的主机)
url_param对用户请求的url中的 params 部分中的一个参数key对应的value值作hash计算,并由服务器 总权重相除以后派发至某挑出的服务器;通常用于追踪用户,以确保来自同一个用户的请求始终发往同 一个real server,如果无没key,将按roundrobin算法
url_param取模法配置示例(静态算法)
[root@ubuntu2004 ~]#vim /etc/haproxy/conf.d/test.cfg
listen WEB_PORT_80
balance url_param userid
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 1
server web2 10.0.0.102:80 weight 1
[root@ubuntu2004 ~]#systemctl restart haproxy.service
url_param一致性hash配置示例(动态算法)
[root@ubuntu2004 ~]#vim /etc/haproxy/conf.d/test.cfg
listen WEB_PORT_80
balance url_param userid
hash-type consistent
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 1
server web2 10.0.0.102:80 weight 1
[root@ubuntu2004 ~]#systemctl restart haproxy.service
访问测试
[root@ubuntu2004 ~]#curl www.wang.org/index.html?userid=111
web2.meng.org 10.0.0.102
[root@ubuntu2004 ~]#curl www.wang.org/index.html?userid=222
web1.meng.org 10.0.0.101
hdr算法
针对用户每个http头部(header)请求中的指定信息做hash,此处由 name 指定的http首部将会被取出并 做hash计算,然后由服务器总权重取模以后派发至某挑出的服务器,如果无有效值,则会使用默认的轮询调度.
只支持http
hdr取模法配置示例 (针对不同的浏览器进行调度,静态算法)
root@ubuntu2004 ~]#vim /etc/haproxy/conf.d/test.cfg
listen WEB_PORT_80
balance hdr(User-Agent)
#balance hdr(host)
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 1
server web2 10.0.0.102:80 weight 1
[root@ubuntu2004 ~]#systemctl restart haproxy.service
一致性hash配置示例(针对不同的浏览器进行调度,动态算法)
root@ubuntu2004 ~]#vim /etc/haproxy/conf.d/test.cfg
listen WEB_PORT_80
balance hdr(User-Agent)
#balance hdr(host)
hash-type consistent
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 1
server web2 10.0.0.102:80 weight 1
[root@ubuntu2004 ~]#systemctl restart haproxy.service
测试访问,所指定冒充浏览器版本
[root@ubuntu2004 ~]#curl -AIE http://www.meng.org/index.html
web1.meng.org 10.0.0.101
[root@ubuntu2004 ~]#curl -Achrome http://www.meng.org/index.html
web2.meng.org 10.0.0.102
[root@ubuntu2004 ~]#curl -Afirefox http://www.meng.org/index.html -v
web1.meng.org 10.0.0.101
rdp-cookie 算法
rdp-cookie对远windows远程桌面的负载,使用cookie保持会话,默认是静态,也可以通过hash-type 指定map-based和consistent,来定义使用取模法还是一致性hash
rdp-cookie 取模法配置示例
listen RDP
bind 10.0.0.7:3389
balance rdp-cookie
mode tcp
server rdp0 10.0.0.17:3389 check fall 3 rise 5 inter 2000 weight 1
rdp-cookie一致性hash配置示例
listen RDP
bind 10.0.0.7:3389
balance rdp-cookie
hash-type consistend
mode tcp
server rdp0 10.0.0.17:3389 check fall 3 rise 5 inter 2000 weight 1
不加consistend就是静态算法,加上consistend就是动态算法
算法总结
#静态
static-rr--------->tcp/http
first------------->tcp/http
#动态 roundrobin-------->tcp/http
leastconn--------->tcp/http
random------------>tcp/http
#以下静态和动态取决于hash_type是否consistent
source------------>tcp/http
Uri--------------->http
url_param--------->http
hdr--------------->http
rdp-cookie-------->tcp #
各种算法使用场景 first #使用较少
static-rr #做了session共享的 web 集群
roundrobin
random
leastconn #数据库
source #基于客户端公网 IP 的会话保持
Uri--------------->http #缓存服务器,CDN服务商,蓝汛、百度、阿里云、腾讯
url_param--------->http #可以实现session保持
hdr #基于客户端请求报文头部做下一步处理
rdp-cookie #基于Windows主机,很少使用