HAproxy负载均衡详细介绍
目录
一、概述
二、HAProxy特点和优点:
三、HAProxy保持会话的三种解决方法
四、HAProxy的balance 8种负载均衡算法
1、RR(Round Robin)
2、LC(Least Connections)
3、SH(Source Hashing)
4、uri(资源标识符)
5、url_param(资源定位符)
7、 source
8、 static-rr
五、HAProxy 主要工作模式
六、 HAProxy 安装
七、状态统计功能测试
一、概述
HAProxy 提供高可用性、负载均衡以及基于TCP和HTTP的应用代理,支持虚拟主机,它是免费、快速并且可靠的一种负载均衡解决方案。适合处理高负载站点的 七层 数据请求。类似的代理服务可以屏蔽内部真实服务器,防止内部服务器遭受攻击。
二、HAProxy特点和优点:
-
支持原生SSL,同时支持客户端和服务器的SSL.
-
支持IPv6和UNIX套字节(sockets)
-
支持HTTP Keep-Alive
-
支持HTTP/1.1压缩,节省宽带
-
支持优化健康检测机制(SSL、scripted TCP、check agent…)
-
支持7层负载均衡。
-
可靠性和稳定性非常好。
-
并发连接 40000-50000个,单位时间处理最大请求 20000个,最大数据处理10Gbps.
-
支持8种负载均衡算法,同时支持session保持。
-
支持虚拟主机。
-
支持连接拒绝、全透明代理。
-
拥有服务器状态监控页面。
-
支持ACL(access control list)。
三、HAProxy保持会话的三种解决方法
HAProxy为了让同一客户端访问服务器可以保持会话。有三种解决方法:客户端IP、Cookie以及Session。
-
通过 客户端IP 进行Hash计算并保存,以此确保当相同IP访问代理服务器可以转发给固定的真实服务器。
-
依靠真实服务器发送客户端的 Cookie信息 进行会话保持。
-
将保存真实服务器的 Session 以及服务器标识 ,实现会话保持。
(HAProxy只要求后端服务器能够在网络联通,也没有像LVS那样繁琐的ARP配置)
四、HAProxy的balance 8种负载均衡算法
1、RR(Round Robin)
RR算法是最简单最常用的一种算法,即轮询调度
2、LC(Least Connections)
最小连接数算法,根据后端的节点连接数大小动态分配前端请求
3、SH(Source Hashing)
基于来源访问调度算法,用于一些有Session会话记录在服务端的场景,可以基于来源的IP、Cookie等做集群调度
4、uri(资源标识符)
表示根据请求的URI,做cdn(内容分发网络)需使用
5、url_param(资源定位符)
表示根据HTTP请求头来锁定每 一 次HTTP请求。
6、rdp—cookie(name)
表示根据据cookie (name)来锁定并哈希每一次TCP请求。
7、 source
表示根据请求的源IP,类似Nginx的IP hash机制。
8、 static-rr
表示根据权重,轮询
#注意:haproxy中的算法需要写全称,不能写缩写。
五、HAProxy 主要工作模式
tcp模式:在客户端和服务器之间将建立一个全双工的连接,且不会对7层的报文做任何处理的简单模式。 通常用于SSL、SSH、SMTP等应用层。
http模式(一般使用):客户端请求在转发给后端服务器之前会被深度分析,所有不与RFC格式兼容的请求都会被拒绝。
六、 HAProxy 安装
环境准备
[root@proxy1 ~]# systemctl stop firewalld
[root@proxy1 ~]# setenforce 0
yum安装
[root@proxy1 ~]# yum install -y haproxy
Last metadata expiration check: 0:11:18 ago on 2025年11月03日 星期一 14时40分03秒.
Dependencies resolved.
====================================================================================================================================================Package Architecture Version Repository Size
====================================================================================================================================================
Installing:haproxy x86_64 2.9.5-12.oe2403sp1 update 1.3 MTransaction Summary
====================================================================================================================================================
Install 1 PackageTotal download size: 1.3 M
Installed size: 3.4 M
Downloading Packages:
haproxy-2.9.5-12.oe2403sp1.x86_64.rpm 819 kB/s | 1.3 MB 00:01
----------------------------------------------------------------------------------------------------------------------------------------------------
Total 642 kB/s | 1.3 MB 00:02
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transactionPreparing : 1/1 Running scriptlet: haproxy-2.9.5-12.oe2403sp1.x86_64 1/1 Installing : haproxy-2.9.5-12.oe2403sp1.x86_64 1/1 Running scriptlet: haproxy-2.9.5-12.oe2403sp1.x86_64 1/1 Verifying : haproxy-2.9.5-12.oe2403sp1.x86_64 1/1 Installed:haproxy-2.9.5-12.oe2403sp1.x86_64 Complete!
配置文件
默认地址:/etc/haproxy/haproxy.cfg
haproxy.cfg 由两大部分组成,分别是 global 和 proxies 部分。
global:全局配置:
进程及安全配置相关的参数
性能调整相关参数
Debug参数
proxies:代理配置
defaults:为 frontend, backend, listen提供默认配置
frontend:前端,相当于 nginx 中的 server {}
backend:后端,相当于 nginx 中的 upstream {}
listen:同时拥有 前端和后端配置
globallog 127.0.0.1 local2chroot /var/lib/haproxypidfile /var/run/haproxy.piduser haproxygroup haproxydaemonmaxconn 4000defaultsmode httplog globaloption httplogoption dontlognullretries 3timeout http-request 5stimeout queue 1mtimeout connect 5stimeout client 1mtimeout server 1mtimeout http-keep-alive 5stimeout check 5smaxconn 3000frontend mainbind *:80default_backend http_backbackend http_backbalance roundrobinserver node1 192.168.150.41:80 checkserver node2 192.168.150.42:80 check
启动服务
[root@proxy1 haproxy]# systemctl start haproxy.service
Job for haproxy.service failed because the control process exited with error code.
See "systemctl status haproxy.service" and "journalctl -xeu haproxy.service" for details.
发现服务启动失败


通过查看日志和端口状态信息,发现80端口被nginx占用,导致haproxy服务启动失败。
[root@proxy1 haproxy]# systemctl stop nginx
[root@proxy1 haproxy]# netstat -anptu |grep :80
[root@proxy1 haproxy]# systemctl start haproxy.service
停掉nginx服务后,重新启动haproxy,显示成功。
测试haproxy的负载均衡


七、状态统计功能测试
listen stats #定义监控页面 mode httpoption httplogbind *:1080 #绑定端口1080 stats refresh 30s #每30秒更新监控数据 stats uri /stats #访问监控页面的uri stats realm HAProxy\ Stats #监控页面的认证提示 stats auth admin:admin #监控页面的用户名和密码
###只有七层才能配置
在配置文件中添加以上字段
重启服务测试

