HAProxy负载均衡

HAProxy负载均衡

1、什么是HAProxy

​ HAProxy 是一款提供高可用性、负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件(负载均衡器),支持虚拟主机,它是免费、快速并且可靠的一种解决方案。 HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在时下的硬件上,完全可以支持数以万计的 并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。

2、HAProxy的作用

​ HAProxy的作用实现负载均衡,将请求分发到多个服务器上,从而提高系统的可用性和性能。它可以根据不同的负载均衡算法(如轮询、加权轮询、IP哈希等)来分配请求,并支持会话保持、健康检查、SSL终止等功能。此外,HAProxy还可以实现反向代理、SSL加速、HTTP压缩等功能,使其成为一款功能强大的代理软件。

3、HAProxy的优缺点

HAProxy的优点
  1. 高性能:HAProxy是一个高性能的负载均衡器,能够处理大量的并发连接和高流量。
  2. 可靠性:HAProxy具有高可靠性和可用性,支持故障转移和负载均衡,可以确保服务的连续性。
  3. 灵活性:HAProxy支持多种负载均衡算法,可以根据需求进行配置,如轮询、最小连接数、IP散列等。
  4. 可扩展性:HAProxy可以水平扩展,支持多个后端服务器,可以根据需要增加或减少服务器数量。
  5. 安全性:HAProxy支持SSL终止和SSL加速,可以提供安全的通信渠道。
  6. 监控和日志:HAProxy提供详细的监控和日志功能,可以实时监控负载均衡器的性能和状态。
HAProxy的缺点
  1. 学习曲线:HAProxy的配置相对复杂,需要一定的学习和经验才能正确配置和管理。
  2. 单点故障:HAProxy本身是一个单点故障,如果负载均衡器出现故障,会影响整个应用的可用性。
  3. 限制:HAProxy在处理大文件传输和高并发请求时可能会受到性能限制。
  4. 功能限制:相对于其他负载均衡器,HAProxy的功能相对较少,不支持一些高级功能,如内容缓存等。
  5. 可视化界面:HAProxy没有官方提供的可视化界面,需要通过命令行或第三方工具进行配置和管理。

4、HAProxy的工作原理

  1. 客户端向HAProxy发送请求。
  2. HAProxy接收到请求后,根据预先配置的负载均衡算法,选择一个后端服务器来处理请求。
  3. HAProxy将请求转发给选定的后端服务器。
  4. 后端服务器处理请求并将响应发送回HAProxy。
  5. HAProxy将响应转发给客户端。

5、HAProxy部署http负载均衡

环境说明:

IP版本
haproxy192.168.200.42redhat8
rs1192.168.200.43redhat8
rs2192.168.200.44redhat8
#关闭防火墙和selinux
[root@haproxy ~]# systemctl disable --now firewalld
[root@haproxy ~]# sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
[root@haproxy ~]# setenforce 0

[root@rs1 ~]# systemctl disable --now firewalld
[root@rs1 ~]# sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
[root@rs1 ~]# setenforce 0

[root@rs2 ~]# systemctl disable --now firewalld
[root@rs2 ~]# sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
[root@rs2 ~]# setenforce 0

#rs上配置http并启动
[root@rs1 ~]# yum -y install httpd
[root@rs1 ~]# echo 'RS1' > /var/www/html/index.html
[root@rs1 ~]# systemctl enable --now httpd
[root@rs1 ~]# ss -antl
State              Recv-Q             Send-Q                         Local Address:Port                         Peer Address:Port            
LISTEN             0                  128                                  0.0.0.0:22                                0.0.0.0:*               
LISTEN             0                  128                                        *:80                                      *:*               
LISTEN             0                  128                                     [::]:22                                   [::]:*       
[root@rs2 ~]# yum -y install httpd
[root@rs2 ~]# echo 'RS2' > /var/www/html/index.html
[root@rs2 ~]# systemctl enable --now httpd
[root@rs2 ~]# ss -antl
State              Recv-Q             Send-Q                         Local Address:Port                         Peer Address:Port            
LISTEN             0                  128                                  0.0.0.0:22                                0.0.0.0:*               
LISTEN             0                  128                                        *:80                                      *:*               
LISTEN             0                  128                                     [::]:22                                   [::]:*       

在这里插入图片描述
在这里插入图片描述

#将rs2的httpd服务端口80更改为8080,测试实验效果
[root@rs2 ~]# vi /etc/httpd/conf/httpd.conf
#Listen 12.34.56.78:80
Listen 8080
[root@rs2 ~]# systemctl restart httpd
[root@rs2 ~]# ss -antl
State              Recv-Q             Send-Q                         Local Address:Port                         Peer Address:Port            
LISTEN             0                  128                                  0.0.0.0:22                                0.0.0.0:*               
LISTEN             0                  128                                        *:8080                                    *:*               
LISTEN             0                  128                                     [::]:22                                   [::]:*    

在这里插入图片描述

