2025年3月14日 星期五 甲辰(龙)年 月十三 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 服务器 > 万维网络 > 中间件

Ha-proxy 负载均衡器

时间:02-24来源:作者:点击数:12
城东书院 www.cdsy.xyz

HAProxy的下载网址:Releases · haproxy/haproxy · GitHub

HAProxy是一个高性能的开源负载均衡器代理服务器,使用C语言编写,提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。其特别适用于负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy可以运行在当前的硬件上,支持数以万计的并发连接,并且能够很简单安全地整合进用户当前的架构中,同时保护web服务器不被暴露到网络上。

HAProxy的工作原理 :当客户端发起连接请求时,它将连接到HAProxy提供的IP地址和端口。HAProxy能够将传入的请求分发到多个后端服务器,并提供各种负载均衡算法,如轮询、加权轮询、最少连接等。同时,HAProxy具有高度可配置性和可定制性,适用于Web应用、数据库负载均衡、应用程序代理等场景,提供高可用性和可伸缩性。

haproxy---主要是做7层负载均衡,也可以做4层负载均衡

apache也可以做7层负载均衡,但是很麻烦。实际工作中没有人用。

负载均衡是通过OSI协议对应的

7层负载均衡:用的7层http协议,

4层负载均衡:用的是tcp协议加端口号做的负载均衡

ha-proxy的特点:

  • 功能强大的ACL支持,给用户极大的方便。
  • 免费与开源:HAProxy作为一个免费的开源软件,提供了很多商业付费软件所具备的功能。
  • 负载均衡能力:它支持L4 (TCP)和L7 (HTTP)两种负载均衡,能够根据不同需求进行选择。
  • 会话保持:HAProxy能通过多种方式保持会话,例如基于客户端IP的Hash计算,或服务器发送的cookie。
  • 虚拟主机支持:可以配置虚拟主机,以根据不同的域名或URL路径指向不同的后端服务器组。
  • 高可用性:通过健康检查和状态监控,确保服务的持续性和可靠性。
  • 多并发连接支持:HAProxy能够处理数以万计的并发连接,适用于大规模、高流量的网站和应用。
  • 拥有一个功能出色的监控页面,实时了解系统的当前状况。
  • HAProxy内部使用非阻塞事件驱动引擎,这使得它能够在高并发场景下表现出色

Haproxy 实现七层负载

实验环境:Keepalived + Haproxy

192.168.137.10 haproxy-master  
192.168.137.20 haproxy-backup  
192.168.137.30 server01  
192.168.137.40 server02  

所有主机:

  • # 四台服务器正常上网,互相可以ping通,关闭防火墙,修改主机名
  • ping www.jd.com
  • ping 192.168.137.10
  • ping 192.168.137.20
  • ping 192.168.137.30
  • ping 192.168.137.40
  • hostnamectl set-hostname 主机名
  • systemctl disable --now firewalld
  • setenforce 0

真实服务器配置:

  • # 安装 nginx
  • yum install -y nginx
  • systemctl start nginx
  • #
  • echo "test-nginx01" > /usr/share/nginx/html/index.html
  • echo "test-nginx02" > /usr/share/nginx/html/index.html
  • vim /etc/nginx/nginx.conf
  • keepalive_timeout 65;
  • systemctl restart nginx

调度器配置Haproxy(主/备):

  • yum -y install haproxy keepalived
  • # 备份:
  • cp -rf /etc/haproxy/haproxy.cfg{,.bak}
  • # 修改配置文件
  • vim /etc/haproxy/haproxy.cfg
  • #
  • global
  • log 127.0.0.1 local2 info
  • pidfile /var/run/haproxy.pid
  • maxconn 4000
  • user haproxy
  • group haproxy
  • daemon
  • nbproc 1
  • defaults
  • mode http
  • log global
  • retries 3
  • option redispatch
  • maxconn 4000
  • contimeout 5000
  • clitimeout 50000
  • srvtimeout 50000
  • listen stats
  • bind *:81
  • stats enable
  • stats uri /haproxy
  • stats auth qianfeng:123
  • frontend web
  • mode http
  • bind *:80
  • option httplog
  • acl html url_reg -i \.html$
  • use_backend httpservers if html
  • default_backend httpservers
  • backend httpservers
  • balance roundrobin
  • server http1 192.168.137.30:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
  • server http2 192.168.137.40:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
  • # 将配置文件拷贝到slave服务器
  • scp /etc/haproxy/haproxy.cfg 192.168.137.20:/etc/haproxy/
  • systemctl start haproxy
  • systemctl enable haproxy

