haproxy的工具及其算法

一、socat 工具

dnf install socat -y

#查看帮助文档

echo "help" | socat stdio /var/lib/haproxy/stats

#查看参数

echo "show info" | socat stdio /var/lib/haproxy/stats

#查看服务状态

echo "show servers state" | socat stdio /var/lib/haproxy/stat

#查看权重

echo "get weight webcluster/web1" | socat stdio /var/lib/haproxy/stats

#更改权重

echo "set weight webcluster/web1 2" | socat stdio /var/lib/haproxy/stats

基本实验:

环境准备:

需要三台虚拟机:

rhel9克隆:haproxy(172.25.254.100)、webserver1(172.25.254.10)、webserver2(172.25.254.20)

见上个文档,可在上个文档实验的基础上进行操作

haproxy是什么?以及haproxy基础实验-CSDN博客

实验步骤:

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
[root@haproxy ~]# ll /var/lib/haproxy/stats 
srwxr-xr-x 1 root root 0 Aug  9 11:11 /var/lib/haproxy/stats
[root@haproxy ~]# 
[root@haproxy ~]# dnf install socat -y
#查看帮助文档
[root@haproxy ~]# echo "help" | socat stdio /var/lib/haproxy/stats 
#查看信息
[root@haproxy ~]# echo "show info" | socat stdio /var/lib/haproxy/stats
#查看服务状态
[root@haproxy ~]# echo "show servers state" | socat stdio /var/lib/haproxy/stats 
#查看权重
[root@haproxy ~]# echo "get weight webcluster/web1" | socat stdio /var/lib/haproxy/stats 
2 (initial 2)

#更改权重
[root@haproxy ~]# echo "set weight webcluster/web1 1" | socat stdio /var/lib/haproxy/stats 

#禁止web1
[root@haproxy ~]# echo "disable server webcluster/web1" | socat stdio /var/lib/haproxy/stats 

#启动web1
[root@haproxy ~]# echo "enable server webcluster/web1" | socat stdio /var/lib/haproxy/stats 

#多进程实验
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
[root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# ll /var/lib/haproxy/
total 0
srw------- 1 root root 0 Aug  9 11:51 stats
srw------- 1 root root 0 Aug  9 11:56 stats1
srw------- 1 root root 0 Aug  9 11:56 stats2
[root@haproxy ~]# echo "show info" | socat stdio /var/lib/haproxy/stats 		#因为没有使用stats,所以查不到信息
2024/08/09 12:00:04 socat[2544] E connect(5, AF=1 "/var/lib/haproxy/stats", 24): Connection refused
[root@haproxy ~]# echo "show info" | socat stdio /var/lib/haproxy/stats1 

测试: 

#这是权重为2的轮询
[root@webserver1 ~]# for i in {1..10}
> do
> curl 172.25.254.100
> done
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
#这是修改权重后,权重为1的轮询
[root@webserver1 ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
#这是禁用web1的轮询
[root@webserver1 ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
#这是启动web1的轮询
[root@webserver1 ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10

 #多进程实验

er

二、haproxy算法

1. 静态
static-rr            cp/http                                 #不管后端
first                  tcp/http


2. 动态
roundrobin        tcp/http
leastconn          tcp/http                               #谁链接少,权重高,就给谁
random             tcp/http


3. 以下静态和动态取决于 hash_type 是否 consistent
source              tcp/http
Uri                     http                                      #访问地址内容进行hash
url_ param        http                                       #对动作请求进行hash
hdr                    http                                       #对报文头进行hash

2.1 静态算法

static-rr

支持权重,不支持调整权重

步骤:
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
[root@haproxy ~]# systemctl restart haproxy.service 

 

测试:

前后对比 

first

2.2 动态算法

roundrobin

步骤:
[ root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
[ root@haproxy ~]# systemctl restart haproxy.service
[ root@haproxy ~]# echo "get weight webcluster/web1" | socat stdio /var/lib/haproxy/stats
[ root@haproxy ~]# echo "set weight webcluster/web1 1" | socat stdio /var/lib/haproxy/stats
[ root@haproxy ~]# echo "get weight webcluster/web1" | socat stdio /var/lib/haproxy/stats

测试:

set前后对比

leastconn

2.3 其他算法

source

(balance source)静态

步骤:

map-base 取模法

通过取模的结果访问主机(当权重都为1时,几台机子取模就取几)(当其中一台机器的权重变化后,导致会话丢失,解决该方法使用hash一致性)

hash一致性

hash算法(动态)

步骤:
[ root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
[ root@haproxy ~]# systemctl restart haproxy.service
[ root@haproxy ~]# echo "set weight webcluster/web1 1" | socat stdio /var/1ib/haproxy/stats
Backend is using a static LB algorithm and only accepts weights ' 0% ' and ' 100% '

#前后对比
[ root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
[ root@haproxy ~]# systemctl restart haproxy.service
[ root@haproxy ~]# echo "set weight webcluster/web1 1" | socat stdio /var/Lib/haproxy/stats

uri

默认是静态算法

?前,向服务器传递的指令

?后,指令的目标,字段

#后面,为片段

uri取模法

uri不变,访问的地址也就不变,不会根据轮询效果来

步骤:
[ root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
[ root@haproxy ~]# systemctl restart haproxy.service

[ root@webserver1 ~]# echo 172.25.254.10 - index1.html > /usr/share/nginx/html/index1.html
[ root@webserver1 ~]# echo 172.25.254.10 - index2.html > /usr/share/nginx/html/index2.html
[ root@webserver1 ~]# echo 172.25.254.10 - index3.html > /usr/share/nginx/html/index3.html
[ root@webserver2 ~]# echo 172.25.254.20 - index1.html > /usr/share/nginx/html/index1.html
[ root@webserver2 ~]# echo 172.25.254.20 - index2.html > /usr/share/nginx/html/index2.html
[ root@webserver2 ~]# echo 172.25.254.20 - index3.html > /usr/share/nginx/html/index3.html
测试:

两台web设置一个index1和设置三个的前后对比:

url_param一致性

测试结果

不使用数字,会导致(与取模的数字冲突)出问题

链接问题,会断开变化主机IP

同一个name,就返回同一个IP

hdr

步骤:

结果: 

三、状态页测试实验

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值