当前位置: 首页 > web >正文

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

文章目录

  • 一、概述
  • 二、socat工具
  • 三、static-rr
  • 四、first

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_hostbind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010mode httplog globalbalance static-rrserver web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 5server web2 10.0.0.27:80 weight 2 check inter 3000 fall 2 rise 5

四、first

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

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

listen web_hostbind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010mode httplog globalbalance firstserver web1 10.0.0.17:80 maxconn 2 weight 1 check inter 3000 fall 2 rise 5server web2 10.0.0.27:80 weight 1 check inter 3000 fall 2 rise 5
http://www.xdnf.cn/news/16841.html

相关文章:

  • 为什么Android主线程与java主线程不同,不会退出?
  • 全栈:怎么把IDEA和Maven集成一下?
  • 前端框架Vue3(四)——组件通信及其他API
  • 分布内侧内嗅皮层的层Ⅱ或层Ⅲ的网格细胞(grid cells)对NLP中的深层语义分析的积极影响和启示
  • 一万字讲解Java中的IO流——包含底层原理
  • QtConcurrent::run函数
  • Nginx反向代理负载均衡
  • 常用设计模式系列(十六)—策略模式
  • Ubuntu 24.04 LTS 保姆级教程:安装 NVIDIA 显卡驱动、CUDA 12.5 及 Docker 容器工具包
  • 【YOLOv1】
  • 云服务器数据库
  • 【龙泽科技】汽车维护与底盘拆装检修仿真教学软件【风光580】
  • 机器学习①【机器学习的定义以及核心思想、数据集:机器学习的“燃料”(组成和获取)】
  • [Broken IOS] 配置CLI | 终端用户界面TUI
  • sqli-labs:Less-12关卡详细解析
  • C++异常处理的成本:理解与优化
  • Golang 调试技巧:在 Goland 中查看 Beego 控制器接收的前端字段参数
  • 文法中的间接左递归
  • Java【代码 21】将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理
  • 量子测量的物理场景与理论
  • sqoop从pg导出数据到hadoop上
  • 【数据结构初阶】--二叉树选择题专辑
  • 【人工智能-15】OpenCV直方图均衡化,模板匹配,霍夫变换,图像亮度变换,形态学变换
  • 【PHP类的基础概念:从零开始学面向对象】
  • ES11 / ES2020 动态 import()(异步加载模块)
  • Java项目:基于SSM框架实现的小区物业管理系统【ssm+B/S架构+源码+数据库+毕业论文+开题报告+任务书+远程部署】
  • 使用神经网络与5折交叉验证进行基因组预测:基础知识指南
  • 【JMeter】性能测试脚本录制及完善
  • 从一开始的网络攻防(十三):WAF入门到上手
  • day 40 打卡-装饰器