十四.haproxy的调度算法

HAProxy 常见调度算法

一、服务器动态权重调整

# Socat 是 Linux 下的一个多功能的网络工具,名字来由是Socket CAT,Socat 的主要特点就是在两个数据流之间建立通道,且支持众多协议和链接方式。如 IP、TCP、 UDP、IPv6、Socket文件等。
yum install -y socat
# 查看haproxy的信息
echo "show info" | socat stdio /var/lib/haproxy/haproxy.sock
# 设置权重
echo "get weight web_port/web01" | socat stdio /var/lib/haproxy/haproxy.sock
echo "set weight web_port/web01 2" | socat stdio /var/lib/haproxy/haproxy.sock
Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.

二、Haproxy的调度算法

1、静态算法
静态算法:按照事先定义好的规则轮询公平调度,不关心后端服务器的当前负载、链接数和相应速度等,且无法实时修改权重,只能靠重启HAProxy生效。

static-rr:基于权重的轮询调度,不支持权重的运行时调整及后端服务器慢启动,其后端主机数量没有限制。

listen web_port
bind 192.168.32.204:80
mode http
log global
balance static-rr
server web01 10.0.0.201:80 weight 1 check inter 3000 fall 2 rise 5
server web02 10.0.0.202:80 weight 2 check inter 3000 fall 2 rise 5

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

listen web_port
bind 192.168.32.204:80
mode http
log global
balance first
server web01 10.0.0.201:80 maxconn 2 weight 1 check inter 3000 fall 2 rise 5
server web02 10.0.0.202:80 weight 2 check inter 3000 fall 2 rise 5

2、动态算法
动态算法:基于后端服务器 状态进行调度适当调整,比如优先调度至当前负载较低的服务器,且权重可以在 haproxy运行时动态调整无需重启。直接reload即可。

roundrobin:基于权重的轮询动态调度算法,支持权重的运行时调整,不完全等于lvs中的rr轮训模式。

HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),其每个后端backend中最多支持4095个real server,roundrobin为默认调度算法,且支持对real server权重动态调整。

listen web_port
bind 192.168.32.204:80
mode http
log global
balance roundrobin
server web01 10.0.0.201:80 weight 1 check inter 3000 fall 2 rise 5
server web02 10.0.0.202:80 weight 2 check inter 3000 fall 2 rise 5

leastconn

leastconn加权的最少连接的动态,支持权重的运行时调整和慢启动,即当前后端服务器连接最少的优先调度(新客户端连接),比较适合长连接的场景使用,比如MySQL等场景。

listen web_port
bind 192.168.32.204:80
mode http
log global
balance leastconn
server web01 10.0.0.201:80 weight 1 check inter 3000 fall 2 rise 5
server web02 10.0.0.202:80 weight 2 check inter 3000 fall 2 rise 5

3、其它算法(可静态可动态)

其它部分算法即可作为静态算法,又可以通过选项成为动态算法。

3.1 source

源地址hash,基于用户源地址hash并将请求转发到后端服务器,默认为静态即取模方式,但是可以通过hash-type支持的选项更改,后续同一个源地址请求将被转发至同一个后端web服务器,
比较适用于session保持/缓存业务等 场景。源地址有两种转发客户端请求到后端服务器的服务器选取计算方式,分别是取模法(静态)和一致性hash(动态)。

3.1.1 map-base取模法(静态)

map-based:取模法,基于服务器总权重的hash数组取模,该hash是静态的即不支持在线调整权重,不支持慢启动,其对后端服务器调度均衡,
缺点是当服务器的总权重发生变化时,即有服务器上线或下线,都会因权重发生变化而导致调度结果整体改变。
所谓取模运算,就是计算两个数相除之后的余数,10%7=3, 7%4=3,基于权重取模:(2^32-1)%(1+1+2)

listen web_port
bind 192.168.32.204:80
mode http
log global
balance source
server web01 10.0.0.201:80 weight 1 check inter 3000 fall 2 rise 5
server web02 10.0.0.202:80 weight 2 check inter 3000 fall 2 rise 5
3.1.2 :一致性hash(动态)

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

listen web_port
bind 192.168.32.204:80
mode http
log global
balance source
hash-type consistent
server web01 10.0.0.201:80 weight 1 check inter 3000 fall 2 rise 5
server web02 10.0.0.202:80 weight 2 check inter 3000 fall 2 rise 5

3.2 uri

