【HAProxy05】企业级反向代理HAProxy调度算法之静态算法与动态算法

HAProxy 调度算法

HAProxy通过固定参数

balance 指明对后端服务器的调度算法,该参数可以配置在listen或backend选 项中。

HAProxy的调度算法分为静态和动态调度算法,但是有些算法可以根据不同的参数实现静态和动态算法 相互转换。 官方文档: http://cbonte.github.io/haproxy-dconv/2.4/configuration.html#4-balancehttp://cbonte.github.io/haproxy-dconv/2.4/configuration.html#4-balance

静态算法

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

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
范例:调整权重
复制代码
#只支持0或100%
[root@haproxy ~]#echo "set weight  www.wang.org_nginx/10.0.0.101 0" | socat stdio/var/lib/haproxy/haproxy.sock
[root@haproxy ~]#echo "get weight  www.wang.org_nginx/10.0.0.101" | socat stdio/var/lib/haproxy/haproxy.sock
 0 (initial 3)
[root@haproxy ~]#echo "set weight  www.wang.org_nginx/10.0.0.101 1" | socat stdio/var/lib/haproxy/haproxy.sock
Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.
#只支持0或100%
[root@haproxy ~]#echo "set weight  www.wang.org_nginx/10.0.0.101 100%" | socat stdio /var/lib/haproxy/haproxy.sock

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

测试访问效果
复制代码
#同时运行下面命令,观察结果
#在后端nginx服务器上限速 nginx.conf配置文件
server {
   ....
   limit_rate 10;
   ....
 }
#用wget下载文件测试才能看到效果
wget --limit-rate 1k http://192.168.10.100/test.img
#curl测试不成功
#while  true;do  curl http://10.0.0.7/index.html ; sleep 0.1;done

#动态修改权重,不报错,但不生效
[root@haproxy ~]#echo "set weight  www.wang.org_nginx/10.0.0.102 10" | socat stdio /var/lib/haproxy/haproxy.sock

动态算法

动态算法:基于后端服务器状态进行调度适当调整,新请求将优先调度至当前负载较低的服务器,且权重可以在haproxy运行时动态调整无需重启。

roundrobin 算法

roundrobin:基于权重的轮询动态调度算法,支持权重的运行时调整,不同于lvs中的rr轮训模式, HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),其每个后端backend中最多支 持4095个real server,支持对real server权重动态调整,roundrobin为默认调度算法,此算法使用广泛

listen web_host

bind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010

mode http

log global

balance roundrobin

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

支持动态调整权重:
复制代码
# echo "get weight web_host/web1" | socat stdio /var/lib/haproxy/haproxy.sock 
1 (initial 1)
 # echo "set weight web_host/web1 3" | socat stdio /var/lib/haproxy/haproxy.sock 
# echo "get weight web_host/web1" | socat stdio /var/lib/haproxy/haproxy.sock 
3 (initial 1)

leastconn 算法

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

相当于LVS中的WLC算法

listen web_host

bind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010

mode http

log global

balance leastconn

server web1 10.0.0.17:80 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

random 算法

在1.9版本开始增加 random的负载平衡算法,其基于随机数作为一致性hash的key,随机负载平衡对于 大型服务器场或经常添加或删除服务器非常有用,支持weight的动态调整,weight较大的主机有更大概 率获取新请求

random配置实例

listen web_host

bind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010

mode http

log global

balance random

server web1 10.0.0.17:80 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

相关推荐
ZERO_pan39 分钟前
服务器装机遇到的问题
运维·服务器
逆小舟40 分钟前
【Linux】人事档案——用户及组管理
linux·c++
青草地溪水旁40 分钟前
pthread_mutex_lock函数深度解析
linux·多线程·pthread
杀气丶1 小时前
Linux下运行芙蕾雅天堂2【俄文简译L2FATER】
运维·服务器·天堂2·l2fater·l2fater.cn
太空的旅行者2 小时前
告别双系统——WSL2+UBUNTU在WIN上畅游LINUX
linux·运维·ubuntu
九章云极AladdinEdu3 小时前
超参数自动化调优指南:Optuna vs. Ray Tune 对比评测
运维·人工智能·深度学习·ai·自动化·gpu算力
人工智能训练师4 小时前
Ubuntu22.04如何安装新版本的Node.js和npm
linux·运维·前端·人工智能·ubuntu·npm·node.js
灿烂阳光g4 小时前
domain_auto_trans,source_domain,untrusted_app
android·linux
Tisfy5 小时前
服务器 - 从一台服务器切换至另一台服务器(损失数十条访客记录)
运维·服务器
风中的微尘5 小时前
39.网络流入门
开发语言·网络·c++·算法