master 服务器:

  • # 备份配置文件
  • cp /etc/keepalived/{keepalived.conf,keepalived.conf.bak}
  • vim /etc/keepalived/keepalived.conf
  • #
  • ! Configuration File for keepalived
  • global_defs {
  • router_id master01
  • }
  • vrrp_instance VI_1 {
  • state MASTER
  • interface ens33
  • virtual_router_id 80
  • priority 100
  • advert_int 1
  • authentication {
  • auth_type PASS
  • auth_pass 1111
  • }
  • virtual_ipaddress {
  • 192.168.137.100/24
  • }
  • }

backup 服务器:

  • # 备份配置文件
  • cp /etc/keepalived/{keepalived.conf,keepalived.conf.bak}
  • vim /etc/keepalived/keepalived.conf
  • #
  • ! Configuration File for keepalived
  • global_defs {
  • router_id backup01
  • }
  • vrrp_instance VI_1 {
  • state BACKUP
  • interface ens33
  • nopreempt
  • virtual_router_id 80
  • priority 50
  • advert_int 1
  • authentication {
  • auth_type PASS
  • auth_pass 1111
  • }
  • virtual_ipaddress {
  • 192.168.137.100/24
  • }
  • }

调度器配置Haproxy(主/备)2:

  • systemctl start keepalived
  • systemctl enable keepalived
  • ip a

扩展:对调度器Haproxy健康检查(可选)

思路:两台机器都做

  • # 让 Keepalived 每隔一定时间执行脚本,脚本的功能是当Haproxy失败,则关闭本机的 Keepalived
  • vim /etc/keepalived/check_haproxy_status.sh
  • # 编写脚本
  • #!/bin/bash
  • /usr/bin/curl -I http://localhost &>/dev/null
  • if [ $? -ne 0 ];then
  • # /etc/init.d/keepalived stop
  • systemctl stop keepalived
  • fi
  • # 给脚本添加权限
  • chmod a+x /etc/keepalived/check_haproxy_status.sh
  • # keepalived 使用脚本
  • vim /etc/keepalived/keepalived.conf
  • #
  • ! Configuration File for keepalived
  • global_defs {
  • router_id master01
  • }
  • vrrp_script check_haproxy {
  • script "/etc/keepalived/check_haproxy_status.sh"
  • interval 5
  • }
  • vrrp_instance VI_1 {
  • state MASTER
  • interface ens33
  • virtual_router_id 80
  • priority 100
  • advert_int 1
  • authentication {
  • auth_type PASS
  • auth_pass 1111
  • }
  • virtual_ipaddress {
  • 192.168.137.100/24
  • }
  • track_script {
  • check_haproxy
  • }
  • }
  • # backup机器也需要:
  • vim /etc/keepalived/keepalived.conf
  • #
  • ! Configuration File for keepalived
  • global_defs {
  • router_id backup02
  • }
  • vrrp_script check_haproxy {
  • script "/etc/keepalived/check_haproxy_status.sh"
  • interval 5
  • }
  • vrrp_instance VI_1 {
  • state BACKUP
  • interface ens33
  • nopreempt
  • virtual_router_id 80
  • priority 50
  • advert_int 1
  • authentication {
  • auth_type PASS
  • auth_pass 1111
  • }
  • virtual_ipaddress {
  • 192.168.137.100/24
  • }
  • track_script {
  • check_haproxy
  • }
  • }
  • # 注:必须先启动haproxy,再启动keepalived
  • systemctl restart keepalived
  • ip a
  • # VIp在master上,关闭master的keepalived,发现VIP漂移到了backup上。

