一、前言背景
由于公司产品发展需要,需要一款消息推送的服务,通过网上查询资料,以及将来在Iot方面的应用前景,隧选定使用EMQ服务。
二、EMQX集群搭建
为了方便,所有的按照搭建都是在docker容器环境下进行的,使用docker-compose进行的服务管理
EMQX静态模式集群搭建开始
废话不多说,各种心酸流程略过……docker-compose.yml配置文件如下
三、为增加EMQX集群增加Redis认证
首先在上面的配置文件中,增加redis认证的配置
四、为emqx集群增加haproxy代理
配置详解:
注意:这里使用的haproxy镜像是有ssl证书的,官方镜像上我没有找到,是自己构建的一个镜像,在构建这个镜像过程中,遇到很多问题,也是由于第一次构建镜像,经验不足,在进行docker build过程中,总是报I/0异常,换了阿里的镜像源和科大的都还是不行,但是镜像运行起来后,进入镜像然后执行apk update等是没有问题,所以我中这种方式构建了一个新的ssl镜像。
haproxy代理配置详解:
五、查看haproxy监控平台和访问emqx的dashboard
访问:http://localhost:8404/haproxy?stats 用户名/密码:admin/password
访问emqx的控制台:http://localhost:18083
六、完整配置文件如下:
version: '3'
services:
emqx1:
image: emqx/emqx:v4.0.0
environment:
- "EMQX_NAME=emqx"
- "EMQX_HOST=node1.emqx.io"
- "EMQX_CLUSTER__DISCOVERY=static"
- "EMQX_CLUSTER__STATIC__SEEDS=emqx@node1.emqx.io,emqx@node2.emqx.io"
- "EMQX_LOADED_PLUGINS=emqx_auth_redis,emqx_recon,emqx_rule_engine,emqx_retainer,emqx_management,emqx_dashboard"
- "EMQX_ALLOW_ANONYMOUS=false"
- "EMQX_AUTH__REDIS__SERVER=emqx_redis:6379"
- "EMQX_AUTH__REDIS__DATABASE=0"
- "EMQX_AUTH__REDIS__PASSWORD=123456"
- "EMQX_AUTH__REDIS__PASSWORD_HASH=salt,sha256"
- "EMQX_AUTH__REDIS__AUTH_CMD=HMGET mqtt_user:%u password salt"
- "EMQX_ZONE__EXTERNAL__RETRY_INTERVAL=2s"
- "EMQX_MQTT__MAX_TOPIC_ALIAS=10"
- "EMQX_MQTT__MAX_PACKET_SIZE=1MB"
security_opt:
- apparmor=unconfined
healthcheck:
test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
interval: 5s
timeout: 25s
retries: 5
depends_on:
- emqx_redis
networks:
emqx-bridge:
aliases:
- node1.emqx.io
emqx2:
image: emqx/emqx:v4.0.0
environment:
- "EMQX_NAME=emqx"
- "EMQX_HOST=node2.emqx.io"
- "EMQX_CLUSTER__DISCOVERY=static"
- "EMQX_CLUSTER__STATIC__SEEDS=emqx@node1.emqx.io,emqx@node2.emqx.io"
- "EMQX_LOADED_PLUGINS=emqx_auth_redis,emqx_recon,emqx_rule_engine,emqx_retainer,emqx_management,emqx_dashboard"
- "EMQX_ALLOW_ANONYMOUS=false"
- "EMQX_AUTH__REDIS__SERVER=emqx_redis:6379"
- "EMQX_AUTH__REDIS__DATABASE=0"
- "EMQX_AUTH__REDIS__PASSWORD=123456"
- "EMQX_AUTH__REDIS__PASSWORD_HASH=salt,sha256"
- "EMQX_AUTH__REDIS__AUTH_CMD=HMGET mqtt_user:%u password salt"
- "EMQX_ZONE__EXTERNAL__RETRY_INTERVAL=2s"
- "EMQX_MQTT__MAX_TOPIC_ALIAS=10"
- "EMQX_MQTT__MAX_PACKET_SIZE=1MB"
security_opt:
- apparmor=unconfined
healthcheck:
test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
interval: 5s
timeout: 25s
retries: 5
depends_on:
- emqx_redis
networks:
emqx-bridge:
aliases:
- node2.emqx.io
emqx_redis:
image: redis:latest
ports:
- 6379:6379
security_opt:
- apparmor=unconfined
volumes:
- /media/liuzk/本地磁盘2/docker-container/redis/emqx_redis/data:/data:rw
command: redis-server --appendonly yes --requirepass "123456"
networks:
- emqx-bridge
emqx_haproxy:
image: ssl-haproxy-2.3:latest
security_opt:
- apparmor=unconfined
ports:
- 1883:1883
- 8404:8404
- 8883:8883
- 18083:18083
volumes:
- ./haproxy:/usr/local/etc/haproxy:ro
depends_on:
- emqx1
- emqx2
networks:
- emqx-bridge
networks:
emqx-bridge:
driver: bridge
haproxy配置文件如下:
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
user root
group root
daemon
listen admin_stats
stats enable
bind *:8404
mode http
option httplog
log global
maxconn 10
timeout client 5000
stats hide-version
stats refresh 30s
stats show-node
stats realm haproxy
stats auth admin:password
stats uri /haproxy?stats
frontend emqx_tcp
bind *:1883
mode tcp
maxconn 500000
timeout client 3h
option tcplog
log-format %T\ %t\ Some\ Text
default_backend emqx_tcp_cluster
frontend emqx_tcp_ssl
bind *:8883 ssl crt /etc/ssl/xip.io/xip.io.pem no-sslv3
mode tcp
maxconn 500000
timeout client 3h
option tcplog
log-format %T\ %t\ Some\ Text
default_backend emqx_tcp_cluster
frontend emqx_dashboard
bind *:18083
mode http
maxconn 50
timeout client 5000
default_backend emqx_dashboard_server
backend emqx_tcp_cluster
mode tcp
option clitcpka
balance leastconn
timeout connect 5000
timeout server 3h
timeout check 5000
server emqx1 emqx1:1883 check inter 2000 fall 2 rise 5 weight 1
server emqx2 emqx2:1883 check inter 2000 fall 2 rise 5 weight 1
backend emqx_dashboard_server
mode http
option http-keep-alive
balance source
timeout connect 50000
timeout server 50000
timeout check 5000
server emqx1 emqx1:18083 check inter 2000 fall 2 rise 5 weight 1
server emqx2 emqx2:18083 check inter 2000 fall 2 rise 5 weight 1