HAProxy七层负载均衡配置方案

HAProxy

一、准备

  • 准备5台CentOS7服务器,IP地址如下:
    • HAProxy 192.168.152.71
    • web1 192.168.152.72
    • web2 192.168.152.73
    • php1 192.168.152.74
    • php2 192.168.152.75

二、配置HAProxy服务器

1. 下载HAProxy

bash 复制代码
yum install -y haproxy

2. 编写配置文件

bash 复制代码
vim /etc/haproxy/haproxy.cfg

编写内容如下

bash 复制代码
# 全局设置
global
    log 127.0.0.1 local3 info          # 日志记录到本地
    maxconn 4096                       # 最大连接数
    uid nobody                         # 运行用户,写用户编号99也行
    gid nobody                         # 运行组,写组编号99也行
    daemon                             # 以守护进程模式运行
    nbproc 1                           # 使用的进程数量
    pidfile /run/haproxy.pid           # PID 文件位置

# 默认设置
defaults
    log           global               # 使用全局日志设置
    mode          http                 # 设置模式为 HTTP
    maxconn       2048                 # 默认最大连接数
    retries       3                    # 重试次数
    option        redispatch           # 如果一个服务器超载,重新分配请求
    contimeout    5000                 # 连接超时(毫秒)
    clitimeout    50000                # 客户端超时(毫秒)
    srvtimeout    50000                # 服务器超时(毫秒)
#timeout connect 5000   #contimeout的新版写法
#timeout client  50000  #clitimeout的新版写法
#timeout server  50000  #srvtimeout的新版写法
    option        abortonclose         # 客户端关闭连接时中断后端连接

    stats uri     /admin?stats         # 统计信息的 URI
    stats realm   Private lands        # 统计信息页面标题
    stats auth    admin:password       # 统计信息页面认证,登录用户为 admin,登录密码为 password
    stats hide-version                 # 隐藏版本信息

# 前端配置
frontend http-in
    bind 0.0.0.0:80                    # 监听所有 IP 的 80 端口
    mode http                          # HTTP 模式
    log global                         # 使用全局日志设置
    option httplog                     # 启用 HTTP 日志记录
    option httpclose                   # 在每个请求后关闭连接

    acl html url_reg -i \.html$        # 匹配 URL 中的 .html 扩展名
    use_backend html-server if html    # 如果匹配 .html 扩展名    acl php url_reg -i \.php$          # 匹配 URL 中的 .php 扩展名
    
    acl php url_reg -i \.php$          # 匹配 URL 中的 .php 扩展名
    use_backend php-server if php      # 如果匹配 .php 扩展名,则使用 php-server 后端

    default_backend html-server        # 默认使用 html-server 后端

# web后端配置
backend html-server
    mode http                          # HTTP 模式
    balance roundrobin                 # 负载均衡算法:轮询
    option httpchk GET /index.html     # 健康检查:请求 /index.html
    cookie SERVERID insert indirect nocache # 插入会话 Cookie
    server html-A 192.168.152.72:80 weight 1 cookie 1 check inter 2000 rise 2 fall 5 # 后端服务器,注意 cookiec 后的值是唯一的
    server html-B 192.168.152.73:80 weight 1 cookie 2 check inter 2000 rise 2 fall 5 # 后端服务器,注意 cookiec 后的值是唯一的

# php后端配置
backend php-server
    mode http                          # HTTP 模式
    balance roundrobin                 # 负载均衡算法:轮询
    option httpchk GET /index.php     # 健康检查:请求 /index.php
    cookie SERVERID insert indirect nocache # 插入会话 Cookie
    server php-A 192.168.152.74:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5 # 后端服务器,注意 cookiec 后的值是唯一的
    server php-B 192.168.152.75:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5 # 后端服务器,注意 cookiec 后的值是唯一的,则使用 html-server 后端

3. 启动HAProxy服务

bash 复制代码
systemctl start haproxy && systemctl enable haproxy

三、配置后端服务

①配置web服务器

测试用简单配置,web1 和 web2 都要配置

  1. 安装http服务

    bash 复制代码
    yum install -y httpd
  2. 编写主页
    web1:

    bash 复制代码
    echo web1 > /var/www/html/index.html

    web2:

    bash 复制代码
    echo web2 > /var/www/html/index.html
  3. 启动http服务

    bash 复制代码
    systemctl start httpd && systemctl enable httpd

②配置php服务器

测试用简单配置,php1 和 php2 都要配置

  1. 安装 php 和 http 服务

    bash 复制代码
    yum install -y httpd php
  2. 编写主页
    php1:

    bash 复制代码
    echo php1 > /var/www/html/index.php

    php2:

    bash 复制代码
    echo php2 > /var/www/html/index.php
  3. 启动服务

    bash 复制代码
    systemctl start httpd && systemctl enable httpd

四、测试

可以在浏览器访问 HAProxy服务器地址/统计信息的URIhttp://192.168.152.71/admin?stats 可以看到如下界面

  • 图中绿色部分变为其他颜色代表什么:
    • 红色
      • 配置文件中服务器地址冲突、找不到服务器
    • 黄色
      • 对应服务器中的http服务未开启

多次刷新,查看结果,前两个的结果为 web1,web2;最后一个结果为 php1,php2。

如果配置没有问题,但多次刷新无果,可能是浏览器缓存,可以用命令 elinks

下载 yum insdall -y elinks

bash 复制代码
elinks --dump http://192.168.152.71
elinks --dump http://192.168.152.71/index.html
elinks --dump http://192.168.152.71/index.php
相关推荐
左手厨刀右手茼蒿3 分钟前
Linux 内核中的块设备驱动:从原理到实践
linux·嵌入式·系统内核
杨云龙UP6 分钟前
从0到1快速学会Linux操作系统(基础),这一篇就够了!
linux·运维·服务器·学习·ubuntu·centos·ssh
HXQ_晴天8 分钟前
Ubuntu 设置中文输入法
linux·运维·ubuntu
Dovis(誓平步青云)9 分钟前
《Linux 信号入门:搞懂 “进程通信的紧急电话” 到底怎么用(初篇)》
linux·运维·服务器
左手厨刀右手茼蒿10 分钟前
Linux 内核中的模块机制:从加载到卸载
linux·嵌入式·系统内核
0vvv020 分钟前
删除wsl环境下的Ubuntu系统
linux·运维·ubuntu
@土豆23 分钟前
Ubuntu 22.04 运行 Filebeat 7.11.2 崩溃问题分析及解决文档
linux·数据库·ubuntu
C++ 老炮儿的技术栈38 分钟前
GCC编译时无法向/tmp 目录写入临时汇编文件,因为设备空间不足,解决
linux·运维·开发语言·汇编·c++·git·qt
Agent产品评测局1 小时前
企业数据处理自动化落地,抓取分析全流程实现方案 —— 2026企业级智能体选型与技术路径深度解析
运维·人工智能·ai·自动化
爱莉希雅&&&1 小时前
linux中MySQL数据库备份恢复的四种方法(更新中)
linux·数据库·mysql·数据库备份·mysqldumper