HAProxy的调度算法

HAProxy的调度算法

HAProxy调度算法分为静态调度算法和动态调度算法。
静态算法是按照事先定义好的调度规则轮询公平调度,不关心后端服务器的当前负载、连接数和响应速度等,并且无法实时修改权重,只能重启后生效。
静态调度算法有:static-rr,first
动态算法是根据后端服务器状态进行调度适当调整,比如优先调度至当前负载较低的服务器,并且权重可以在haproxy运行时调整,无需重启服务
动态调度算法有:roundrobin,leastconn,source,uri,url_param,hdr,rdp-cookie
定义方法:

balance <调度算法>

静态调度算法

static-rr

静态轮询,基于权重轮询调度,不支持HAProxy在运行时进行权重调整和后端服务器的慢启动,后端的主机数量没有限制

listen web_http
        mode http
        balance static-rr       #设定静态轮询
        bind 192.168.27.21:80
        server web1 192.168.27.31:80 weight 2  check  inter 3s fall 3 rise 5
        server web2 192.168.27.32:80 weight 1  check  inter 3s fall 3 rise 5

测试:

[root@localhost ~]# curl 192.168.27.21
server 1
[root@localhost ~]# curl 192.168.27.21
server 1
[root@localhost ~]# curl 192.168.27.21
server 2

first

HAProxy会根据服务器在列表中的位置,自上而下进行调度,只有当第一台服务器的连接数达到上限时,新的请求才会被分配到下一台服务器,此设置会忽略服务器的权重
示例:

listen web_http
        mode http
        balance first
        bind 192.168.27.21:80
        server web1 192.168.27.31:80 maxconn 10  check  inter 3s fall 3 rise 5          #配置连接上线位为10,不设置权重
        server web2 192.168.27.32:80 weight 2  check  inter 3s fall 3 rise 5

测试:

[root@localhost ~]# curl 192.168.27.21
server 1
[root@localhost ~]# curl 192.168.27.21
server 1
[root@localhost ~]# curl 192.168.27.21
server 1
[root@localhost ~]# curl 192.168.27.21
server 1
[root@localhost ~]# curl 192.168.27.21
server 1
[root@localhost ~]# curl 192.168.27.21
server 1

所有的请求依旧会发往server1,server2的权重设置无效,只有当server1的连接数占满时才将请求发送给server2


动态调度算法

roundrobin

基于权重的轮询动态调度算法,支持权重的运行时调整,支持慢启动,每个后端backend中最多支持4095个server,此为默认调度算法。
示例:
1.修改配置文件

stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin    #将socket文件开启,在进行动态调节权重时,是通过socket进行的
listen web_http
        mode http
        balance roundrobin      #调度算法设置为roundrobin
        bind 192.168.27.21:80
        server web1 192.168.27.31:80 weight 1  check  inter 3s fall 3 rise 5
        server web2 192.168.27.32:80 weight 1  check  inter 3s fall 3 rise 5

2.创建socket文件的目录

[root@localhost ~]# mkdir /var/lib/haproxy

3.重启服务器,查看socket文件是否启用

[root@localhost ~]# ls /var/lib/haproxy/
haproxy.sock                #已经存在

测试:
动态调节服务器状态时需要使用到socat命令,先安装此命令

[root@localhost ~]# yum install socat -y

socat的简单使用,可以通过echo+指令然后使用管道传送给socket文件

[root@localhost ~]# echo "help" | socat stdio /var/lib/haproxy/haproxy.sock
#此命令可以用来查看socat一些简单的使用帮助

1.获取服务器权重

[root@localhost ~]# echo "get weight web_http/web1" | socat stdio /var/lib/haproxy/haproxy.sock      #获取权重时,指定后端服务器组和服务器名称
1 (initial 1)       权重为1

2.动态修改权重,然后再次查看

[root@localhost ~]# echo "set weight web_http/web1 2" | socat stdio /var/lib/haproxy/haproxy.sock       #使用set来修改权重,

[root@localhost ~]# echo "get weight web_http/web1" | socat stdio /var/lib/haproxy/haproxy.sock 
2 (initial 1)

测试访问

[root@localhost ~]# curl 192.168.27.21
server 1
[root@localhost ~]# curl 192.168.27.21
server 1
[root@localhost ~]# curl 192.168.27.21
server 2
#权重已经被调整,需要注意动态设置的方法临时有效,重启服务将失效

将服务器动态下线

[root@localhost ~]# echo "disable server web_http/web1" | socat stdio /var/lib/haproxy/haproxy.sock 

测试访问

[root@localhost ~]# curl 192.168.27.21
server 2
[root@localhost ~]# curl 192.168.27.21
server 2
#server1被下线,访问只能被调度到server2

将服务器动态上线

[root@localhost ~]# echo "enable server web_http/web1" | socat stdio /var/lib/haproxy/haproxy.sock 

测试访问

[root@localhost ~]# curl 192.168.27.21
server 1
[root@localhost ~]# curl 192.168.27.21
server 1
[root@localhost ~]# curl 192.168.27.21
server 2
#server1又能正常访问,并且之前设置的权重依旧有效

source

source调度算法是基于用户的源地址hash并将请求转发到后端服务器,默认为静态即取模方式(可以通过hash-type支持的选项进行更改),后续同一个地址发来的请求将都被转发为后端的同一个web服务器,比较适合用于session保持/缓存业务等场景
示例:

listen web_http
        mode http
        balance source
        bind 192.168.27.21:80
        server web1 192.168.27.31:80  check  inter 3s fall 3 rise 5
        server web2 192.168.27.32:80  check  inter 3s fall 3 rise 5

测试

[root@localhost ~]# curl 192.168.27.21
server 1
[root@localhost ~]# curl 192.168.27.21
server 1
[root@localhost ~]# curl 192.168.27.21
server 1
#当第一次访问被调度到后天的server1后之后的所有请求都被调度到server1

调度算法为源地址哈希是是无法进行权重的修改的,默认为静态

[root@localhost ~]# echo "set weight web_http/web1 2" | socat stdio /var/lib/haproxy/haproxy.sock 
Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.
#无法进行对权重进行设置,只能开启或关闭

source 一致性hash算法

源地址哈希由于是静态的,如果后端的服务器发生改变,取模后的值也将发生改变,以至于整张表上的记录作废,从而重新进行计算。
一致性哈希是动态的,支持在线的权重调整,当服务器的总权重发生变化时,影响的只是一部分,不会引起大的变动
示例:

listen web_http
        mode http
        balance source
        hash-type consistent        #使用hash-type指定一致性哈希
        bind 192.168.27.21:80
        server web1 192.168.27.31:80  check  inter 3s fall 3 rise 5
        server web2 192.168.27.32:80  check  inter 3s fall 3 rise 5

uri

uri哈希算法,可以通过用户访问的uri来进行调度,常用于缓存服务器。当用户第一访问某uri是,haproxy会从后台选出一台服务进行调度并记录,当再其他用户也访问此uri时,haproxy会固定的将其调度到后台上次所请求此uri的那台服务器上。
示例:
将配置文件改为基于uri哈希的算法

listen web_http
        mode http
        balance uri         #设置为uri算法
        bind 192.168.27.21:80
        server web1 192.168.27.31:80 weight 1 check  inter 3s fall 3 rise 5
        server web2 192.168.27.32:80 weight 3 check  inter 3s fall 3 rise 5

测试
分别使用不同的客户端对相同的资源进行访问
使用client1进行访问

[root@client1 ~]# curl 192.168.27.21/index.html
server 1

使用client2进行访问

[root@client2 ~]# curl 192.168.27.21/index.html
server 1

使用client3进行访问

[root@client3 ~]# curl 192.168.27.21/index.html
server 1

结论:不同用户访问同一个资源将会调度到后台的统一个服务器上


url_param

url_param的调度算法是对用户请求的url中{params}部分中的参数的值做哈希计算,并由服务器总权重相除后派发至选中的后端服务器;常用于追踪用户确保同一个用户的请求永远发向同一台后端服务器。
假设url为http://www.mylinuxops.com/index.php?name=wang
其中url_param为?之后的内容即name=wang,而其中name为参数,wang为值
示例:

listen web_http
        mode http
        balance url_param name      #设定调度算法为 url_param,参数为name
        bind 192.168.27.21:80
        server web1 192.168.27.31:80 weight 2 check  inter 3s fall 3 rise 5
        server web2 192.168.27.32:80 weight 1 check  inter 3s fall 3 rise 5

测试
当用户所带的参数的值相同时则请求后台相同的服务器

[root@localhost ~]# curl 192.168.27.21/index.html?name=123
server 2
[root@localhost ~]# curl 192.168.27.21/index.html?name=123
server 2

当用户使用未设定的参数时,将轮询

[root@localhost ~]# curl 192.168.27.21/index.html?aaa=123
server 1
[root@localhost ~]# curl 192.168.27.21/index.html?aaa=123
server 1
[root@localhost ~]# curl 192.168.27.21/index.html?aaa=123
server 2

hdr(NAME)

hdr调度算法是针对用户每个http请求头部中指定的信息做hash,然后由服务器的总权重相除取模后派发至后端的服务器,如果没有有效的值则将进行轮询调度。hdr可以使用的name有(cookie、User-agent、host)
示例:

listen web_http
        mode http                       #此算法是针对http的所以模式需要使用http
        balance hdr(user-agent)         #使用hdr算法,以用户的客户端作调度
        bind 192.168.27.21:80
        server web1 192.168.27.31:80 weight 2 check  inter 3s fall 3 rise 5
        server web2 192.168.27.32:80 weight 1 check  inter 3s fall 3 rise 5

示例:
使用curl访问时

[root@localhost ~]# curl 192.168.27.21
server 2
[root@localhost ~]# curl 192.168.27.21
server 2
[root@localhost ~]# curl 192.168.27.21
server 2
[root@localhost ~]# curl 192.168.27.21
server 2

使用curl模拟其他客户端访问

[root@localhost ~]# curl -A "12" 192.168.27.21
server 1
[root@localhost ~]# curl -A "12" 192.168.27.21
server 1
[root@localhost ~]# curl -A "12" 192.168.27.21

rdp-cookie

rdp-cookie调度算法是针对远程桌面的负载,使用cookie保持会话,用在远程为windows的情况下。
示例:

listen RDP
        bind 192.168.27.21:3389
        balance rdp-cookie      #修改调度算法为rdp-cookie
        mod tcp                 #由于后端为windows所以此处必须为tcp
        server rdp1 192.168.27.40:3389 check  inter 3s fall 3 rise 5
        server rdp1 192.168.27.40:3389 check  inter 3s fall 3 rise 5

猜你喜欢

转载自blog.51cto.com/11886307/2405070