【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

相关推荐
格林威4 分钟前
常规线扫描镜头有哪些类型?能做什么?
人工智能·深度学习·数码相机·算法·计算机视觉·视觉检测·工业镜头
openHiTLS密码开源社区32 分钟前
【密码学实战】openHiTLS passwd命令行:专业密码哈希生成工具
linux·密码学·哈希算法·ldap·密码策略·随机盐值
WTCLLB1 小时前
netgear r6220 路由器,刷openwrt后,系统备份还原
linux·网络·智能路由器·openwrt
小灰灰的可爱无人可替代2 小时前
记录一次使用docker和docker-compose更新vue前端项目问题
nginx·docker·vue
迎風吹頭髮2 小时前
UNIX下C语言编程与实践38-UNIX 信号操作:signal 函数与信号捕获函数的编写
linux·c语言·unix
做运维的阿瑞2 小时前
Linux系统性能监控与故障定位实战:CPU/内存/I/O/网络
linux·运维·网络
程序员莫小特2 小时前
老题新解|大整数加法
数据结构·c++·算法
驱动探索者2 小时前
车库到双子星:惠普的百年科技传奇
linux
wanhengidc3 小时前
云手机能够做些什么?
运维·服务器·人工智能·智能手机·云计算
会飞的鱼_1233 小时前
设备管理平台项目全流程部署指南:从环境到ELK日志监控
nginx