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
相关推荐
BeyondESH1 分钟前
Linux线程同步—竞态条件和互斥锁(C语言)
linux·服务器·c++
hanniuniu134 分钟前
详细解读,F5服务器负载均衡的技术优势
运维·服务器·负载均衡
m0_635502208 分钟前
Spring Cloud Gateway组件
网关·微服务·负载均衡·过滤器
鱼饼6号17 分钟前
Prometheus 上手指南
linux·运维·centos·prometheus
Asher Gu23 分钟前
Linux系统编程入门 | 模拟实现 ls -l 命令
linux
c无序40 分钟前
【Linux进程控制】进程程序替换
linux
m0_609000422 小时前
向日葵好用吗?4款稳定的远程控制软件推荐。
运维·服务器·网络·人工智能·远程工作
小安运维日记3 小时前
Linux云计算 |【第四阶段】NOSQL-DAY1
linux·运维·redis·sql·云计算·nosql
CoolTiger、6 小时前
【Vmware16安装教程】
linux·虚拟机·vmware16
m0_741768856 小时前
使用docker的小例子
运维·docker·容器