【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

相关推荐
星光开发者15 分钟前
基于决策树随机森林算法的小说推荐系统的设计与实现-计算机毕设【课程设计】78485
vue.js·算法·微信小程序·django·php·课程设计·express
-森屿安年-24 分钟前
按身高排序
c++·算法
DYWorker00132 分钟前
Linux驱动:RK3568 GMAC + YT8531C 千兆以太网全流程解剖
linux
程序员清风42 分钟前
算法与数据结构入门:复杂度、数组与链表
数据结构·算法·链表
无定义_1 小时前
Codeforces Round 1124 (Div. 2)
算法
吴声子夜歌2 小时前
Nginx应用与运维——Nginx负载均衡应用实战(二)
运维·nginx·负载均衡
lisanmengmeng2 小时前
NRPE 添加命令(一)
linux·运维·服务器
pt10432 小时前
CML网络仿真入门-2:使用CML模拟网络实验环境
运维·网络协议
寻林写彡2 小时前
01| 裸机部署 K8S:从零搭建生产可用集群
云原生·kubernetes
垆边人似月.2 小时前
任务混部(200 分档)
算法