haproxy

HAProxy(High Availability Proxy)是一款高性能的负载均衡和反向代理软件,它基于事件驱动、单进程模型设计,能够在TCP/UDP层面以及HTTP(S)等应用层协议上实现高效的流量分发。以下是关于HAProxy的详细介绍:

一、HAProxy的主要特点

  1. 高性能
    • HAProxy通过优化的事件驱动引擎,能够以最小的系统资源开销处理大量并发请求。它支持高达数百万级别的并发连接,并具有极低的延迟。
    • 采用C语言编写,能够高效地处理网络请求,适用于高负载环境。
  2. 高可用性
    • 支持健康检查和故障恢复机制,能够自动检测到不可用的服务器并将流量转发到可用的服务器上,确保服务连续性。
    • 支持所有主服务器故障切换至备用服务器,实现高可用性。
  3. 灵活性
    • 支持多种负载均衡算法,如轮询、最少连接、源IP哈希等,可根据实际业务需求灵活配置。
    • 可以在双向添加、修改或删除HTTP报文首部字段,支持响应报文压缩。
  4. 安全性
    • 可以作为反向代理,隐藏后端服务器的真实IP地址,提高系统的安全性。
    • 支持SSL/TLS服务器,可以对HTTPS请求进行加密处理,保护数据传输安全。

二、HAProxy的适用场景

  • Web服务器负载均衡:将客户端请求分发到多个Web服务器上,提高网站的并发处理能力和响应速度。
  • 数据库负载均衡:对数据库访问请求进行负载均衡,确保数据库服务器的负载均衡和高可用性。
  • 邮件服务器负载均衡:对邮件服务器的访问请求进行负载均衡,提高邮件系统的稳定性和响应速度。
  • 缓存服务器负载均衡:对缓存服务器的访问请求进行负载均衡,提高缓存系统的命中率和性能。

三、HAProxy的配置与部署

HAProxy的配置文件通常包括三个部分:global(全局配置)、defaults(默认配置)和listen(应用组件配置)。

  • global配置:设置全局参数,如日志记录、最大连接数、用户ID等。
  • defaults配置:设置默认参数,如日志格式、模式、重试次数等。
  • listen配置:定义应用组件,包括前端和后端服务器的详细信息,以及负载均衡算法等。

在部署HAProxy时,需要确保所有后端服务器都能够正常访问,并且HAProxy的配置文件已经正确设置。然后,启动HAProxy实例并监听指定的端口,即可开始接受客户端请求并进行负载均衡处理。

四、HAProxy的优缺点

优点

  • 高性能:能够处理大量的并发连接。
  • 灵活性:支持多种负载均衡算法和配置选项。
  • 高可用性:具备健康检查和故障恢复机制。
  • 安全性:可作为反向代理隐藏后端服务器。

缺点

  • 单点故障:如果HAProxy本身出现故障,可能会导致整个系统的服务不可用。
  • 配置复杂:HAProxy的配置相对复杂,需要一定的学习成本。
  • 性能瓶颈:虽然HAProxy的性能很高,但在处理极端大量并发连接时,可能会成为系统的性能瓶颈。
  • 功能单一:只支持做负载均衡的调度服务器,不支持正则处理、动静分离等高级功能。

所以,HAProxy是一款功能强大、性能卓越的高性能负载均衡和反向代理软件,适用于各种高负载、高可用性的网络服务场景。然而,在使用时也需要注意其可能存在的缺点,并采取相应的措施来避免潜在的风险。

软件下载

yum install haproxy -y
#查看版本
haproxy -v
HAProxy version 2.9.9-ad75c48 2024/06/14 - https://haproxy.org/
Status: stable branch - will stop receiving fixes around Q1 2025.
Known bugs: http://www.haproxy.org/bugs/bugs-2.9.9.html
Running on: Linux 3.10.0-1160.el7.x86_64 #1 SMP Tue Aug 18 14:50:17 EDT 2020
x86_64

实验环境的搭建

