emqx服务器调优

13次阅读

共计 6138 个字符,预计需要花费 16 分钟才能阅读完成。

优化前架构

主要问题

  • emqtt 2.x 版本问题
  • linux 内核参数
  • erl 配置参数
haproxy 问题
  • 单点
  • 配置最大连接数问题

    • 配置文件中 TCP 最大连接数被我设置成 2049 啦,这就导致 TCP 同时保持的最大连接只有 2049 个,限制了客户端连接成功率
  • 配置 tcp 保活时长问题

    • TCP 心跳最大时长我设置啦 30 秒
  • 其他配置参数不合理(分发,重试策略)
  • 服务器端口重用没有开启

优化后架构

优化功能点

emq 版本升级
  • emqx3.1 版本
  • 安装 / 部署
rpm -ivh emqx-centos7-v3.1.0.x86\_64.rpm
  • 启动 / 停止
service emqx start/stop/restart
  • 集群加入 / 离开
emqx_ctl cluster join emqx@ip
linux 系统调优
  • 参考官网
    https://emqttd-docs-zh-cn.rea…
  • emq 配置参数调优
haproxy 调优
  • 安装
[root@dz home]# yum install -y pcre-devel  bzip2-devel  gcc gcc-c++ make
[root@dz home]# tar -zxvf haproxy-1.8.13.tar.gz 
[root@dz home]# cd haproxy-1.8.13
[root@dz haproxy-1.8.13]# make TARGET=linux2628 PREFIX=/usr/local/haproxy
[root@dz haproxy-1.8.13]# make install PREFIX=/usr/local/haproxy
install -d "/usr/local/haproxy/sbin"
install haproxy  "/usr/local/haproxy/sbin"
install -d "/usr/local/haproxy/share/man"/man1
install -m 644 doc/haproxy.1 "/usr/local/haproxy/share/man"/man1
install -d "/usr/local/haproxy/doc/haproxy"
for x in configuration management architecture peers-v2.0 cookie-options lua WURFL-device-detection proxy-protocol linux-syn-cookies network-namespaces DeviceAtlas-device-detection 51Degrees-device-detection netscaler-client-ip-insertion-protocol peers close-options SPOE intro; do \
    install -m 644 doc/$x.txt "/usr/local/haproxy/doc/haproxy" ; \
done
[root@dz haproxy-1.8.13]# 
[root@dz haproxy-1.8.13]# /usr/local/haproxy/sbin/haproxy -v
HA-Proxy version 1.8.13 2018/07/30
Copyright 2000-2018 Willy Tarreau <willy@haproxy.org>

[root@dz haproxy-1.8.13]# 
[root@dz haproxy-1.8.13]# mkdir /etc/haproxy
[root@dz haproxy-1.8.13]# groupadd haproxy
[root@dz haproxy-1.8.13]# useradd -s /sbin/nologin -M -g haproxy haproxy // 添加 haproxy 运行 haproxy 账号并设置及属主与属组
[root@dz haproxy-1.8.13]# cp examples/haproxy.init /etc/init.d/haproxy
[root@dz haproxy-1.8.13]# chmod 755 /etc/init.d/haproxy
[root@dz haproxy-1.8.13]# chkconfig --add haproxy
[root@dz haproxy-1.8.13]# cp /usr/local/haproxy/sbin/haproxy /usr/sbin/
  • 配置参数调优
vim /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
#默认配置和全局配置
defaults
    log                     global
    option                  dontlognull
    option http-server-close
    # option forwardfor
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         60s
    timeout client          2m
    timeout server          2m
    timeout http-keep-alive 10s
    timeout check           10s
#配置前端的监听端口
frontend emqtt-front
    bind *:1883
    maxconn     1000000
    mode tcp
    default_backend emqtt-backend
#配置后端的转发端口
backend emqtt-backend
    balance roundrobin
    # balance source
    server emq1 10.199.96.149:9883 check inter 100000 fall 2 rise 5 weight 1
    server emq2 10.199.96.150:9883 check inter 100000 fall 2 rise 5 weight 1
    server emq3 10.199.96.152:9883 check inter 100000 fall 2 rise 5 weight 1
    # source 0.0.0.0 usesrc clientip

frontend emqtt-admin-front
    bind *:18083
    mode http
    default_backend emqtt-admin-backend

backend emqtt-admin-backend
    mode http
    balance roundrobin
    server emq1 10.199.96.149:18083 check
    server emq2 10.199.96.150:18083 check
    server emq3 10.199.96.152:18083 check
#控制台配置
listen admin_stats
        stats   enable
        bind    *:8081
        mode    http
        option  httplog
        log     global
        maxconn 10
        stats   refresh 30s
        stats   uri /admin
        stats   realm haproxy
        stats   auth admin:admin
        stats   hide-version
        stats   admin if TRUE
  • 启动