基于对用户请求的uri做hash并将请求转发到后端指定服务器,也可以通过map-based和consistent定义使用取模法还是一致性hash。
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_port
bind 192.168.32.204:80
mode http
log global
balance uri
server web01 10.0.0.201:80 weight 1 check inter 3000 fall 2 rise 5
server web02 10.0.0.202:80 weight 2 check inter 3000 fall 2 rise 5
3.2.2 uri一致性hash配置(动态)
listen web_port
bind 192.168.32.204:80
mode http
log global
balance uri
hash-type consistent
server web01 10.0.0.201:80 weight 1 check inter 3000 fall 2 rise 5
server web02 10.0.0.202:80 weight 1 check inter 3000 fall 2 rise 5

3.3 url_param

url_param对用户请求的url中的params部分中的参数name作hash计算,并由服务器总权重相除以后派发至某挑出的服务器;通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个real server。

假设url = http://www.kingseal.com/foo/bar/index.php?k1=v1&k2=v2
则:
host = "www.kingseal.com"
url_param = "k1=v1&k2=v2"

3.3.1 url_param取模法配置(静态)
listen web_port
bind 192.168.32.204:80
mode http
log global
balance url_param k1,k2
server web01 10.0.0.201:80 weight 1 check inter 3000 fall 2 rise 5
server web02 10.0.0.202:80 weight 1 check inter 3000 fall 2 rise 5
3.3.2 url_param一致性hash配置(动态)
listen web_port
bind 192.168.32.204:80
mode http
log global
balance url_param k1,k2
hash-type consistent
server web01 10.0.0.201:80 weight 1 check inter 3000 fall 2 rise 5
server web02 10.0.0.202:80 weight 1 check inter 3000 fall 2 rise 5

3.4 hdr

针对用户每个http头部(header)请求中的指定信息做hash,此处由name指定的http首部将会被取出并做hash计算,然后由服务器总权重相除以后派发至某挑出的服务器,
假如无有效的值,则会使用默认的轮询调度。

3.4.1 hdr取模法配置(静态)
listen web_port
bind 192.168.32.204:80
mode http
log global
balance hdr(User-Agent)
server web01 10.0.0.201:80 weight 1 check inter 3000 fall 2 rise 5
server web02 10.0.0.202:80 weight 1 check inter 3000 fall 2 rise 5
3.4.2 一致性hash配置(动态)
listen web_port
bind 192.168.32.204:80
mode http
log global
balance hdr(User-Agent)
hash-type consistent
server web01 10.0.0.201:80 weight 1 check inter 3000 fall 2 rise 5
server web02 10.0.0.202:80 weight 1 check inter 3000 fall 2 rise 5

rdp-cookie对windows远程桌面的负载,使用cookie保持会话。

3.5.1 rdp-cookie取模法配置(静态)
listen RDP
bind 192.168.32.204:3389
balance rdp-cookie
mode tcp
server rdp0 10.0.0.201:3389 check fall 3 rise 5 inter 2000 weight 1
3.5.2 rdp-cookie一致性hash配置(动态)
listen RDP
bind 192.168.32.204:3389
balance rdp-cookie
mode tcp
hash-type consistent
server rdp0 10.0.0.201:3389 check fall 3 rise 5 inter 2000 weight 1
3.5.3 可以使用iptables来实现
#必须开启ip转发功能
cat >> /etc/sysctl.conf <<EOF
net.ipv4.ip_forward = 1
EOF
sysctl -p
 
iptables -t nat -A PREROUTING -d 192.168.32.204 -p tcp --dport 3389 -j DNAT --todestination 10.0.0.201:3389
iptables -t nat -A POSTROUTING -s 192.168.0.0/16 -j SNAT --to-source 192.168.32.204

3.6 random(动态)

在1.9版本开始增加一个叫做random的负载平衡算法,其基于一个随机数作为一致性hash的key,随机负载平衡对于大型服务器场合经常添加或删除服务器非常有用,因为它可以避免在这种情况下由roundrobin或leastconn导致的锤击效应。

listen web_port
bind 192.168.32.204:80
mode http
log global
balance random
server web01 10.0.0.201:80 weight 1 check inter 3000 fall 2 rise 5
server web02 10.0.0.202:80 weight 1 check inter 3000 fall 2 rise 5

4、算法总结

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

5、算法的使用场景

first #使用较少
static-rr #做了session共享的web集群
roundrobin #默认
random
leastconn #数据库
source #基于客户端公网IP的会话保持
Uri--------------->http #缓存服务器,CDN服务商,蓝汛、百度、阿里云、腾讯
url_param--------->http
hdr #基于客户端请求报文头部做下一步处理
rdp-cookie #很少使用

posted @ 2022-07-27 10:41  老夫聊发少年狂88  阅读(74)  评论(0编辑  收藏  举报