haproxy:

root@localhost ~]#systemctl stop firewalld.service 
[root@localhost ~]#systemctl mask firewalld.service
[root@localhost ~]# vmset.sh eth0 172.25.254.100 haproxy.huihui.org

webserver1:

[root@localhost ~]#vmset.sh eth0 172.25.254.10 web.hui.org
[root@localhost ~]#dnf install nginx -y  

[root@localhost ~]#yum install httpd -y        
[root@localhost ~]#echo web1 - 172.25.254.10 > /usr/share/nginx/html/index.html 
[root@localhost ~]#systemctl enable --now httpd

webserver2:

[root@localhost ~]#vmset.sh eth0 172.25.254.20 web.hui.org
[root@localhost ~]#dnf install nginx -y            
[root@localhost ~]#yum install httpd -y
[root@localhost ~]#echo web2 - 172.25.254.20 > /usr/share/nginx/html/index.html 
[root@localhost ~]#systemctl enable --now httpd

测试:

haproxy的基本部署方法以及负载均衡:

测试:

配置完成后启动服务

[root@haproxy ~]# systemctl restart haproxy
[root@haproxy ~]# curl 172.25.254.10
web1 - 172.25.254.10
[root@haproxy ~]# curl 172.25.254.20
web2 - 172.25.254.20

 多线程和多进程

vim /etc/haproxy/haproxy.cfg
 
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 100000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1 
stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2  #启用多个文件
 
#nbproc 2 #启用多进程
#cpu-map 1 0 
#cpu-map 2 1
nbthread 2
 
systemctl restart haproxy.service

自定义日志:

[root@haproxy ~]#vim /etc/rsyslog.conf

local2.*                         /var/log/haproxy.log

[root@haproxy ~]#systemctl restart rsyslog.service 
[root@haproxy ~]#systemctl restart haproxy.service 
[root@haproxy ~]#ll /var/log/haproxy.log

proxies的常用配置参数:

[root@haproxy ~]#dnf install httpd

[root@haproxy ~]#vim /etc/httpd/conf/httpd.conf

修改Listen 8080

[root@haproxy ~]#systemctl enable --now httpd

[root@haproxy ~]#echo sorry 你今天开心了吗? > /var/www/html/index.html

[root@haproxy ~]#vim /etc/haproxy/haproxy.cfg

root@haproxy ~]#systemctl restart haproxy.service #重启haproxy的服务

在web1、web2关闭http、nginx服务

[root@webserver ~]#systemctl stop nginx

[root@webserver2 ~]#systemctl stop http

再去haproxy机子上测试一下

访问重定向:

[root@haproxy ~]#vim /etc/haproxy/haproxy.cfg

 

 [root@haproxy ~]# systemctl restart haproxy.service

然后在网页中输入地址,就会跳转到百度

HAProxy的算法

static-rr:静态轮询

listen webserver 80
    bind *:80
    mode http
    balance static-rr
    server webserver1 172.25.254.10:80 weight 1 check inter 3 fall 3 rise 5
    server webserver2 172.25.254.20:80 weight 1 check inter 3 fall 3 rise 5

Round Robin:轮询

listen stats
    bind *:80
    mode http
    balance roundrobin
    server webserver1 172.25.254.10:80 weight 1 check inter 2 fall 3 rise 5
    server webserver2 172.25.254.20:80 weight 1 check inter 2 fall 3 rise 5

Leastconn:最少连接

listen stats
    bind *:80
    mode http
    balance leastconn
    server webserver1 172.25.254.10:80 weight 1 check inter 2 fall 3 rise 5
    server webserver2 172.25.254.20:80 weight 1 check inter 2 fall 3 rise 5

First:首选

listen stats
    bind *:80
    mode http
    balance first
    server webserver1 172.25.254.10:80 weight 1 check inter 2 fall 3 rise 5
    server webserver2 172.25.254.20:80 weight 1 check inter 2 fall 3 rise 5 

Source:源地址