systemctl start haproxy
8 月 08 09:14:34 dz haproxy[3223]: /etc/rc.d/init.d/haproxy: 第 26 行:[: =: 期待一元表达式
修改 /etc/rc.d/init.d/haproxy 文件
[${NETWORKING} = "no" ] && exit 0
改成
["${NETWORKING}" = "no" ] && exit 0
systemctl daemon-reload
  • 开机自启动
chkconfig haproxy on
  • 高可用
cat << EOF >> /etc/sysctl.conf
fs.file-max=2097152 
fs.nr_open=2097152
net.core.somaxconn=32768
net.ipv4.tcp_max_syn_backlog=16384
net.core.netdev_max_backlog=16384
net.ipv4.ip_local_port_range=500 65535
net.core.rmem_default=262144
net.core.wmem_default=262144
net.core.rmem_max=16777216
net.core.wmem_max=16777216
net.core.optmem_max=16777216
net.ipv4.tcp_rmem=1024 4096 16777216
net.ipv4.tcp_wmem=1024 4096 16777216
net.nf_conntrack_max=1000000
net.netfilter.nf_conntrack_max=1000000
net.netfilter.nf_conntrack_tcp_timeout_time_wait=30
net.ipv4.tcp_max_tw_buckets=1048576
net.ipv4.tcp_fin_timeout = 15
EOF


cat << EOF >>/etc/security/limits.conf
*      soft   nofile      1048576
*      hard   nofile      1048576
EOF

echo DefaultLimitNOFILE=1048576 >>/etc/systemd/system.conf 

echo session required /usr/lib64/security/pam_limits.so >>/etc/pam.d/login

cat << EOF >> /etc/sysctl.conf
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_tw_recycle=1
net.ipv4.tcp_fin_timeout=30
net.ipv4.tcp_syncookies = 1
EOF
  • keepalived 部署
yum install keepalived
  • 增加配置文件
### /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     huangmeng@dyjs.com
    #  failover@firewall.loc
    #  sysadmin@firewall.loc
   }
   notification_email_from huangmeng4520@163.com
   smtp_server smtp.163.com
   smtp_connect_timeout 30
   router_id mqtt40
   vrrp_skip_check_adv_addr
#    vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    mcast_src_ip 172.16.40.22
    # unicast_peer {
    # 172.18.40.41 ##(对端 IP 地址)此地址一定不能忘记,vrrp need use
    # }
    virtual_ipaddress {
        172.16.40.24/24
        # 192.168.200.16
        # 192.168.200.17
        # 192.168.200.18
    }
}
  • 开机启动
systemctl enable keepalived
  • 172.16.40.24 作为虚拟 IPkeepalive 的服务器提供外网访问

测试工具

依赖安装
yum -y install ncurses-devel openssl-devel unixODBC-devel gcc-c++  
mkdir -p /app/install $$ cd /app/install/
wget http://erlang.org/download/otp_src_21.3.tar.gz
tar -xvzf otp_src_19.0.tar.gz
cd otp_src_19.0
./configure --prefix=/usr/local/erlang --with-ssl -enable-threads -enable-smmp-support -enable-kernel-poll --enable-hipe --without-javac
make && make install
配置 erl 环境变量
vim /etc/profile
# erlang
export ERLPATH=/usr/local/erlang
export PATH=$ERLPATH/bin:$PATH
source /etc/profile
erl -v
安装压测软件
yum -y install git
cd /app/install
git clone https://github.com/emqtt/emqtt_benchmark.git
cd emqtt_benchmark
make
## 调整系统参数并且开始压测
sysctl -w net.ipv4.ip_local_port_range="500 65535"
echo 1000000 > /proc/sys/fs/nr_open
ulimit -n 1000000
测试指令与结果展示
[root@zhanghp2 emqtt_benchmark]# ./emqtt_bench pub  -h 192.168.199.132 -p 1883 -c 500 -I 10 -t bench21/%i -s 256
connected: 1
connected: 2
connected: 3
connected: 4
connected: 5
connected: 6
connected: 7
connected: 8
connected: 9
connected: 10
connected: 11
connected: 12
connected: 13
connected: 14
connected: 15
connected: 16
测试命令参数说明
./emqtt_bench pub --help
./emqtt_bench sub --help
报错总结
`conneted:``138`
`client``49863``EXIT: {shutdown,eaddrnotavail}`
`# 分配不了端口 `
`[error] [Client <``0.7267``.``0``>] CONNACK Timeout!`
`client``7590``EXIT: {shutdown,connack_timeout}`
`# 链接超时 `
conneted:`191`
client``49810``EXIT: {shutdown,econnrefused}`
#链接被拒绝
#查看端口号使用命令 
netstat -npta | grep < 端口号 > 
#查看端口号使用数量 
netstat -npta |grep < 端口号 > | wc -l

监控页面

附件

  • haproxy https://pan.baidu.com/s/10opa… 提取码: hmyq
  • erl21 https://pan.baidu.com/s/1DzbI… 提取码: j485
正文完
 0