两台负载均衡机器都配置 haproxy 的日志:

  • vim /etc/rsyslog.conf
  • # 需要打开注释并添加
  • # Provides UDP syslog reception #由于haproxy的日志是用udp传输的,所以要启用rsyslog的udp监听
  • $ModLoad imudp -----UDP协议
  • $UDPServerRun 514 -----514端口
  • 找到 #### RULES #### 下面添加
  • local2.* /var/log/haproxy.log
  • # 参数解释:
  • # $ModLoad imudp:ModLoad加载指定模块。imudp:模块名称,用于从 UDP 网络连接中接收日志消息。
  • # local2 日志消息的一个分类标识符。在此配置中,local2可能与HAProxy的日志配置相关联。* 是优先级选择器,表示所有优先级的日志消息(从最紧急的emerg到最不重要的debug)都要被记录。
  • systemctl restart rsyslog
  • systemctl restart haproxy
  • tail -f /var/log/haproxy.log

配置文件详细:

  1. 全局配置
  • global
  • log 127.0.0.1 local2 info
  • pidfile /var/run/haproxy.pid
  • maxconn 4000
  • user haproxy
  • group haproxy
  • daemon
  • nbproc 1

log 127.0.0.1 local2 info:

  • 日志记录配置。127.0.0.1表示日志发送到本地的Syslog服务器,local2是Syslog的设施(facility),info是日志级别。

pidfile /var/run/haproxy.pid:

  • 指定HAProxy进程ID的文件位置。

maxconn 4000:

  • 设置HAProxy允许的最大并发连接数为4000。超出这个限制的连接将被队列或拒绝。

user haproxy 和 group haproxy:

  • 指定HAProxy以哪个用户和用户组身份运行,以提高安全性。

daemon:

  • 使HAProxy以后台守护进程的方式运行,不阻塞终端。

nbproc 1:

  • 指定HAProxy运行的进程数量。1表示单进程模式。通常情况下,设置为服务器的CPU核心数,以充分利用多核CPU的性能。
  1. 默认配置
  • defaults
  • mode http
  • log global
  • retries 3
  • option redispatch
  • maxconn 4000
  • contimeout 5000
  • clitimeout 50000
  • srvtimeout 50000

mode http:

  • 设置HAProxy的工作模式为HTTP(7层),可以处理应用层的内容,如URL、头信息等。如果设置为TCP(4层),则只能处理传输层数据。

log global:

  • 继承全局配置中的日志设置。

retries 3:

  • 如果连接后端服务器失败,HAProxy会尝试重新连接3次。如果仍然失败,认为该服务器不可用。

option redispatch:

  • 如果某个服务器不可用,HAProxy会重新分配请求到其他健康的服务器,确保服务的可用性。

maxconn 4000:

  • 设置每个连接的最大并发数为4000,与【 全局配置】中相同。

contimeout 5000:

  • 设置HAProxy与后端服务器建立连接的超时时间为5000毫秒(5秒)。

clitimeout 50000:

  • 设置客户端与HAProxy之间连接的超时时间为50000毫秒(50秒)。

srvtimeout 50000:

  • 设置后端服务器处理请求的超时时间为50000毫秒(50秒)。
  1. 统计页面配置
  • listen stats
  • bind *:81
  • stats enable
  • stats uri /haproxy
  • stats auth qianfeng:123

bind *:81:

  • 监听所有IP地址的81端口,用于访问HAProxy的统计页面。

stats enable:

  • 启用统计页面。

stats uri /haproxy:

  • 指定访问统计页面的URI路径,即http://<HAProxy-IP>:81/haproxy。

stats auth qianfeng:123:

  • 启用用户认证,用户名为qianfeng,密码为123。访问统计页面时需要输入此用户名和密码。
  1. 前端配置
  • frontend web
  • mode http
  • bind *:80
  • option httplog
  • acl html url_reg -i \.html$
  • use_backend httpservers if html
  • default_backend httpservers

