【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

相关推荐
自然常数e4 小时前
深入理解指针(6)
c语言·数据结构·算法·visual studio
Xの哲學4 小时前
Linux VxLAN深度解析: 从数据平面到内核实现的全面剖析
linux·服务器·算法·架构·边缘计算
TL滕4 小时前
从0开始学算法——第十八天(分治算法练习)
笔记·学习·算法
阿里云云原生5 小时前
直播回顾丨详解阿里云函数计算 AgentRun,手把手带你走进 Agentic AI 生产时代
云原生
添砖java‘’5 小时前
Linux信号机制详解:从产生到处理
linux·c++·操作系统·信号处理
月明长歌5 小时前
【码道初阶】【LeetCode 958】判定完全二叉树:警惕 BFS 中的“管中窥豹”陷阱
算法·leetcode·宽度优先
Evan芙5 小时前
nginx日志管理及日志格式定制
运维·nginx
MC皮蛋侠客5 小时前
Linux C++使用GDB调试动态库崩溃问题完全指南
linux·c++
Wang's Blog5 小时前
RabbitMQ: 消息发送、连接管理、消息封装与三种工程方案
linux·ubuntu·rabbitmq
一直都在5725 小时前
数据结构入门:二叉排序树的构建与相关算法
数据结构·算法