负载均衡:Load Balance,简称LB,是一种服务或基于硬件设备等实现的高可用反向代理技术,负载均 衡将特定的业务(web服务、网络流量等)分担给指定的一个或多个后端特定的服务器或设备,从而提高了 公司业务的并发处理能力、保证了业务的高可用性、方便了业务后期的水平动态扩展。
一.环境配置

配置haproxy主机,webserver1,webserver2的ip地址:



在webserver1,webserver2上安装httpd服务以便用于测试:
bash
[root@webserver1 ~] dnf install httpd -y
root@webserver1 ~] echo webserver1 - 192.168.0.10 > /var/www/html/index.html
[root@webserver1 ~] systemctl enable --now httpd
[root@webserver2 ~] dnf install httpd -y
[root@webserver2 ~] echo webserver2 - 192.168.0.20 > /var/www/html/index.html
[root@webserver2 ~] systemctl enable --now httpd

验证环境:

二.Haproxy的安装及配置参数
安装haproxy:
bash
[root@norouter ~] dnf install haproxy.x86_64 -y
[root@norouter ~] systemctl enable --now haproxy

harpoxy的参数实验:
1.实现最基本的负载
bash
[root@norouter ~]# vim /etc/haproxy/haproxy.cfg
该文件中有:
global:全局参数,在最顶部
defaults:默认参数,在 global 之后
frontend:定义客户端入口,在 defaults 之后
backend:定义后端服务器池,在 frontend 之后(或同级)
在文件中写入以下内容:
frontend webcluster
bind *:80
mode http
use_backend webserver-80
#bind *:80:监听所有网卡的 80 端口,接收客户端请求。
#mode http:工作在 HTTP 模式,支持对 HTTP 报文进行处理。
#use_backend webserver-80:将所有请求转发到名为 webserver-80 的后端服务器组
backend webserver-80
server web1 192.168.0.10:80 check inter 3s fall 3 rise 5
server web2 192.168.0.20:80 check inter 3s fall 3 rise 5
#定义了两个后端服务器:
#web1:IP 地址为 192.168.0.10,端口 80。
#web2:IP 地址为 192.168.0.20,端口 80。
#check inter 3s fall 3 rise 5:启用健康检查,每 3 秒检查一次;连续 3 次失败则标记为宕机,连续 5 次成功则恢复服务。

修改配置文件:vim /etc/haproxy/haproxy.cfg
重启服务后测试:

2.指定把日志发送位置
实验:指定日志发送到192.168.0.10
在192.168.0.10 开启接受日志的端口:
bash
[root@webserver1 ~] vim /etc/rsyslog.conf
在文件的31,32行去掉 #
[root@webserver1 ~] systemctl restart rsyslog.service

测试接受日志端口是否开启:

在haproxy主机中设定日志发送信息:
bash
[root@norouter ~] vim /etc/haproxy/haproxy.cfg
[root@norouter ~] systemctl restart haproxy.service

在webserver1上可以查看到日志:

3.实现haproxy的多进程

bash
[root@norouter ~] vim /etc/haproxy/haproxy.cfg
写在global段中:
nbproc 2
[root@norouter ~] systemctl restart haproxy.service

验证:

多进程cpu绑定:
bash
[root@norouter ~] vim /etc/haproxy/haproxy.cfg
nbproc 2
cpu-map 1 0
cpu-map 2 1
[root@norouter ~] systemctl restart haproxy.service
为不同进程准备不同套接字:



4.实现haproxy的多线程
注意多线程不能和多进程同时启用

启动多线程:
bash
[root@norouter ~] vim /etc/haproxy/haproxy.cfg
nbthread 2
[root@norouter ~] systemctl restart haproxy.service

测试效果:

三.socat热更新工具
热更新:在服务或软件不停止的情况下更新软件或服务的工作方式,完成对软件不停工更新,典型的热更新设备,usb,在使用usb进行插拔时,电脑系统时不需要停止工作的,这中设备叫热插拔设备。
1.安装socat
bash
[root@haproxy ~]# dnf install socat -y
[root@haproxy ~]# socat -h

2.利用socat查看haproxy信息

3.利用socat更改haproxy信息
对socket进行授权:



该修改为临时修改,临时生效,重启后丢失。
测试:

四.Haproxy算法实验
静态算法:按照事先定义好的规则轮询公平调度,不关心后端服务器的当前负载、连接数和响应速度 等,且无法实时修改权重(只能为0和1,不支持其它值),只能靠重启HAProxy生效。
static-rr,first为静态算法。
动态算法:基于后端服务器状态进行调度适当调整, 新请求将优先调度至当前负载较低的服务器 权重可以在haproxy运行时动态调整无需重启。
roundrobin,leastconn为动态算法。
**混合算法:**其它算法即可作为静态算法,又可以通过选项成为动态算法
source,uri,url_param,hdr为混合算法。
1.static-rr
#基于权重的轮询调度
#不支持运行时利用socat进行权重的动态调整(只支持0和1,不支持其它值)
#不支持端服务器慢启动 其后端主机数量没有限制,相当于LVS中的 wrr
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
balance static-rr
server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 2
server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1
[root@haproxy ~] systemctl restart haproxy.service

