博客
关于我
mqtt haproxy 代理及负载搭建
阅读量:795 次
发布时间:2023-02-09

本文共 4913 字,大约阅读时间需要 16 分钟。

Haproxy 与 MQTT 分布集群搭建指南

Haproxy 安装与配置

1. 解压

cd /home/hylink/tar zxvf haproxy-1.8.12.tar.gzcd zxvf haproxy-1.8.12

2. 安装

make TARGET=linux2628 ARCH=x86_64 PREFIX=/home/hylink/haproxymake install PREFIX=/home/hylink/haproxy

3. 配置 haproxy.cfg

sudo gedit /home/hylink/haproxy/sbin/haproxy.cfg

添加以下内容:

global    log 127.0.0.1 local0    log 127.0.0.1 local1 notice    daemon    nbproc 2    maxconn 51200    pidfile /home/hylink/haproxy/sbin/haproxy.pid  defaults    log global    mode http    option httplog    option dontlognull    retries 3    option abortonclose    timeout connect 5000ms    timeout client 30000ms    timeout server 60000ms    balance roundrobinlisten stats    bind *:9080    mode http    option httplog    maxconn 10    stats refresh 30s    stats uri /stats    stats realm Haproxy Manager    stats hide-version    stats admin if TRUE

4. 启动 Haproxy

/home/hylink/haproxy/sbin/haproxy  -f  /home/hylink/haproxy/sbin/haproxy.cfg

如果出现以下异常:

[WARNING] 227/155234 (4947) : Proxy 'stats': in multi-process mode, stats will be limited to process assigned to the current request.[WARNING] 227/155234 (4947) : Proxy 'stats': stats admin will not work correctly in multi-process mode.[WARNING] 227/155234 (4947) : [/home/hylink/haproxy/sbin/haproxy.main()] Cannot raise FD limit to 102411, limit is 4096.[WARNING] 227/155234 (4947) : [/home/hylink/haproxy/sbin/haproxy.main()] FD limit (4096) too low for maxconn=51200/maxsock=102411. Please raise 'ulimit-n' to 102411 or more to avoid any trouble.

解决方案:

  • 修改 /etc/security/limits.conf,添加以下内容:
  • * soft nofile = 32768* hard nofile = 65536
    1. 修改 /etc/profile,添加以下内容:
    2. ulimit -n 32768
      1. 切换到 root 用户并执行:
      2. sudo -sulimit -n 32768source /etc/profile

        然后重新启动 Haproxy:

        /home/hylink/haproxy/sbin/haproxy  -f  /home/hylink/haproxy/sbin/haproxy.cfg

        MQTT 配置

        1. 查看集群信息

        ./bin/emqttd_ctl cluster status

        2. 配置 MQTT 端口

        haproxy.cfg 中添加以下内容:

        listen mqttbind *:1890mode tcpmaxconn 50000option clitcpkatimeout client 3htimeout server 3hoption tcplogbalance leastconnserver emq1 172.19.12.231:1883 check inter 10000 fall 2 rise 5 weight 1server emq2 172.19.12.207:1883 check inter 10000 fall 2 rise 5 weight 1

        3. 重启 Haproxy

        /home/hylink/haproxy/sbin/haproxy  -f  /home/hylink/haproxy/sbin/haproxy.cfg

        负载均衡配置说明

        负载均衡算法

      3. Roundrobin:简单的轮询算法,每个服务器根据权重轮流使用。
      4. Static-rr:静态轮询,服务器权重在安装时确定。
      5. Leastconn:根据最少连接数处理请求,适合长连接服务。
      6. Source:根据请求源 IP 进行哈希分配。
      7. Uri:根据请求 URI 进行哈希分配。
      8. Hdr(name):根据 HTTP 请求头进行哈希分配。
      9. Rdp-cookie:根据 RDP cookie 进行哈希分配。
      10. ACL 规则定义

        1. 定义 ACL 策略

        acl denali_policy hdr_reg(host) -i ^(www.inbank.com|image.inbank.com)$acl tm_policy hdr_dom(host) -i www.inbank.comacl invalid_req url_sub -i sip_apiname=acl timetask_req url_dir -i timetaskacl missing_cl hdr_cnt(Content-length) eq 0

        2. ACL 匹配规则

        block if missing_clblock if !invalid_req || timetask_requse_backend denali_server if denali_policyuse_backend tm_server if tm_policyuse_backend ^[^\ ]*\ /(img|css)/ dynamicuse_backend ^[^\ ]*\ /admin/stats statsdefault_backend mms

        全局配置

        global    log 127.0.0.1 local0    log 127.0.0.1 local1 notice    daemon    nbproc 1    maxconn 4096    # 用户和组    # pidfile /var/run/haproxy.pid    # ulimit-n 819200    # chroot /usr/share/haproxy    debug    quiet

        默认配置

        defaults    log global    mode http    option httplog    option dontlognull    retries 2    option httpclose    option abortonclose    maxconn 4096    timeout connect 5000ms    timeout client 30000ms    timeout server 30000ms    balance roundrobin

        统计页面配置

        listen statsbind 0.0.0.0:1080mode httpoption httplogmaxconn 10stats refresh 30sstats uri /statsstats realm XingCloud Haproxystats auth admin:adminstats auth Frank:Frankstats hide-versionstats admin if TRUE

        错误页面配置

        #errorfile 403 /home/haproxy/haproxy/errorfiles/403.http#errorfile 500 /home/haproxy/haproxy/errorfiles/500.http#errorfile 502 /home/haproxy/haproxy/errorfiles/502.http#errorfile 503 /home/haproxy/haproxy/errorfiles/503.http#errorfile 504 /home/haproxy/haproxy/errorfiles/504.http

        Frontend 配置

        frontend mainbind *:80acl web hdr(host) -i www.abc.comacl img hdr(host) -i img.abc.comuse_backend webserver if webuse_backend imgserver if imgdefault_backend dynamic

        Backend 配置

        backend webservermode httpbalance roundrobinoption httpchk /index.html HTTP/1.0server web1 10.16.0.9:8085 cookie 1 weight 5 check inter 2000 rise 2 fall 3server web2 10.16.0.10:8085 cookie 2 weight 3 check inter 2000 rise 2 fall 3backend imgservermode httpoption httpchk /index.phpbalance roundrobinserver img01 192.168.137.101:80 check inter 2000 fall 3server img02 192.168.137.102:80 check inter 2000 fall 3backend dynamicbalance roundrobinserver test1 192.168.1.23:80 check maxconn 2000server test2 192.168.1.24:80 check maxconn 2000listen tcptestbind 0.0.0.0:5222mode tcpoption tcplogbalance sourceserver s1 192.168.100.204:7222 weight 1server s2 192.168.100.208:7222 weight 1

        Haproxy 监测页面参数

        • Queue:当前队列请求数量
        • Session rate:每秒的连接回话数量
        • Sessions:总回话量
        • Bytes:网络字节数输入输出
        • Denied:拒绝请求量
        • Errors:错误请求和连接
        • Warnings:重新尝试和Redispatch

    转载地址:http://stffk.baihongyu.com/

    你可能感兴趣的文章
    logging.config报错FileNotFoundError
    查看>>
    logstash mysql 准实时同步到 elasticsearch
    查看>>
    Logstash简介和部署---ElasticStack(ELK)工作笔记019
    查看>>
    luogu P1268 树的重量
    查看>>
    LUOGU P4095 [HEOI2013]Eden 的新背包问题
    查看>>
    Luogu2973:[USACO10HOL]赶小猪
    查看>>
    LVS-DR工作原理图文详解
    查看>>
    LZ4 1.10 压缩算法发布!具有多线程功能,压缩速度显著提高达 8 倍
    查看>>
    lzg_ad:打印机需要的组件支持
    查看>>
    mabatis 中出现< 以及> 代表什么意思?
    查看>>
    Mac book pro打开docker出现The data couldn’t be read because it is missing
    查看>>
    mac elasticsearch brew安装填坑
    查看>>
    mac M1 下安装docker 及相关镜像
    查看>>
    Mac M1 芯片不兼容node-sass
    查看>>
    MAC M1大数据0-1成神篇-25 hadoop高可用搭建
    查看>>
    mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
    查看>>
    Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
    查看>>
    Mac OS下错误The superclass “javax.servlet.http.HttpServlet“ was not found on the Java Build Path的解决方法
    查看>>
    mac 搭建APK反编译环境[转]
    查看>>
    MAC 显示隐藏文件
    查看>>