Haproxy调度算法 - 静态算法介绍与使用

发布于:2025-08-01 ⋅ 阅读:(17) ⋅ 点赞:(0)


HAProxy通过固定参数 balance 指明对后端服务器的调度算法,该参数可以配置在listen或backend选项中。
HAProxy的调度算法分为静态和动态调度算法,但是有些算法可以根据参数在静态和动态算法中相互转换。
官方文档: http://cbonte.github.io/haproxy-dconv/2.4/configuration.html#4-balance

一、概述

按照事先定义好的规则轮询进行调度,不关心后端服务器的当前负载、连接数和响应速度等,且无法实时动态修改权重(只能为0和1,不支持其它值)或者修改后不生效,如果需要修改只能靠重启HAProxy生效

二、socat工具

对服务器动态权重和其它状态可以利用 socat工具进行调整,Socat 是 Linux 下的一个多功能的网络工具,名字来由是Socket CAT,相当于netCAT的增强版.Socat 的主要特点就是在两个数据流之间建立双向通道,且支持众多协议和链接方式。如 IP、TCP、 UDP、IPv6、Socket文件等。

例:利用工具socat对服务器动态权重调整

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

#查看帮助
[root@centos7 ~]# socat -h
[root@centos7 ~]# echo "help" | socat stdio /var/lib/haproxy/haproxy.sock
[root@centos7 ~]#echo "show info" | socat stdio /var/lib/haproxy/haproxy.sock
[root@centos7 ~]#cat /etc/haproxy/haproxy.cfg
......
listen lhl-test-80
bind :81,:82
mode http
server web1 10.0.0.17:80 check inter 3000 fall 3 rise 5
server web2 10.0.0.27:80 check weight 3
......

[root@centos7 ~]#echo "show servers state" | socat stdio /var/lib/haproxy/haproxy.sock

[root@centos7 ~]#echo "get weight lhl-test-80/web2" | socat stdio /var/lib/haproxy/haproxy.sock
3 (initial 3)

#修改weight,注意只针对单进程有效
[root@centos7 ~]#echo "set weight lhl-test-80/web2 2" | socat stdio /var/lib/haproxy/haproxy.sock
[root@centos7 ~]#echo "get weight lhl-test-80/web2" | socat stdio /var/lib/haproxy/haproxy.sock
2 (initial 3)

#将后端服务器禁用,注意只针对单进程有效
[root@centos7 ~]#echo "disable server lhl-test-80/web2" | socat stdio /var/lib/haproxy/haproxy.sock

#启用后端服务器
[root@centos7 ~]#echo "enable server lhl-test-80/web2" | socat stdio /var/lib/haproxy/haproxy.sock

#将后端服务器软下线,即weight设为0
[root@centos7 ~]#echo "set weight lhl-test-80/web1 0" | socat stdio /var/lib/haproxy/haproxy.sock

#针对haproxy的多进程,将后端服务器禁用
[root@centos7 ~]#vim /etc/haproxy/haproxy.cfg
......
stats socket /var/lib/haproxy/haproxy1.sock mode 600 level admin process 1 #绑定第
1个进程和socket文件
stats socket /var/lib/haproxy/haproxy2.sock mode 600 level admin process 2 #绑定第
2个进程和socket文件
nbproc 2
.....

[root@centos7 ~]#echo "disable server lhl-test-80/web2" | socat stdio
/var/lib/haproxy/haproxy1.sock
[root@centos7 ~]#echo "disable server lhl-test-80/web2" | socat stdio
/var/lib/haproxy/haproxy2.sock

[root@haproxy ~]#for i in {1..2};do echo "set weight lhl-test-80/web$i 10" | socat stdio /var/lib/haproxy/haproxy$i.sock;done
#如果静态算法,如:static-rr,可以更改weight为0或1,但不支持动态更改weight为其它值,否则会提示下面信息
[root@centos7 ~]#echo "set weight lhl-test-80/web1 0" | socat stdio /var/lib/haproxy/haproxy.sock

[root@centos7 ~]#echo "set weight lhl-test-80/web1 1" | socat stdio /var/lib/haproxy/haproxy.sock
[root@centos7 ~]#echo "set weight lhl-test-80/web1 2" | socat stdio /var/lib/haproxy/haproxy.sock
Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.

例:上线和下线后端服务器脚本

[root@centos7 ~ ]#cat haproxy_host_up_down.sh
. /etc/init.d/functions
case $1 in
up)
    echo "set weight lhl-m42-web-80/$2 1" | socat stdio /var/lib/haproxy/haproxy.sock
    [ $? -eq 0 ] && action "$2 is up"
    ;;
down)
    echo "set weight lhl-m42-web-80/$2 0" | socat stdio /var/lib/haproxy/haproxy.sock
    [ $? -eq 0 ] && action "$2 is down"
    ;;
*)
    echo "Usage: `basename $0` up|down IP"
    ;;
esac

三、static-rr

static-rr:基于权重的轮询调度,不支持运行时利用socat进行权重的动态调整(只支持0和1,不支持其它值)及后端服务器慢启动,其后端主机数量没有限制,相当于LVS中的 wrr

listen web_host
    bind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010
    mode http
    log global
    balance static-rr
    server web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 10.0.0.27:80 weight 2 check inter 3000 fall 2 rise 5

四、first

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

不支持用socat进行动态修改权重,可以设置0和1,可以设置其它值但无效

listen web_host
    bind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010
    mode http
    log global
    balance first
    server web1 10.0.0.17:80 maxconn 2 weight 1 check inter 3000 fall 2 rise 5
    server web2 10.0.0.27:80 weight 1 check inter 3000 fall 2 rise 5