mqtt haproxy 代理及负载搭建

目录

mqtt 分布集群搭建

haproxy 安装配置

解压

安装

配置haproxy.cfg

启动haproxy

配置mqtt

测试

负载配置说明

负载均衡算法

ACL规则定义

全局配置

默认配置

统计页面配置

设置haproxy 错误页面

frontend前端配置

backend后端配置

haproxy监测页面参数


mqtt 分布集群搭建

详情请参照emqtt 分布集群及节点桥接搭建

haproxy 安装配置

解压

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

安装

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

#参数说明
TARGET=linux26 #内核版本,使用uname -r查看内核,如:2.6.18-371.el5,此时该参数就为linux26;kernel 大于2.6.28的用:TARGET=linux2628
ARCH=x86_64 #系统位数
PREFIX=/home/hylink/haproxy /home/hylink/haproxy为haprpxy安装路径

配置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 roundrobin

listen 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

启动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.

解决方案:系统里的能打开的文件描述符太小,程序里的比较大,只要将系统里的FD重新调大就好

1、修改/etc/security/limits.conf,通过

vi /etc/security/limits.conf

修改其内容,在文件最后加入(数值也可以自己定义):

* soft  nofile = 32768
* hard  nofile = 65536

2、修改/etc/profile,通过

vi /etc/profile

修改,在最后加入以下内容

ulimit -n 32768

3、切换用户

su - root
source /etc/profile

在root用户下继续执行

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

启动haproxy,访问:http://172.19.12.231:9080/stats 进入统计页面,至此haproxy搭建完毕。

配置mqtt

执行

./bin/emqttd_ctl cluster status

查看集群信息

执行

vim /home/hylink/haproxy/sbin/haproxy.cfg

添加以下内容

listen mqtt
    bind *:1890
    mode tcp
    maxconn 50000
    option clitcpka # For TCP keep-alive
    timeout client 3h #By default TCP keep-alive interval is 2hours in OS kernal, 'cat /proc/sys/net/ipv4/tcp_keepalive_time'
    timeout server 3h #By default TCP keep-alive interval is 2hours in OS kernal
    option tcplog
    balance leastconn
    server emq1 172.19.12.231:1883 check inter 10000 fall 2 rise 5 weight 1
    server emq2 172.19.12.207:1883 check inter 10000 fall 2 rise 5 weight 1

重启haproxy

访问:http://172.19.12.231:9080/stats

测试

分别用两台机器的客户端连接代理机器的1890端口。

这个客户端分别被分配到不同的机器上

接收消息效果

负载配置说明

 

负载均衡算法

一、roundrobin,表示简单的轮询,每个服务器根据权重轮流使用,在服务器的处理时间平均分配的情况下这是最流畅和公平的算法。该算法是动态的,对于实例启动慢的服务器权重会在运行中调整。

二、static-rr,表示根据权重,建议关注;每个服务器根据权重轮流使用,类似roundrobin,但它是静态的,意味着运行时修改权限是无效的。另外,它对服务器的数量没有限制。

三、leastconn,表示最少连接者先处理,建议关注;leastconn建议用于长会话服务,例如LDAP、SQL、TSE等,而不适合短会话协议。如HTTP.该算法是动态的,对于实例启动慢的服务器权重会在运行中调整。

四、source,表示根据请求源IP,建议关注;对请求源IP地址进行哈希,用可用服务器的权重总数除以哈希值,根据结果进行分配。

           只要服务器正常,同一个客户端IP地址总是访问同一个服务器。如果哈希的结果随可用服务器数量而变化,那么客户端会定向到不同的服务器;

           该算法一般用于不能插入cookie的Tcp模式。它还可以用于广域网上为拒绝使用会话cookie的客户端提供最有效的粘连;

           该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。

五、uri,表示根据请求的URI;表示根据请求的URI左端(问号之前)进行哈希,用可用服务器的权重总数除以哈希值,根据结果进行分配。

        只要服务器正常,同一个URI地址总是访问同一个服务器。

        一般用于代理缓存和反病毒代理,以最大限度的提高缓存的命中率。该算法只能用于HTTP后端;

        该算法一般用于后端是缓存服务器;

        该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。

六、url_param,表示根据请求的URl参数'balance url_param' requires an URL parameter name

              在HTTP GET请求的查询串中查找<param>中指定的URL参数,基本上可以锁定使用特制的URL到特定的负载均衡器节点的要求;

              该算法一般用于将同一个用户的信息发送到同一个后端服务器;

              该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。