listen stats
    bind *:80
    mode http
    balance source
    server webserver1 172.25.254.10:80 weight 1 check inter 2 fall 3 rise 5
    server webserver2 172.25.254.20:80 weight 1 check inter 2 fall 3 rise 5

URI:请求URI

listen stats
    bind *:80
    mode http
    balance uri
    server webserver1 172.25.254.10:80 weight 1 check inter 2 fall 3 rise 5
    server webserver2 172.25.254.20:80 weight 1 check inter 2 fall 3 rise 5

UrlParam:请求URI参数

listen stats
    bind *:80
    mode http
    balance uri

    balance url_param webcluster
    server webserver1 172.25.254.10:80 weight 1 check inter 2 fall 3 rise 5
    server webserver2 172.25.254.20:80 weight 1 check inter 2 fall 3 rise 5

hash:

listen stats
    bind *:80
    mode http
    balance source

    hash-type consistent
    server webserver1 172.25.254.10:80 weight 1 check inter 2 fall 3 rise 5
    server webserver2 172.25.254.20:80 weight 1 check inter 2 fall 3 rise 5

uri+hash:

listen stats
    bind *:80
    mode http
    balance uri

    hash-type consistent
    server webserver1 172.25.254.10:80 weight 1 check inter 2 fall 3 rise 5
    server webserver2 172.25.254.20:80 weight 1 check inter 2 fall 3 rise 5

HDR:请求头

listen stats
    bind *:80
    mode http
    balance hdr(User-Agent)
    server webserver1 172.25.254.10:80 weight 1 check inter 2 fall 3 rise 5
    server webserver2 172.25.254.20:80 weight 1 check inter 2 fall 3 rise 5

一致性hash配置

listen stats
    bind *:80
    mode http
    balance hdr(User-Agent)

    hash-type consistent
    server webserver1 172.25.254.10:80 weight 1 check inter 2 fall 3 rise 5
    server webserver2 172.25.254.20:80 weight 1 check inter 2 fall 3 rise 5

rdp-cookie:Cookie

listen webcluster
    bind *:80
    mode http
    balance roundrobin

    cookie WEBCOOKIE insert nocache indirect
    server webserver1 172.25.254.10:80 cookie lee1 weight 1 check inter 2 fall 3 rise 5
    server webserver2 172.25.254.20:80 cookie lee2 weight 1 check inter 2 fall 3 rise 5

 ip穿透

1.四层穿透

listen webserver 80
bind *:80
mode tcp
balance roundrobin
server webserver1 172.25.254.10:80 weight 1 check inter 3 fa11 3 rise 5
server webserver2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5

2.七层穿透

listen webserver_80
    option forwardfor
    bind *:80
    mode http
    balance roundrobin
    server web1  172.25.254.10:80  weight 1 check inter 2 fall 3 rise 5  
    server wed2  172.25.254.20:80 send-proxy weigh 1 check inter 2 fall 3 rise 5

ACL

frontend webcluster
    bind *:80
    mode http
    acl test hdr_dom(host)  -i www.timinglee.com
    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

基于源IP或子网调度访问

haproxy配置

frontend webcluster
    bind *:80
    mode http
    acl ctrl_ip src 172.25.250.1 172.25.250.20 192.168.0.0/24
    use_backend webcluster-host if ctrl_ip
    default_backend default-host
 
backend webcluster-host
    mode http
    server web1 172.25.250.10:80 check inter 2 fall 2 rise 5
        
backend default-host
    mode http
    server web2 172.25.250.20:80 check inter 2 fall 2 rise 5

socat工具热处理haproxy:

查看权重

[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 "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 ~]#ll /var/lib/haproxy/

[root@haproxy ~]#vim /etc/haproxy/haproxy.cfg
打开配置文件,在全局配置global下写

重启服务

[root@haproxy ~]#systemctl restart haproxy.service

[root@haproxy ~]#ll /var/lib/haproxy/

  • 19
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三石Sama

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值