#先装依赖包
[root@haproxy ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel vim wget

#安装haproxy并创建用户
[root@haproxy ~]#  useradd -r -M -s /sbin/nologin haproxy
 
#在haproxy官网下载软件包

在这里插入图片描述
在这里插入图片描述

[root@haproxy ~]# wget https://www.haproxy.org/download/2.7/src/haproxy-2.7.10.tar.gz
[root@haproxy ~]# ls
anaconda-ks.cfg  haproxy-2.7.10.tar.gz
#解压
[root@haproxy ~]# tar xf haproxy-2.7.10.tar.gz
[root@haproxy ~]# ls
anaconda-ks.cfg  haproxy-2.7.10  haproxy-2.7.10.tar.gz
[root@haproxy ~]# cd haproxy-2.7.10
[root@haproxy haproxy-2.7.10]# make clean   //该命令用于清理之前编译的参数
[root@haproxy ~]# cd haproxy-2.7.10  
[root@haproxy haproxy-2.7.10]# make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 USE_SYSTEMD=1
#指定路径进行安装
[root@haproxy haproxy-2.7.10]# make install PREFIX=/usr/local/haproxy
[root@haproxy haproxy-2.7.10]# ls /usr/local/
bin  etc  games  haproxy  include  lib  lib64  libexec  sbin  share  src
[root@haproxy haproxy-2.7.10]# ls /usr/local/haproxy/
doc  sbin  share

#通过软链接的方式设置环境变量
[root@haproxy haproxy-2.7.10]# cd /usr/local/haproxy/
[root@haproxy haproxy]# ln -s /usr/local/haproxy/sbin/* /usr/sbin/
[root@haproxy haproxy]# which haproxy 
/usr/sbin/haproxy

[root@haproxy haproxy]# haproxy -v
HAProxy version 2.7.10-d796057 2023/08/09 - https://haproxy.org/
Status: stable branch - will stop receiving fixes around Q1 2024.
Known bugs: http://www.haproxy.org/bugs/bugs-2.7.10.html
Running on: Linux 4.18.0-193.el8.x86_64 #1 SMP Fri Mar 27 14:35:58 UTC 2020 x86_64
#配置各个负载的内核参数
[root@haproxy haproxy]# echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
[root@haproxy haproxy]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@haproxy haproxy]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
#编写haproxys.service文件
[root@haproxy ~]# vim   /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target

[root@haproxy ~]# systemctl daemon-reload

#启动日志
配置日志记录功能
[root@haproxy ~]# vim /etc/rsyslog.conf
# Save boot messages also to boot.log
local7.*                                                /var/log/boot.log
local0.*        /var/log/haproxy.log  //添加此行

#重启日志服务
[root@haproxy ~]# systemctl restart rsyslog.service

#提供配置文件
[root@haproxy ~]# mkdir /etc/haproxy
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
[root@haproxy ~]# cat /etc/haproxy/haproxy.cfg 
#--------------全局配置----------------
global
    log 127.0.0.1 local0  info
    #log loghost local0 info
    maxconn 20480
#chroot /usr/local/haproxy
    pidfile /var/run/haproxy.pid
    #maxconn 4000
    user haproxy
    group haproxy
    daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode http
    log global
    option dontlognull
    option httpclose
    option httplog
    #option forwardfor
    option redispatch
    balance roundrobin
    timeout connect 10s
    timeout client 10s
    timeout server 10s
    timeout check 10s
    maxconn 60000
    retries 3
#--------------统计页面配置------------------
listen admin_stats
    bind 0.0.0.0:8189
    stats enable
    mode http
    log global
    stats uri /haproxy_stats  //访问状态页面的URI
    stats realm Haproxy\ Statistics
    stats auth admin:admin  //登录状态页面的用户名和密码,可自行修改
    #stats hide-version
    stats admin if TRUE
    stats refresh 30s
#---------------web设置-----------------------
listen webcluster
    bind 0.0.0.0:80
    mode http
    #option httpchk GET /index.html
    log global
    maxconn 3000
    balance roundrobin
    cookie SESSION_COOKIE insert indirect nocache
    server rs1 192.168.200.43:80 check inter 2000 fall 5  //加入做负载均衡的主机
    server rs2 192.168.200.44:8080 check inter 2000 fall 5 //加入做负载均衡的主机

//重启haproxy服务,并将haproxy服务设置开机自启
[root@haproxy ~]# systemctl restart haproxy
[root@haproxy ~]# systemctl enable --now  haproxy.service
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.
[root@haproxy ~]# systemctl status haproxy.service
● haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabl>
   Active: active (running) since Tue 2023-10-10 21:41:07 CST; 21s ago
  Process: 16679 ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q (code=ex>
 Main PID: 16683 (haproxy)

#查看端口
[root@haproxy ~]# ss -antl
State      Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     
LISTEN     0           128                    0.0.0.0:80                  0.0.0.0:*        
LISTEN     0           128                    0.0.0.0:22                  0.0.0.0:*        
LISTEN     0           128                    0.0.0.0:8189                0.0.0.0:*        
LISTEN     0           128                       [::]:22                     [::]:*        

访问负载均衡器的http页面测试

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值