一、理论
Haproxy用于群集调度。
Haproxy的调度算法
RR(round robin)
轮询算法。
A B C 三个节点 10个访问请求 A:1 4 7 B:2 5 8 C:3 6 9
LC(least connections)
最小连接数算法
A B C 三个节点 谁当前活跃连接数最少就把访问请求给谁。
SH(source hashing)
基于来源访问调度算法,可基于来源IP、cookie等进行调度。
A B C 三个节点 基于ip来源 第一个客户访问被调度器分配到A上,第二个客户分到B 第三个客户分到C。 A绑定客户1 B绑定客户2 C绑定客户3 除非调度器重启,否则绑定关系不会消失。
二、实践
bash
1、环境
n1 192.168.10.101
n2 192.168.10.102
ha 192.168.10.103
client 192.168.10.104
2、过程
[root@n1 ~]# dnf -y install gcc make pcre-devel zlib-devel
[root@n1 ~]# tar zxf nginx-1.26.3.tar.gz
[root@n1 ~]# cd nginx-1.26.3
[root@n1 nginx-1.26.3]# useradd -M -s /sbin/nologin nginx
[root@n1 nginx-1.26.3]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@n1 nginx-1.26.3]# make && make install
[root@n1 nginx-1.26.3]# cd /usr/local/nginx/html/
[root@n1 html]# echo "n1" > test.html
[root@n1 html]# /usr/local/nginx/sbin/nginx
[root@n1 html]# curl 192.168.10.101/test.html
n1
[root@n2 ~]# dnf -y install gcc make pcre-devel zlib-devel
[root@n2 ~]# tar zxf nginx-1.26.3.tar.gz
[root@n2 ~]# cd nginx-1.26.3
[root@n2 nginx-1.26.3]# useradd -M -s /sbin/nologin nginx
[root@n2 nginx-1.26.3]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@n2 nginx-1.26.3]# make && make install
[root@n2 nginx-1.26.3]# cd /usr/local/nginx/html/
[root@n2 html]# echo "n2" > test.html
[root@n2 html]# /usr/local/nginx/sbin/nginx
[root@n2 html]# curl 192.168.10.101/test.html
n2
[root@ha ~]# dnf -y install haproxy
[root@ha ~]# cat /etc/haproxy/haproxy.cfg global为全局配置 defaults为默认配置 listen为应用组件配置。
global
log 127.0.0.1 local2 # 配置日志记录,local2为日志设备,默认存放到系统日志
chroot /var/lib/haproxy # 禁锢运行目录,防止被恶意攻击对系统上其他路径资源的破坏。
pidfile /var/run/haproxy.pid # 进程文件存放位置。
user haproxy # 运行用户
group haproxy # 运行组
daemon # 是否以守护进程方式运行。
maxconn 4000 # 最大连接数
defaults(在应用组件中如果没有特别声明,将按照默认配置参数设置。)
mode http # 模式为http
log global # 定义日志为global配置中的日志定义。
option httplog # 采用http日志格式记录日志。
option dontlognull # 空请求,用于http健康检查。
retries 3 # 检查节点服务器失败次数,连续达到三次失败,则认为节点不可用。
timeout http-request 5s # http最大请求时间,单位:秒。
timeout queue 1m # 请求队列超时时间,单位:分。
timeout connect 5s # 连接超时时间,单位:秒。
timeout client 1m # 客户端超时时间,单位:秒。
timeout server 1m # 服务器超时时间,单位:分。
timeout http-keep-alive 5s #
timeout check 5s #
maxconn 3000 # 最大连接数
listen webcluster # 定义一个appli4-backup的应用
bind 0.0.0.0:80 # 监听所有网卡的80端口
option httpchk GET /index.html # 通过GET /index.html检查服务器健康状态。
balance roundrobin # 负载均衡调度算法使用轮询
server n1 192.168.10.101:80 check inter 2000 fall 3 # 后端服务器1 每两秒检查一次,3次失败后下线。
server n2 192.168.10.102:80 check inter 2000 fall 3
[root@ha ~]# systemctl start haproxy
[root@client ~]# curl 192.168.10.103/test.html
n1
[root@client ~]# curl 192.168.10.103/test.html
n2
[root@client ~]# curl 192.168.10.103/test.html
n1
[root@client ~]# curl 192.168.10.103/test.html
n2
[root@client ~]# curl 192.168.10.103/test.html
n1
[root@client ~]# curl 192.168.10.103/test.html
n2
[root@n2 ~]# /usr/local/nginx/sbin/nginx -s stop
[root@client ~]# curl 192.168.10.103/test.html
n1
[root@client ~]# curl 192.168.10.103/test.html
n1
[root@client ~]# curl 192.168.10.103/test.html
n1
[root@client ~]# curl 192.168.10.103/test.html
n1
[root@ha ~]# vim /etc/haproxy/haproxy.cfg
global
log /dev/log local0 info # /dev/log制定了haproxy的标准输出和错误输出的目标文件。 local0是一个标签(log identifier),用于区分不同组件或服务的日志,方便后续通过rsyslog或其他工具进行分类归文件。 info制定了日志的最低级别。haproxy会记录从info级别及以上的日志。
#chroot /var/lib/haproxy
[root@ha dev]# ll
lrwxrwxrwx. 1 root root 28 5月29日 13:13 log -> /run/systemd/journal/dev-log
[root@ha ~]# vim /etc/rsyslog.d/99-haproxy.conf
local0.* /var/log/haproxy.log # local0.* 是一个rsyslog的匹配规则,表示所有与local0设定相关的日志都符合。 *表示匹配所有日志级别(例如:debug、info、notice等)。 /var/log/haproxy.log是日志文件的路径。
[root@ha ~]# touch /var/log/haproxy.log
[root@ha ~]# chmod 640 /var/log/haproxy.log
[root@ha ~]# chown root:adm /var/log/haproxy.log # 确保rsyslog用户(adm组)有写入权限。
[root@ha ~]# systemctl restart haproxy
[root@ha ~]# systemctl restart rsyslog
[root@ha ~]# tail -f /var/log/haproxy.log
May 29 13:39:32 ha haproxy[1943]: 192.168.10.104:55548 [29/May/2025:13:39:32.839] webcluster webcluster/n1 0/0/1/1/2 200 200 - - ---- 1/1/0/0/0 0/0 "GET /test.html HTTP/1.1"
May 29 13:39:33 ha haproxy[1943]: 192.168.10.104:55564 [29/May/2025:13:39:33.358] webcluster webcluster/n2 0/0/3/2/5 200 200 - - ---- 1/1/0/0/0 0/0 "GET /test.html HTTP/1.1"
May 29 13:39:33 ha haproxy[1943]: 192.168.10.104:55572 [29/May/2025:13:39:33.816] webcluster webcluster/n1 0/0/1/2/3 200 200 - - ---- 1/1/0/0/0 0/0 "GET /test.html HTTP/1.1"
May 29 13:39:34 ha haproxy[1943]: 192.168.10.104:55576 [29/May/2025:13:39:34.244] webcluster webcluster/n2 0/0/1/1/2 200 200 - - ---- 1/1/0/0/0 0/0 "GET /test.html HTTP/1.1"