博客
关于我
mqtt haproxy 代理及负载搭建
阅读量:794 次
发布时间: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/

    你可能感兴趣的文章
    Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
    查看>>
    MySQL Cluster与MGR集群实战
    查看>>
    multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
    查看>>
    mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
    查看>>
    Multiple websites on single instance of IIS
    查看>>
    mysql CONCAT()函数拼接有NULL
    查看>>
    multiprocessing.Manager 嵌套共享对象不适用于队列
    查看>>
    multiprocessing.pool.map 和带有两个参数的函数
    查看>>
    MYSQL CONCAT函数
    查看>>
    multiprocessing.Pool:map_async 和 imap 有什么区别?
    查看>>
    MySQL Connector/Net 句柄泄露
    查看>>
    multiprocessor(中)
    查看>>
    mysql CPU使用率过高的一次处理经历
    查看>>
    Multisim中555定时器使用技巧
    查看>>
    MySQL CRUD 数据表基础操作实战
    查看>>
    multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
    查看>>
    mysql csv import meets charset
    查看>>
    multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
    查看>>
    MySQL DBA 数据库优化策略
    查看>>
    multi_index_container
    查看>>