HAProxy (High Availability Proxy) 是一款非常高效且功能强大的开源负载均衡器和代理服务器软件。它被设计用于处理高流量的网站,并能够提供高级别的性能、稳定性和安全性。HAProxy 主要用于 TCP 和 HTTP 层面的负载均衡,支持多种负载均衡算法和策略。
特点
- 高性能:HAProxy 能够处理每秒数百万个连接,适用于高负载环境。
- 高度可配置:通过简单的文本配置文件可以进行详细的设置。
- 健康检查:自动检测后端服务器的健康状态,并在必要时将请求重新路由到健康的服务器。
- SSL/TLS 终止:可以在 HAProxy 层面上终止 SSL/TLS 连接,减轻后端服务器的负担。
- 会话保持:可以根据需要启用会话保持功能,确保来自同一客户端的请求被定向到相同的后端服务器。
- 灵活的日志记录:支持详细日志记录,便于监控和调试。
- 高可用性:支持通过 VRRP 或 Keepalived 与另一个 HAProxy 实例形成高可用集群。
使用场景
- Web 应用程序负载均衡:分发来自用户的 HTTP/HTTPS 请求到多个 Web 服务器。
- 数据库负载均衡:分发数据库查询到多个数据库服务器。
- API 负载均衡:作为 RESTful API 的前端,分发请求到不同的服务实例。
- 安全网关:提供 SSL 加密、DDoS 防护等安全特性。
hapoxy的工作原理
工作流程
-
监听端口:
- HAProxy 在配置好的端口上监听客户端的连接请求。
-
接受请求:
- 当客户端发起请求时,HAProxy 接收这些请求,并根据配置解析请求,确定如何处理。
-
路由请求:
- 根据配置的负载均衡算法(如轮询、加权轮询、最小连接数、源 IP 哈希等),HAProxy 将请求分发到后端服务器。
- 如果启用了会话保持,HAProxy 会确保来自同一客户端的请求始终被路由到相同的后端服务器。
-
健康检查:
- HAProxy 可以定期向后端服务器发送健康检查请求,以确认它们是否可以接收新的请求。
- 如果后端服务器未通过健康检查,HAProxy 会暂时从负载均衡池中移除该服务器,直到其恢复。
-
处理响应:
- 当后端服务器返回响应时,HAProxy 收集这些响应,并将它们转发给原始客户端。
- 在某些情况下,HAProxy 还可以修改响应内容(例如,替换某些 URL 或重写 cookie)。
-
日志记录和统计:
- HAProxy 会记录请求和响应的详细信息,以便进行监控和故障排查。
- 提供实时统计数据,帮助管理员了解系统的性能和健康状况。
技术细节
-
事件驱动模型:
- HAProxy 使用事件驱动和非阻塞 I/O 模型,这意味着它可以同时处理多个连接而不需创建额外的线程或进程。
- 这种设计允许 HAProxy 有效地处理大量并发连接,减少上下文切换的开销。
-
高效的数据传输:
- HAProxy 利用操作系统级别的特性,比如零拷贝技术和单缓冲机制,以提高数据传输效率。
- 这意味着数据可以在网络堆栈和应用程序之间直接传输,而不需要额外的复制操作。
-
性能优化:
- HAProxy 包含许多内部优化,比如 O(1) 复杂度的事件检查器和延迟更新技术,这使得它能够在较低的 CPU 使用率下处理高负载。
结构
-
前端:
- 代表了 HAProxy 接受外部连接的部分,通常绑定到特定的 IP 地址和端口。
-
后端:
- 定义了将请求转发给的一组服务器。每个后端可以包含多个服务器。
-
监听器:
- 定义了 HAProxy 如何处理来自前端的连接,包括负载均衡策略、健康检查等。
高可用性
- Keepalived 或者 VRRP:
- HAProxy 可以与其他软件如 Keepalived 结合使用,形成高可用集群。
- 这种配置通常涉及两个或更多的 HAProxy 实例,其中一个作为主节点,其他作为备用节点。
负载均衡算法
HAProxy支持多种负载均衡算法,包括:轮询调度(Round Robin):将请求依次分配给每个后端服务器。
最少连接(Least Connections):将请求分配给当前连接数最少的服务器。
源地址哈希(Source Hashing):根据客户端的IP地址分配请求,确保同一客户端的请求总是分配到同一台服务器。
加权轮询(Weighted Round Robin):根据服务器的权重分配请求,权重高的服务器分配更多的请求
四层负载均衡
1.通过ip+port决定负载均衡的去向。
2.对流量请求进行NAT处理,转发至后台服务器。
3.记录tcp、udp流量分别是由哪台服务器处理,后续该请求连接的流量都通过该服务器处理。
4.支持四层的软件:
lvs:重量级四层负载均衡器。
Nginx:轻量级四层负载均衡器,可缓存。(nginx四层是通过upstream模块)
Haproxy:模拟四层转发。
七层负载均衡
1.通过虚拟ur|或主机ip进行流量识别,根据应用层信息进行解析,决定是否需要进行负载均衡。
2.代理后台服务器与客户端建立连接,如nginx可代理前后端,与前端客户端tcp连接,与后端服务器建立 tcp连接,
3.支持7层代理的软件:
Nginx:基于http协议(nginx七层是通过proxy_pass)
Haproxy:七层代理,会话保持、标记、路径转移等。
四层负载均衡和七层负载均衡的区别
所谓的四到七层负载均衡,就是在对后台的服务器进行负载均衡时,依据四层的信息或七层的信息来决 定怎么样转发流量
四层的负载均衡,就是通过发布三层的IP地址(VIP),然后加四层的端口号,来决定哪些流量需要做负 载均衡,对需要处理的流量进行NAT处理,转发至后台服务器,并记录下这个TCP或者UDP的流量是由哪 台服务器处理的,后续这个连接的所有流量都同样转发到同一台服务器处理
七层的负载均衡,就是在四层的基础上(没有四层是绝对不可能有七层的),再考虑应用层的特征,比 如同一个Web服务器的负载均衡,除了根据VIP加80端口辨别是否需要处理的流量,还可根据七层的 URL、浏览器类别、语言来决定是否要进行负载均衡。
1.分层位置:四层负载均衡在传输层及以下,七层负载均衡在应用层及以下
2.性能 :四层负载均衡架构无需解析报文消息内容,在网络吞吐量与处理能力上较高:七层 可支持解析应用 层报文消息内容,识别URL、Cookie、HTTP header等信息。
3.原理 :四层负载均衡是基于ip+port;七层是基于虚拟的URL或主机IP等。
4.功能类比:四层负载均衡类似于路由器;七层类似于代理服务器。
5.安全性:四层负载均衡无法识别DDoS攻击;七层可防御SYN Cookie/Flood攻击
HAProxy 的这些特性使其成为许多高流量网站和服务的理想选择。它不仅能够处理大量的并发连接,还提供了丰富的配置选项来满足特定的需求。
安装与配置
官方下载
https://github.com/haproxy/wiki/wiki/Packages
yum库安装
yum install haproxy -y
查看当前版本
haproxy -v
配置文件地址
/etc/haproxy/haproxy.cfg
HAProxy 是一个非常强大和灵活的工具,适合于各种规模的应用程序和服务。它可以作为单机部署,也可以作为高可用集群的一部分。由于其丰富的特性和良好的社区支持,HAProxy 在行业中非常受欢迎。
haproxy实验环境的配置
将webserver1和webserver2的文件导入nginx的默认发布文件
webserver1
echo webserver1:172.25.254.10 > usr/share/nginx/html/index.html
webserver2
echo webserver2:172.25.254.20 > usr/share/nginx/html/index.html
在webserver1和webserver2上分别打开nginx的服务
systemctl enable --now nginx
systemctl status nginx.service
在本机上进行测试
一、haproxy的基本部署方法以及配置负载均衡
haproxy主机上安装haproxy(需配置网络源,如阿里清华)
yum install haproxy -y
查看haproxy的配置文件并进行配置
vim /etc/haproxy/haproxy.cfg
将以下内容写入配置文件中
重启服务
systemctl restart haproxy.service
systemctl enable haproxy.service
二、haproxy的全局配置参数以及日志分离
查看haproxy的默认进程
[root@haproxy ~]# pstree -p | grep haproxy
多进程设置
vim /etc/haproxy/haproxy.cfg
将nbproc 2
添加到配置文件的global中,代表启用多线程
重启服务查看
[root@haproxy ~]# systemctl restart haproxy.service
[root@haproxy ~]# pstree -p | grep haproxy
再将cpu-map 1 0 和cpu-map 2 1添加到配置文件的global中
[root@haproxy ~]# systemctl restart haproxy.service
[root@haproxy ~]# pstree -p | grep haproxy
查看子进程
同时设定多线程和多进程,会产生互斥
[root@haproxy ~]# systemctl restart haproxy.service
[root@haproxy ~]# pstree -p | grep haproxy
自定义haproxy日志
[root@haproxy ~]# vim /etc/rsyslog.conf
将这两行取消注释,同时打开udp端口
module(load="imudp") # needs to be done just once
input(type="imudp" port="514")
[root@haproxy ~]# ll /var/log/haproxy.log
[root@haproxy ~]# systemctl restart rsyslog.service
当然可以。下面是根据您提供的信息重构后的描述:
三、HAProxy 负载均衡算法
HAProxy 支持多种负载均衡算法,这些算法可以根据不同的应用场景来选择。
静态算法
- static-rr:
- 基于权重的轮询调度算法,仅支持0和1的权重设置。
- 不支持运行时动态调整权重,也不考虑后端服务器的实际负载情况。
- 适合简单、稳定的负载均衡场景。
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
更改配置
listen webcluster
bind *:80
mode http
#balance first
balance static-rr
server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
[root@test ~]# 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
- first:
- 按照服务器列表顺序进行调度。
- 当第一台服务器达到连接上限时,请求才会被分配给下一台服务器。
- 忽略权重设置,适用于节约成本的场景。
动态算法
- roundrobin:
- 最常用的基于权重的轮询调度算法。
- 支持运行时权重调整和慢启动。
- 每个后端最多支持4095个 real server,适用于大多数 web 应用场景。
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
更改配置
listen webcluster
bind *:80
mode http
balance roundrobin
server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
-
leastconn:
- 加权最少连接数调度算法。
- 优先将请求分配给当前连接数最少的服务器。
- 支持权重调整和慢启动,特别适合长连接场景,如数据库服务器。
-
random:
- 基于随机数的调度算法。
- 支持权重调整,权重较高的服务器有更高的概率接收新请求。
- 适用于需要 session 共享且服务器数量较多的场景。
其他算法
- source:
- 基于客户端源地址 hash 的调度。
- 后续来自同一个源地址的请求会被转发到同一个后端服务器。
- 适用于基于客户端公网 IP 的会话保持。
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
修改配置
listen webcluster
bind *:80
mode http
balance source
server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
[root@test ~]# 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
-
map-based:
- 对源地址进行 hash 计算,然后根据服务器总权重取模决定请求转发。
- 适用于需要精确控制调度逻辑的场景。
-
一致性哈希:
- 在哈希环上进行调度。
- 当服务器数量变化时,只影响局部请求的分配。
- 支持动态权重调整和慢启动,适用于需要动态调整后端服务器的场景。
-
uri:
- 基于用户请求的 URI 进行 hash。
- 适用于缓存服务器场景。
-
url_param:
- 基于 URL 参数进行 hash。
- 可以实现 session 保持。
-
hdr:
- 基于 HTTP 请求头部进行 hash。
- 适用于需要基于特定头部信息进行负载均衡的场景。
算法使用场景
- first:适用于节约成本的场景。
- static-rr 和 roundrobin:适用于需要 session 共享的 web 集群。
- random:适用于需要 session 共享且服务器数量较多的场景。
- leastconn:适用于数据库等长连接场景。
- source:适用于基于客户端公网 IP 的会话保持。
- uri、url_param 和 hdr:适用于特定的应用层负载均衡需求。
通过选择合适的负载均衡算法,可以根据具体的应用场景优化 HAProxy 的性能和稳定性。
四、haproxy的热更新方法
在haproxy上安装socat
yum install socat -y
修改配置文件
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
stats socket /var/lib/haproxy/stats mode 600 level admin
查看haproxy状态
[root@haproxy ~]# echo "show info" | socat stdio /var/lib/haproxy/stats
Name: HAProxy
Version: 2.4.22-f8e3218
Release_date: 2023/02/14
Nbthread: 1
Nbproc: 1
Process_num: 1
Pid: 3352
Uptime: 0d 0h03m43s
Uptime_sec: 223
Memmax_MB: 0
PoolAlloc_MB: 0
查看集群状态
[root@haproxy ~]# echo "show servers state" | socat stdio /var/lib/haproxy/stats
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
2 webcluster 1 web1 172.25.254.20 2 0 2 2 188 6 3 7 6 0 0 0 - 80 - 0 0 - - 0
2 webcluster 2 web2 172.25.254.30 2 0 1 1 188 6 3 7 6 0 0 0 - 80 - 0 0 - - 0
4 static 1 static 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 4331 - 0 0 - - 0
5 app 1 app1 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 5001 - 0 0 - - 0
5 app 2 app2 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 5002 - 0 0 - - 0
5 app 3 app3 127.0.0.1 0 0 1 1 186 8 2 0 6 0 0 0 - 5003 - 0 0 - - 0
5 app 4 app4 127.0.0.1 0 0 1 1 186 8 2 0 6 0 0 0 - 5004 - 0 0 - - 0
查看集群权重
[root@haproxy ~]# echo get weight webcluster/web1 | socat stdio
/var/lib/haproxy/stats
2 (initial 2)
[root@haproxy ~]# echo get weight webcluster/web2 | socat stdio
/var/lib/haproxy/stats
1 (initial 1)
设置权重
[root@haproxy ~]# echo "set weight webcluster/web1 1 " | socat stdio
/var/lib/haproxy/stats
[root@haproxy ~]# echo "set weight webcluster/web1 2 " | socat stdio
/var/lib/haproxy/stats
下线后端服务器
[root@haproxy ~]# echo "disable server webcluster/web1 " | socat stdio
/var/lib/haproxy/stats
上线后端服务器
[root@haproxy ~]# echo "enable server webcluster/web1 " | socat stdio
/var/lib/haproxy/stats
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
stats socket /var/lib/haproxy/stats1 mode 600 level admin process 1
stats socket /var/lib/haproxy/stats2 mode 600 level admin process 2
nbproc 2
cpu-map 1 0
cpu-map 2 1
每个进程会有单独的sock文件来进行单独管理
[root@haproxy ~]# ll /var/lib/haproxy/
总用量 0
srw------- 1 root root 0 8月 11 13:43 stats
srw------- 1 root root 0 8月 11 13:46 stats1
srw------- 1 root root 0 8月 11 13:46 stats2
五、haproxy的状态页面监控
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen stats
mode http
bind *:443
stats enable
stats refresh 5
stats uri /status
stats auth ding:ding
[root@haproxy ~]# systemctl restart haproxy.service
六、cookie的会话保持
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode http
balance roundrobin
cookie WEBCOOKIE insert nocache indirect
server web1 172.25.254.10:80 cookie ding1 check inter 2 fall 3 rise 5 weight 1
server web2 172.25.254.20:80 cookie ding2 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
七、haproxy-ip透传
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode http
#balance first
#balance static-rr
balance roundrobin
#balance leastconn
#balance source
#balance uri
#balance url_param name,userid
#balance hdr(User-Agent)
#hash-type consistent
# redirect prefix http://www.baidu.com/
#cookie WEBCOOKIE insert nocache indirect
server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 1
server web2 172.25.254.20:80 send-proxy check inter 2 fall 3 rise 5 weight 1
# server web_sorry 172.25.254.100:8080 backup
[root@haproxy ~]# systemctl restart haproxy.service
[root@webserver1 ~]# systemctl disable nginx.service
[root@webserver1 ~]# systemctl stop nginx.service
[root@webserver1 ~]# dnf install httpd -y
[root@webserver1 ~]# echo web1 - 172.25.254.10 > /var/www/html/index.html
[root@webserver1 ~]# systemctl start httpd
[root@webserver1 ~]# vi /etc/httpd/conf/httpd.conf
配置web服务器,记录负载均衡透传的客户端IP地址
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
[root@webserver1 ~]# systemctl restart httpd
本机测试
[C:\~]$ curl 172.25.254.100
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 25 100 25 0 0 10794 0 --:--:-- --:--:-- --:--:-- 12500
webserver2:172.25.254.20
[C:\~]$ curl 172.25.254.100
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 21 100 21 0 0 6231 0 --:--:-- --:--:-- --:--:-- 7000
web1 - 172.25.254.10
[C:\~]$ curl 172.25.254.100
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 21 100 21 0 0 7996 0 --:--:-- --:--:-- --:--:-- 10500
web1 - 172.25.254.10
[root@webserver1 ~]# tail /etc/httpd/logs/access_log
172.25.254.1 172.25.254.100 - - [10/Aug/2024:22:39:31 +0800] "GET / HTTP/1.1" 200 27 "-" "curl/8.7.1"
172.25.254.1 172.25.254.100 - - [10/Aug/2024:22:41:33 +0800] "GET / HTTP/1.1" 200 27 "-" "curl/8.7.1"
172.25.254.1 172.25.254.100 - - [10/Aug/2024:22:41:34 +0800] "GET / HTTP/1.1" 200 27 "-" "curl/8.7.1"
八、haproxy中访问控制列表
[root@haproxy ~]# vim/etc/haproxy/haproxy.cfg
frontend webcluster
bind *:80
mode http
acl test hdr_dom(host) -i www.timingding.org
use_backend webcluster-host if test
default_backend default-host
backend webcluster-host
mode http
server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
backend default-host
mode http
server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
[root@haproxy ~]# systemctl restart haproxy.service
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@haproxy ~]# curl www.timingding.org
webserver1 - 172.25.254.10
需要做域名解析,否则无法通过域名访问。
[root@haproxy ~]# vim/etc/haproxy/haproxy.cfg
frontend webcluster
bind *:80
mode http
acl test base_sub -m ding
use_backend webcluster-host if test
default_backend default-host
backend webcluster-host
mode http
server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
backend default-host
mode http
server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
[root@haproxy ~]# systemctl restart haproxy.service
创建子目录测试:
[root@webserver1 ~]# systemctl restart nginx.service
[root@webserver1 ~]# mkdir /usr/share/nginx/html/ding -p
[root@webserver1 ~]# echo 172.25.254.10 ding > /usr/share/nginx/html/ding/index.html
九、haproxy的访问控制列表
[root@haproxy ~]# vim/etc/haproxy/haproxy.cfg
frontend webcluster
bind *:80
mode http
acl test hdr_dom(host) -i www.abc.org
use_backend webcluster-host if test
default_backend default-host
backend webcluster-host
mode http
server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
backend default-host
mode http
server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
[root@haproxy ~]# systemctl restart haproxy.service
十、重定向错误页面
[root@webserver1 ~]# systemctl stop httpd.service
[root@webserver2 ~]# systemctl stop nginx.service
[root@haproxy ~]# mkdir /etc/haproxy/errorpage -p
[root@haproxy ~]# vim /etc/haproxy/errorpage/503.http
HTTP/1.0 503 Service Unavailable
Cache-Control: no-cache
Connection: close
Content-Type: text/html;charset=UTF-8
<html><body><h1>什么动物生气最安静</h1>
大猩猩!!
</body></html>
[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
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
errorfile 503 /etc/haproxy/errorpage/503.http
[root@haproxy ~]# systemctl restart haproxy.service