测试:
不支持在运行时动态修改权重,只接受 0% 或 100% 这两个特殊值

2.first
#根据服务器在列表中的位置,自上而下进行调度
#其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务
#其会忽略服务器的权重设置
#不支持用socat进行动态修改权重,可以设置0和1,可以设置其它值但无效
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
balance first
server haha 192.168.0.10:80 maxconn 1 check inter 3s fall 3 rise 5 weight 2
server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1
[root@haproxy ~] systemctl restart haproxy.service
测试:在一个shell中执行持续访问:

3.roundrobin
基于权重的轮询动态调度算法
支持权重的运行时调整,不同于lvs中的rr轮训模式,
HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数)
其每个后端backend中最多支持4095个real server
支持对real server权重动态调整
roundrobin为默认调度算法,此算法使用广泛
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
balance roundrobin
server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 2
server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1
[root@haproxy ~] systemctl restart haproxy.service

测试:

动态权重更新:

4.leastconn
#leastconn加权的最少连接的动态
#支持权重的运行时调整和慢启动,即:根据当前连接最少的后端服务器而非权重进行优先调度(新客户 端连接)
#比较适合长连接的场景使用,比如:MySQL等场景
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
balance leastconn
server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 2
server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1
[root@haproxy ~] systemctl restart haproxy.service

5.source
源地址hash,基于用户源地址hash并将请求转发到后端服务器,后续同一个源地址请求将被转发至同一 个后端web服务器。此方式当后端服务器数据量发生变化时,会导致很多用户的请求转发至新的后端服 务器,默认为静态方式,但是可以通过hash-type支持的选项更改这个算法一般是在不插入Cookie的TCP 模式下使用,也可给拒绝会话cookie的客户提供最好的会话粘性,适用于session会话保持但不支持 cookie和缓存的场景源地址有两种转发客户端请求到后端服务器的服务器选取计算方式,分别是取模法 和一致性hash
默认静态算法:
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
balance source
server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 2
server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1
[root@haproxy ~] systemctl restart haproxy.service


source动态算法:
bash
listen webcluster
bind *:80
balance source
hash-type consistent
server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 2
server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1

测试:

6.uri
基于对用户请求的URI的左半部分或整个uri做hash,再将hash结果对总权重进行取模后 根据最终结果将请求转发到后端指定服务器 适用于后端是缓存服务器场景 默认是静态算法,也可以通过hash-type指定map-based和consistent,来定义使用取模法还是一致性 hash;此算法基于应用层,所以只支持 mode http ,不支持 mode tcp。
准备实验环境:
bash
[root@webserver1 ~] echo RS1 - 192.168.0.10 > /var/www/html/index1.html
[root@webserver1 ~] echo RS1 - 192.168.0.10 > /var/www/html/index2.html
[root@webserver2 ~] echo RS2 - 192.168.0.20 > /var/www/html/index1.html
[root@webserver2 ~] echo RS2 - 192.168.0.20 > /var/www/html/index2.html


设定uri算法:
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
balance uri
hash-type consistent
server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 2
server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1
[root@haproxy ~] systemctl restart haproxy.service
测试:


7.url_param
url_param对用户请求的url中的 params 部分中的一个参数key对应的value值作hash计算,并由服务器 总权重相除以后派发至某挑出的服务器,后端搜索同一个数据会被调度到同一个服务器,多用与电商 通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个real server 如果无没key,将按roundrobin算法
实验环境:
bash
[root@webserver1 ~] echo RS1 - 192.168.0.10 > /var/www/html/index1.html
[root@webserver1 ~] echo RS1 - 192.168.0.10 > /var/www/html/index2.html
[root@webserver2 ~] echo RS2 - 192.168.0.20 > /var/www/html/index1.html
[root@webserver2 ~] echo RS2 - 192.168.0.20 > /var/www/html/index2.html
设定url_param算法:
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
balance url_param name
hash-type consistent
server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 2
server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1
[root@haproxy ~] systemctl restart haproxy.service

测试:

8.hdr
针对用户每个http头部(header)请求中的指定信息做hash, 此处由 name 指定的http首部将会被取出并做hash计算, 然后由服务器总权重取模以后派发至某挑出的服务器,如果无有效值,则会使用默认的轮询调度。
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
balance hdr(User-Agent)
hash-type consistent
server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 2
server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1
[root@haproxy ~] systemctl restart haproxy.service

测试:

