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

yum install -y haproxy

2. 编写配置文件

vim /etc/haproxy/haproxy.cfg

编写内容如下

# 全局设置
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服务

systemctl start haproxy && systemctl enable haproxy

三、配置后端服务

①配置web服务器

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

  1. 安装http服务
    yum install -y httpd
    
  2. 编写主页
    web1:
    echo web1 > /var/www/html/index.html
    
    web2:
    echo web2 > /var/www/html/index.html
    
  3. 启动http服务
    systemctl start httpd && systemctl enable httpd
    

②配置php服务器

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

  1. 安装 php 和 http 服务
    yum install -y httpd php
    
  2. 编写主页
    php1:
    echo php1 > /var/www/html/index.php
    
    php2:
    echo php2 > /var/www/html/index.php
    
  3. 启动服务
    systemctl start httpd && systemctl enable httpd
    

四、测试

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

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

在这里插入图片描述

  • 可以在浏览器分别输入:
    • http://192.168.152.71
    • http://192.168.152.71/index.html
    • http://192.168.152.71/index.php

多次刷新,查看结果,前两个的结果为 web1,web2;最后一个结果为 php1,php2。
如果配置没有问题,但多次刷新无果,可能是浏览器缓存,可以用命令 elinks
下载 yum insdall -y elinks

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
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值