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
相关推荐
shan~~19 小时前
linux达梦数据库操作
linux·数据库·chrome
liliangcsdn19 小时前
LLM时代基于unstructured解析非结构化pdf
linux·服务器·数据分析
Codigger官方19 小时前
Linux 基金会牵头成立 React 基金会:前端开源生态迎来里程碑式变革
linux·前端·react.js
武文斌7719 小时前
项目学习总结:LVGL图形参数动态变化、开发板的GDB调试、sqlite3移植、MQTT协议、心跳包
linux·开发语言·网络·arm开发·数据库·嵌入式硬件·学习
爱吃喵的鲤鱼20 小时前
仿mudou——Connection模块(连接管理)
linux·运维·服务器·开发语言·网络·c++
让子弹飞0220 小时前
永久解决ubuntu网络连接问题
linux·运维·ubuntu
郝学胜-神的一滴20 小时前
使用Linux的read和write系统函数操作文件
linux·服务器·开发语言·数据库·c++·程序人生·软件工程
七七七七0721 小时前
【Linux 系统】打开文件和文件系统
linux·运维·spring
DeeplyMind1 天前
第二章:模块的编译与运行-7 Loading and Unloading Modules
linux·驱动开发
---学无止境---1 天前
Linux中驱动程序通过fasync异步通知应用程序的实现
linux