HAProxy 调度算法
HAProxy通过固定参数
balance 指明对后端服务器的调度算法,该参数可以配置在listen或backend选 项中。
HAProxy的调度算法分为静态和动态调度算法,但是有些算法可以根据不同的参数实现静态和动态算法 相互转换。
Socat 工具
对服务器动态权重和其它状态可以利用 socat 工具进行调整,Socat 是 Linux 下的一个多功能的网络工 具,名字来由是Socket CAT,相当于netCAT的增强版.Socat 的主要特点就是在两个数据流之间建立双向 通道,且支持众多协议和链接方式。如 IP、TCP、 UDP、IPv6、Socket文件等
范例一:利用工具socat 对服务器动态权重调整
1.安装socat与查看帮助
#安装socat
[root@ubuntu1804 ~]#apt update && apt -y install socat
[root@centos ~]#yum -y install socat
#查看HAPorxy套接字文件 我的是/run/haproxy/admin.sock 有的是/var/lib/haproxy/haproxy.sock
[root@centos7 ~]#find / -type s -a -name "*.sock"
/run/haproxy/admin.sock
[root@centos7 ~]#echo "help" | socat stdio /run/haproxy-master.sock
#查看帮助
[root@centos7 ~]#socat -h
[root@haproxy ~]#echo "help" | socat stdio /run/haproxy/admin.sock
The following commands are valid at this level:
abort ssl cert <certfile> : abort a transaction for a certificate file
add acl [@<ver>] <acl> <pattern> : add an acl entry
add map [@<ver>] <map> <key> <val> : add a map entry (payload supported instead of key/val)
add ssl crt-list <list> <cert> [opts]* : add to crt-list file <list> a line <cert> or a payload
clear acl [@<ver>] <acl> : clear the contents of this acl
......................(省略)
2.获取当前连接数
#可以用于实现Zabbix监控
[root@haproxy ~]#echo "show info" | socat stdio /run/haproxy/admin.sock
Name: HAProxy
Version: 2.4.24-0ubuntu0.22.04.1
Release_date: 2023/10/31
Nbthread: 2
Nbproc: 1
Process_num: 1
......................(省略)
#获取当前连接数
[root@haproxy ~]#echo "show info" | socat stdio /run/haproxy/admin.sock | awk '/CurrConns/{print $2}'
3.修改并查看权重
[root@haproxy ~]#cat /etc/haproxy/conf.d/www.web.com.cfg
frontend www.web01.com
bind 172.16.1.211:80
use_backend www.web02.combackend www.web02.com
server web01 10.0.0.52:80 check inter 3000 fall 3 rise 5
server web02 10.0.0.53:80 check inter 3000 fall 3 rise 5
#请求显示所有后端服务器的当前状态
[root@haproxy ~]#echo "show servers state" | socat stdio /run/haproxy/admin.sock
1
# be_id be_name srv_id srv_name srv_addr srv_op_state srv_admin_state srv_uweight srv_iweight srv_time_since_last_change srv_check_status srv_check_result srv_check_health srv_check_state srv_agent_state bk_f_forced_id srv_f_forced_id srv_fqdn srv_port srvrecord srv_use_ssl srv_check_port srv_check_addr srv_agent_addr srv_agent_port
4 www.web02.com 1 web01 10.0.0.52 0 0 1 1 10383 7 2 0 6 0 0 0 - 80 - 0 0 - - 0
4 www.web02.com 2 web02 10.0.0.53 0 0 1 1 10381 7 2 0 6 0 0 0 - 80 - 0 0 - - 0
5 MASTER 1 cur-1 - 2 0 0 0 10383 1 0 0 0 0 0 0 - 0 - 0 0 - - 0
#修改weight,注意只针对单进程有效
[root@haproxy ~]#echo "set weight www.web02.com/web01 2" | socat stdio /run/haproxy/admin.sock#获取名为 网址/web01 的后端服务器的权重。
[root@haproxy ~]#echo "get weight www.web02.com/web01" | socat stdio /run/haproxy/admin.sock
2 (initial 1)#测试 web01权重高
[root@master-db ~]#curl 172.16.1.211
web01 10.0.0.52
[root@master-db ~]#curl 172.16.1.211
web01 10.0.0.52
[root@master-db ~]#curl 172.16.1.211
web02 10.0.0.53
4.禁用与启动后端服务器
#将后端服务器禁用,注意只针对单进程有效
[root@haproxy ~]#echo "disable server www.web02.com/web01" | socat stdio /run/haproxy/admin.sock
#启用后端服务器
[root@haproxy ~]#echo "enable server www.web02.com/web01" | socat stdio /run/haproxy/admin.sock
后端服务器禁用状态
后端服务器启动状态
5. 针对haproxy的多进程,将后端服务器禁用
#针对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 wang-test-80/web2" | socat stdio /var/lib/haproxy/haproxy1.sock
[root@centos7 ~]#echo "disable server wang-test-80/web2" | socat stdio /var/lib/haproxy/haproxy2.sock
[root@haproxy ~]#for i in {1..2};do echo "set weight wang-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 wang-test-80/web1 0" | socat stdio /var/lib/haproxy/haproxy.sock
[root@centos7 ~]#echo "set weight wang-test-80/web1 1" | socat stdio /var/lib/haproxy/haproxy.sock
[root@centos7 ~]#echo "set weight wang-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%'.
6.新的写法
#新的写法
#相当于disable server 进入维护状态(state maint)。
[root@ubuntu2004 ~]#echo "set server www.web02.com/web01 state maint" | socat stdio /run/haproxy/admin.sock
#相当于enable server ready表示服务器已准备好接受连接。
[root@ubuntu2004 ~]#echo "set server www.web02.com/web01 state ready" | socat stdio /run/haproxy/admin.sock
#设置后端服务器为active or backup SOFT STOPPED for maintenance
drain 状态意味着服务器将不再接受新的连接,但会处理完当前已经建立的连接后再关闭。这通常用于维护或重启服务器之前,以确保现有连接能够优雅地关闭。
[root@ubuntu2004 ~]#echo "set server www.web02.com/web01 state drain" | socat stdio /run/haproxy/admin.sock
范例二:上线和下线后端服务器脚本
Ubuntu版本
[root@ubuntu2204 ~]#cat haproxy_host_up_down.sh
#!/bin/bash
WEB_SERVERS="
10.0.0.201
10.0.0.202
"
BACKEND=web_servers
APP=nginx
IMAGE=$APP:1.20
DELAY=10
for i in $WEB_SERVERS;do
echo "set server $BACKEND/$i state maint" | socat stdio /var/lib/haproxy/haproxy.sock
ssh $i docker rm -f $APP
ssh $i "echo DOCKER $i WEBSITE $1 v1.0 > /data/www/index.html"
ssh $i docker run -d -p 80:80 -v /data/www:/usr/share/nginx/html --name $APP $IMAGE
sleep $DELAY
echo "set server $BACKEND/$i state ready" | socat stdio /var/lib/haproxy/haproxy.sock
done
centos7版本
[root@centos7 ~]#cat haproxy_host_up_down.sh
. /etc/init.d/functions
case $1 in
up)
echo "enable server wang-web-80/$2" | socat stdio /var/lib/haproxy/haproxy.sock
[ $? -eq 0 ] && action "$2 is up"
;;
down)
echo "disable server wang-web-80/$2" | socat stdio /var/lib/haproxy/haproxy.sock
[ $? -eq 0 ] && action "$2 is down"
;;
*)
echo "Usage: `basename $0` up|down IP"
;;
esac
范例三:实现容器服务的上线和下线
#!/bin/bash
WEB_SERVERS="
10.0.0.101
10.0.0.102
"
for i in $WEB_SERVERS;do
echo "set server www.wang.org_nginx/$i state maint" | socat stdio /var/lib/haproxy/haproxy.sock
ssh $i docker rm -f nginx
ssh $i "echo DOCKER $i WEBSITE $1 > /data/www/index.html"
ssh $i docker run -d -p 80:80 -v /data/www:/usr/share/nginx/html --name nginx nginx
sleep 10
echo "set server www.wang.org_nginx/$i state ready" | socat stdio /var/lib/haproxy/haproxy.sock
done
范例四:用户无感知滚动升级
#!/bin/bash
Web_url="www.web02.com"
WEB_SERVERS="
10.0.0.52
10.0.0.53
"
for i in $WEB_SERVERS;do
echo "set server $Web_url/$i state maint" | socat stdio /run/haproxy/admin.sock
ssh $i "echo nginx $i WEBSITE $1 > /apps/nginx/html/index.html"
echo "set server $Web_url/$i state ready" | socat stdio /run/haproxy/admin.sock
done
测试
[root@haproxy ~]#bash app_upgrade.sh v1.0
[root@haproxy ~]#bash app_upgrade.sh v2.0
[root@haproxy ~]#bash app_upgrade.sh v3.0
[root@master-db ~]#while true ;do curl 172.16.1.211 ;sleep 0.8; done
nginx 10.0.0.53 WEBSITE v1.0
nginx 10.0.0.52 WEBSITE v1.0
nginx 10.0.0.53 WEBSITE v1.0
nginx 10.0.0.52 WEBSITE v1.0
nginx 10.0.0.53 WEBSITE v1.0
nginx 10.0.0.52 WEBSITE v1.0
nginx 10.0.0.53 WEBSITE v1.0
nginx 10.0.0.52 WEBSITE v2.0
nginx 10.0.0.53 WEBSITE v2.0
nginx 10.0.0.52 WEBSITE v2.0
nginx 10.0.0.53 WEBSITE v2.0
nginx 10.0.0.52 WEBSITE v2.0
nginx 10.0.0.53 WEBSITE v2.0
nginx 10.0.0.52 WEBSITE v2.0
nginx 10.0.0.53 WEBSITE v2.0
nginx 10.0.0.52 WEBSITE v2.0
nginx 10.0.0.53 WEBSITE v2.0
nginx 10.0.0.52 WEBSITE v3.0
nginx 10.0.0.53 WEBSITE v3.0
nginx 10.0.0.53 WEBSITE v3.0
nginx 10.0.0.52 WEBSITE v3.0
nginx 10.0.0.53 WEBSITE v3.0
^C
标签:HAProxy,haproxy,10.0,sock,企业级,nginx,HAProxy04,socat,root
From: https://blog.csdn.net/weixin_74814027/article/details/143564035