算法总结:
bash
#静态
static-rr--------->tcp/http
first------------->tcp/http
#动态
roundrobin-------->tcp/http
leastconn--------->tcp/http
random------------>tcp/http
#以下静态和动态取决于hash_type是否consistent
source------------>tcp/http
Uri--------------->http
url_param--------->http
hdr--------------->http
五.基于cookie的会话保持
如果在haprorxy中设定算法为source,在同一台客户端主机中,无论使用什么浏览器访问的最终服务器是同一个
可以使用cookie值进行优化,让同一台客户端中同一个浏览器中访问的是同一个服务器
不同浏览器访问的是不同的服务器
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
balance roundrobin
hash-type consistent
cookie WEBCOOKIE insert nocache indirect
server haha 192.168.0.10:80 cookie web1 check inter 3s fall 3 rise 5 weight 2
server hehe 192.168.0.20:80 cookie web2 check inter 3s fall 3 rise 5 weight 1
[root@haproxy ~] systemctl restart haproxy.service



六.HAProxy状态页
配置:
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
listen stats
mode http
bind 0.0.0.0:4321
stats enable
log global
# stats refresh
stats uri /status
stats auth lee:lee
[root@haproxy ~] systemctl restart haproxy.service

测试:

开启自动刷新:
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
listen stats
mode http
bind 0.0.0.0:4321
stats enable
log global
stats refresh 1
stats uri /status
stats auth lee:lee
[root@haproxy ~] systemctl restart haproxy.service


七.IP透传
七层IP透传:
实验环境:
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
balance roundrobin
server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 1
server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1
[root@haproxy ~] systemctl restart haproxy.service

测试环境:

在rs主机中默认未开启透传功能
bash
[root@webserver2 ~] cat /etc/httpd/logs/access_log
192.168.0.100 - - [19/Feb/2026:19:13:04 +0800] "GET / HTTP/1.1" 200 26 "-" "hao"
192.168.0.100 - - [19/Feb/2026:19:28:24 +0800] "GET / HTTP/1.1" 200 26 "-" "curl/7.76.1"
192.168.0.100 - - [19/Feb/2026:19:43:22 +0800] "GET / HTTP/1.1" 200 26 "-" "curl/7.76.1"
192.168.0.100 - - [19/Feb/2026:19:43:22 +0800] "GET / HTTP/1.1" 200 26 "-" "curl/7.76.1"

开启ip透传:
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
.......................
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8 #开启haproxy透传功能
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000

在rs中设定采集透传IP:
bash
[root@webserver2 ~] vim /etc/httpd/conf/httpd.conf
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{X-Forwarded-For}i\" \"%{Referer}i\" \"%{User-Agent}i \"" combined
[root@webserver2 ~] systemctl restart httpd

测试效果:
bash
[root@webserver2 ~] cat /etc/httpd/logs/access_log
92.168.0.100 - - [19/Feb/2026:19:59:30 +0800] "GET / HTTP/1.1" 200 26 "192.168.0.1" "-" "curl/8.4.0 "
192.168.0.100 - - [19/Feb/2026:19:59:32 +0800] "GET / HTTP/1.1" 200 26 "192.168.0.1" "-" "curl/8.4.0 "

四层IP透传:
环境设置:
bash
#在RS中把apache停止
[root@webserver1 ~] systemctl disable --now httpd
[root@webserver2 ~] systemctl disable --now httpd
#部署nginx
[root@webserver1 ~] dnf install nginx -y
[root@webserver2 ~] dnf install nginx -y
[root@webserver1 ~] echo RS1 - 192.168.0.10 > /usr/share/nginx/html/index.html
[root@webserver2 ~] echo RS2 - 192.168.0.20 > /usr/share/nginx/html/index.html
[root@webserver1 ~] systemctl enable --now nginx
[root@webserver2 ~] systemctl enable --now nginx


环境测试:

启用nginx的四层访问控制:
bash
[root@webserver1 ~] vim /etc/nginx/nginx.conf
server {
listen 80 proxy_protocol; #启用四层访问控制
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
[root@webserver1 ~] systemctl restart nginx.service
[root@webserver2 ~] vim /etc/nginx/nginx.conf
server {
listen 80 proxy_protocol; #启用四层访问控制
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
[root@webserver2 ~] systemctl restart nginx.service

测试:

出现上述报错标识nginx只支持四层访问。
设定haproxy访问4层:
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode tcp #四层访问
balance roundrobin
server haha 192.168.0.10:80 send-proxy check inter 3s fall 3 rise 5 weight 1
server hehe 192.168.0.20:80 send-proxy check inter 3s fall 3 rise 5 weight 1
[root@haproxy ~] systemctl restart haproxy.service

测试四层访问
设置4层ip透传:
bash
[root@webserver1&2 ~] vim /etc/nginx/nginx.conf
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'"$proxy_protocol_addr"' #采集透传信息
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
[root@webserver1&2 ~] systemctl restart nginx.service

测试:

bash
[root@webserver1 ~] cat /var/log/nginx/access.log
192.168.0.100 - - [19/Feb/2026:20:12:41 +0800] "GET / HTTP/1.1" "172.25.254.100"200 19 "-" "curl/7.76.1" "-"
192.168.0.100 - - [19/Feb/2026:20:12:41 +0800] "GET / HTTP/1.1" "172.25.254.100"200 19 "-" "curl/7.76.1" "-"

八.Harproxy的四层负载
环境设定
bash
[root@webserver1+2 ~] dnf install mariadb-server mariadb -y
[root@webserver1+1 ~] vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server_id=10 #设定数据库所在主机的id标识,在20上设定id为20
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
#建立远程登录用户并授权
[root@webserver2+1 ~] systemctl start mariadb
[root@webserver2+1 ~] mysql
MariaDB [(none)]> CREATE USER 'lee'@'%' identified by 'lee';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> GRANT ALL ON *.* TO 'lee'@'%';
Query OK, 0 rows affected (0.000 sec)




测试:

四层负载操作:
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
listen mariadbcluster
bind *:6663
mode tcp
balance roundrobin
server haha 192.168.0.10:3306 check inter 3s fall 3 rise 5 weight 1
server hehe 192.168.0.20:3306 check inter 3s fall 3 rise 5 weight 1
[root@haproxy ~] systemctl restart haproxy.service


测试:

backup参数
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
listen mariadbcluster
bind *:6663
mode tcp
balance roundrobin
server haha 192.168.0.10:3306 check inter 3s fall 3 rise 5 weight 1
server hehe 192.168.0.20:3306 check inter 3s fall 3 rise 5 weight 1 backup
[root@haproxy ~] systemctl restart haproxy.service

测试:

关闭10的mariadb并等待1分钟:


还原故障主机等待片刻:


九.自定义HAProxy 错误界面
1.sorryserver的设定
正常的所有服务器如果出现宕机,那么客户将被定向到指定的主机中,这个当业务主机出问题时被临时访问的主机叫做sorryserver
bash
#在新主机中安装apache(可以用haproxy主机代替)
[root@haproxy ~] dnf install httpd -y
[root@haproxy ~] vim /etc/httpd/conf/httpd.conf
第47行:
Listen 8080
[root@haproxy ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@haproxy ~]# echo "11111111111" > /var/www/html/index.html


bash
#配置sorryserver上线
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode tcp
balance roundrobin
server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 1
server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1
server wuwu 192.168.0.100:8080 backup #sorryserver
[root@haproxy ~] systemctl restart haproxy.service

测试:

关闭两台正常的业务主机:



2.自定义错误页面
当所有主机包括sorryserver都宕机了,那么haproxy会提供一个默认访问的错误页面,这个错误页面跟报错代码有关,这个页面可以通过定义来机型设置
bash
# 修改为http
[root@norouter ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode http
option httplog
balance roundrobin
server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 1
server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1

bash
#出现的错误页面
[root@webserver1+2 ~]# systemctl stop httpd
[root@haproxy ~]# systemctl stop httpd
#所有后端web服务都宕机
[Administrator.DESKTOP-VJ307M3] ➤ curl 172.25.254.100
<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.
</body></html>

创建错误文件并写入haproxy配置:



测试:

十.Haproxy ACL访问控制
在Linux中设定解析,也可在windows中设定解析:

设定基础的haproxy实验配置:
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
[root@haproxy ~] systemctl restart haproxy.service

基础acl示例:
bash
[root@haproxy ~] vim /etc/haproxy/haproxy.cfg
frontend webcluster
bind *:80
mode http
acl test hdr_end(host) -i .com #acl列表
use_backend webserver-80-web1 if test #acl列表访问匹配
default_backend webserver-80-web2 #acl列表访问不匹配
backend webserver-80-web1
server web1 192.168.0.10:80 check inter 3s fall 3 rise 5
backend webserver-80-web2
server web2 192.168.0.20:80 check inter 3s fall 3 rise 5
[root@haproxy ~] systemctl restart haproxy.service

测试:

基于访问头部:


base参数acl:


测试:

acl禁止列表黑名单:

测试:

禁止列表白名单:

测试:

十一.Haproxy全站加密
1.制作证书
bash
[root@haproxy ~] mkdir /etc/haproxy/certs/
[root@haproxy ~] openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/haproxy/certs/timinglee.org.key -x509 -days 365 -out /etc/haproxy/certs/timinglee.org.crt
[root@haproxy ~] ls /etc/haproxy/certs/
timinglee.org.crt timinglee.org.key
[root@haproxy ~] cat /etc/haproxy/certs/timinglee.org.{key,crt} > /etc/haproxy/certs/timinglee.pem


2.全站加密

测试:




