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

    你可能感兴趣的文章
    MySQL 基础模块的面试题总结
    查看>>
    MySQL 处理插入重主键唯一键重复值办法
    查看>>
    Mysql 备份
    查看>>
    MySQL 备份 Xtrabackup
    查看>>
    mysql 复杂查询_mysql中复杂查询
    查看>>
    mYSQL 外键约束
    查看>>
    mysql 多个表关联查询查询时间长的问题
    查看>>
    mySQL 多个表求多个count
    查看>>
    mysql 多字段删除重复数据,保留最小id数据
    查看>>
    MySQL 多表联合查询:UNION 和 JOIN 分析
    查看>>
    MySQL 大数据量快速插入方法和语句优化
    查看>>
    mysql 如何给SQL添加索引
    查看>>
    mysql 字段区分大小写
    查看>>
    mysql 字段合并问题(group_concat)
    查看>>
    mysql 字段类型类型
    查看>>
    MySQL 字符串截取函数,字段截取,字符串截取
    查看>>
    MySQL 存储引擎
    查看>>
    mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
    查看>>
    MySQL 存储过程参数:in、out、inout
    查看>>
    mysql 存储过程每隔一段时间执行一次
    查看>>