frontend web:

  • 定义前端,名字为web。前端是客户端请求的入口。

mode http:

  • 设定前端的为HTTP。

bind *:80:

  • 监听所有IP地址的80端口,所有通过80端口的HTTP请求都会被接收。

option httplog:

  • 启用HTTP日志格式,记录HTTP相关的详细日志。

acl html url_reg -i .html$:

  • 定义一个访问控制列表(ACL),名称为html。规则是匹配以.html结尾的URL(不区分大小写)。

use_backend httpservers if html:

  • 如果请求的URL匹配html ACL规则,则将请求转发到后端服务器组httpservers

default_backend httpservers:

  • 如果请求不匹配任何ACL规则,则使用默认的后端服务器组httpservers
  1. 后端配置
  • backend httpservers
  • balance roundrobin
  • server http1 192.168.137.30:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
  • server http2 192.168.137.40:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2

backend httpservers:

  • 定义后端服务器组,名称为httpservers。后端服务器组是用于处理实际请求的服务器。

balance roundrobin:

  • 负载均衡策略为轮询(Roundrobin)

server http1 192.168.246.162:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2:

  • 定义一台后端服务器http1
  • 192.168.246.162:80: 服务器的IP地址和端口号。
  • maxconn 2000: 这台服务器允许的最大连接数为2000。
  • weight 1: 权重为1;
  • check: 启用健康检查,定期检查该服务器的状态。
  • inter 1s: 每1秒进行一次健康检查。
  • rise 2: 连续2次检查通过后认为服务器健康。
  • fall 2: 连续2次检查失败后认为服务器不健康。

Haproxy 实现四层负载均衡(了解)

  • # 两台real server安转mariadb mariadb-server
  • [root@real-server ~]# yum install -y mariadb-server
  • # 启动mariadb
  • [root@real-server ~]# systemctl enable --now mariadb
  • # 连接数据库
  • [root@real-server ~]# mysql -uroot -p
  • MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by '123456';
  • MariaDB [(none)]> flush privileges;
  • MariaDB [(none)]> exit;
  • 两台haproxy配置文件:
  • [root@ha-proxy-master ~]# cat /etc/haproxy/haproxy.cfg
  • Haproxy L4
  • ===================================================================================
  • global
  • log 127.0.0.1 local2
  • pidfile /var/run/haproxy.pid
  • maxconn 4000
  • user haproxy
  • group haproxy
  • daemon
  • nbproc 1
  • defaults
  • mode http
  • log global
  • option redispatch
  • retries 3
  • maxconn 4000
  • contimeout 5000
  • clitimeout 50000
  • srvtimeout 50000
  • listen stats
  • bind *:81
  • stats enable
  • stats uri /haproxy
  • stats auth qianfeng:123
  • frontend web
  • mode http
  • bind *:80
  • option httplog
  • default_backend httpservers
  • backend httpservers
  • balance roundrobin
  • server http1 192.168.246.162:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
  • server http2 192.168.246.163:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
  • # 添加已下字段
  • listen mysql
  • bind *:3306
  • mode tcp
  • balance roundrobin
  • server mysql1 192.168.246.163:3306 weight 1 check inter 1s rise 2 fall 2
  • server mysql2 192.168.246.162:3306 weight 1 check inter 1s rise 2 fall 2
  • inter表示健康检查的间隔,单位为毫秒 可以用1s等,fall代表健康检查失败2回后放弃检查。
  • rise代表连续健康检查成功2此后将认为服务器可用。
  • 默认的,haproxy认为服务时永远可用的,除非加上check让haproxy确认服务是否真的可用。
  • 找一台机器做为客户端去测试,在测试的时候注意mysql的远程登录权限
城东书院 www.cdsy.xyz
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
上一篇:京东开源的 JD-hotkey,强得可怕! 下一篇:很抱歉没有了
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