七、hdr(name),表示根据HTTP请求头来锁定每一次HTTP请求;

              在每个HTTP请求中查找HTTP头<name>,HTTP头<name>将被看作在每个HTTP请求,并针对特定的节点;

              如果缺少头或者头没有任何值,则用roundrobin代替;

              该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。

八、rdp-cookie(name),表示根据据cookie(name)来锁定并哈希每一次TCP请求。

                     为每个进来的TCP请求查询并哈希RDP cookie<name>;

                     该机制用于退化的持久模式,可以使同一个用户或者同一个会话ID总是发送给同一台服务器。

                     如果没有cookie,则使用roundrobin算法代替;

                     该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。

#其实这些算法各有各的用法,我们平时应用得比较多的应该是roundrobin、source和lestconn。

 

ACL规则定义

########ACL策略定义#########################

1、#如果请求的域名满足正则表达式返回true -i是忽略大小写

acl denali_policy hdr_reg(host) -i ^(www.inbank.com|image.inbank.com)$

2、#如果请求域名满足www.inbank.com 返回 true -i是忽略大小写

acl tm_policy hdr_dom(host) -i www.inbank.com

3、#在请求url中包含sip_apiname=,则此控制策略返回true,否则为false

acl invalid_req url_sub -i sip_apiname=#定义一个名为invalid_req的策略

4、#在请求url中存在timetask作为部分地址路径,则此控制策略返回true,否则返回false

acl timetask_req url_dir -i timetask

5、#当请求的header中Content-length等于0时返回 true

acl missing_cl hdr_cnt(Content-length) eq 0

#########ACL策略匹配相应###################

1、#当请求中header中Content-length等于0 阻止请求返回403

block if missing_cl

2、#block表示阻止请求,返回403错误,当前表示如果不满足策略invalid_req,或者满足策略timetask_req,则阻止请求。

block if !invalid_req || timetask_req

3、#当满足denali_policy的策略时使用denali_server的backend

use_backend denali_server if denali_policy

4、#当满足tm_policy的策略时使用tm_server的backend

use_backend tm_server if tm_policy

5、#reqisetbe关键字定义,根据定义的关键字选择backend

reqisetbe ^Host: img dynamic

reqisetbe ^[^ ]* /(img|css)/ dynamic

reqisetbe ^[^ ]* /admin/stats stats

6、#以上都不满足的时候使用默认mms_server的backend

default_backend mms

全局配置

###########全局配置#########

global

  log 127.0.0.1 local0 #[日志输出配置,所有日志都记录在本机,通过local0输出]

  log 127.0.0.1 local1 notice #定义haproxy 日志级别[error warringinfo debug]

  daemon #以后台形式运行harpoxy

  nbproc 1 #设置进程数量

  maxconn 4096 #默认最大连接数,需考虑ulimit-n限制

  #user haproxy #运行haproxy的用户

  #group haproxy #运行haproxy的用户所在的组

  #pidfile /var/run/haproxy.pid #haproxy 进程PID文件

  #ulimit-n 819200 #ulimit 的数量限制

  #chroot /usr/share/haproxy  #chroot运行路径

  #debug #haproxy 调试级别,建议只在开启单进程的时候调试

  #quiet

 

默认配置

########默认配置############

defaults

  log global

  mode http  #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK

  option httplog #日志类别,采用httplog

  option dontlognull #不记录健康检查日志信息

  retries 2 #两次连接失败就认为是服务器不可用,也可以通过后面设置

  #option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip

  option httpclose #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现

  #option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器,以后将不支持

  option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接

  maxconn 4096 #默认的最大连接数

  timeout connect 5000ms #连接超时

  timeout client 30000ms #客户端超时

  timeout server 30000ms #服务器超时

  #timeout check 2000 #心跳检测超时

  #timeout http-keep-alive10s #默认持久连接超时时间

  #timeout http-request 10s #默认http请求超时时间

  #timeout queue 1m #默认队列超时时间

  balance roundrobin #设置默认负载均衡方式,轮询方式

  #balance source #设置默认负载均衡方式,类似于nginx的ip_hash

  #balnace leastconn #设置默认负载均衡方式,最小连接数

 

统计页面配置

########统计页面配置########

listen stats

  bind 0.0.0.0:1080 #设置Frontend和Backend的组合体,监控组的名称,按需要自定义名称

  mode http #http的7层模式

  option httplog #采用http日志格式

  #log 127.0.0.1 local0 err #错误日志记录

  maxconn 10 #默认的最大连接数

  stats refresh 30s #统计页面自动刷新时间

  stats uri /stats #统计页面url

  stats realm XingCloud Haproxy #统计页面密码框上提示文本

  stats auth admin:admin #设置监控页面的用户和密码:admin,可以设置多个用户名

  stats auth Frank:Frank #设置监控页面的用户和密码:Frank

  stats hide-version #隐藏统计页面上HAProxy的版本信息

  stats admin if TRUE #设置手工启动/禁用,后端服务器(haproxy-1.4.9以后版本)

 

设置haproxy 错误页面

########设置haproxy 错误页面#####

#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前端配置##############

frontend main

  bind *:80 #这里建议使用bind *:80的方式,要不然做集群高可用的时候有问题,vip切换到其他机器就不能访问了。

  acl web hdr(host) -i www.abc.com  #acl后面是规则名称,-i为忽略大小写,后面跟的是要访问的域名,如果访问www.abc.com这个域名,就触发web规则,。

  acl img hdr(host) -i img.abc.com  #如果访问img.abc.com这个域名,就触发img规则。

  use_backend webserver if web   #如果上面定义的web规则被触发,即访问www.abc.com,就将请求分发到webserver这个作用域。

  use_backend imgserver if img   #如果上面定义的img规则被触发,即访问img.abc.com,就将请求分发到imgserver这个作用域。

  default_backend dynamic #不满足则响应backend的默认页面

 

backend后端配置

########backend后端配置##############

backend webserver #webserver作用域

  mode http

  balance roundrobin #balance roundrobin 负载轮询,balance source 保存session值,支持static-rr,leastconn,first,uri等参数

  option httpchk /index.html HTTP/1.0 #健康检查, 检测文件,如果分发到后台index.html访问不到就不再分发给它

  server web1 10.16.0.9:8085 cookie 1 weight 5 check inter 2000 rise 2 fall 3

  server web2 10.16.0.10:8085 cookie 2 weight 3 check inter 2000 rise 2 fall 3

  #cookie 1表示serverid为1,check inter 1500 是检测心跳频率 

  #rise 2是2次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重

backend imgserver

  mode http

  option httpchk /index.php

  balance roundrobin 

  server img01 192.168.137.101:80 check inter 2000 fall 3

  server img02 192.168.137.102:80 check inter 2000 fall 3

backend dynamic 

  balance roundrobin 

  server test1 192.168.1.23:80 check maxconn 2000 

  server test2 192.168.1.24:80 check maxconn 2000

listen tcptest 

  bind 0.0.0.0:5222 

  mode tcp 

  option tcplog #采用tcp日志格式 

  balance source 

  #log 127.0.0.1 local0 debug 

  server s1 192.168.100.204:7222 weight 1 

  server s2 192.168.100.208:7222 weight 1

haproxy监测页面参数

Queue

Cur: current queued requests //当前的队列请求数量

Max:max queued requests     //最大的队列请求数量

Limit:           //队列限制数量

Session rate(每秒的连接回话)列表:

scur: current sessions        //每秒的当前回话的限制数量

smax: max sessions           //每秒的新的最大的回话量

slim: sessions limit           //每秒的新回话的限制数量

Sessions 

Total:            //总共回话量 

Cur:             //当前的回话

Max: //最大回话 

Limit: //回话限制

Lbtot: total number of times a server was selected  //选中一台服务器所用的总时间

Bytes

In: //网络的字节数输入总量  

Out: //网络的字节数输出总量

Denied

Req: denied requests//拒绝请求量

Resp:denied responses //拒绝回应

Errors

Req:request errors             //错误请求 

Conn:connection errors          //错误的连接

Resp: response errors (among which srv_abrt)  ///错误的回应

Warnings

Retr: retries (warning)                      //重新尝试

Redis:redispatches (warning)               //再次发送

Server列表:

Status:状态,包括up(后端机活动)和down(后端机挂掉)两种状态

LastChk:    持续检查后端服务器的时间

Wght: (weight) : //权重

Act: server is active (server), number of active servers (backend) //活动链接数量

Bck: server is backup (server), number of backup servers (backend) //backup://备份的服务器数量

Down:          //后端服务器连接后都是down的数量

Downtime: downtime: total downtime (in seconds)    //总的downtime 时间

Throttle: warm up status                          //设备变热状态

原文地址:https://www.cnblogs.com/gmhappy/p/11864112.html