团队内部使用Docker+Haproxy+SSL+Emqx搭建MQTT集群,一直没搞成功。写篇文章记录下整个搭建过程,方便以后查询。主要使用的方案是通过Haproxy实现Emqx负载均衡,使用自建SSL保证数据安全,使用Docker保证快速搭建,迁移等。
生成CA证书
首先生成一个CA机构证书,主要用来给服务证书签名用的
1 2 3 4 5 6 7 8 9 10
| openssl openssl genrsa -out ca.key 2048 # openssl rsa -in ca.key -out ca.key # # openssl req -new -x509 -key ca.key -out ca.crt -days 65536 # # cat ca.crt ca.key | tee ca.pem
|
生成服务端证书
生成Haproxy证书,保证TCP连接数据安全
1 2 3 4 5 6 7 8
| # openssl genrsa -out server.key 2048 # openssl req -new -key server.key -out server.csr # openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out server.crt -days 65535 # cat server.crt server.key | tee server.pem
|
安装Haproxy
Haproxy是高可用代理软件,它是一种比较流行的开源软件,基于TCP(四层)/HTTP(七层)协议的负载均衡代理解决方案,可以在Linux、FreeBSD等平台下运行,常见于跨多个服务器,比如:web、应用程序、数据库、MQ消息队列、MC数据库等等。通过多个Haproxy服务来提高服务的性能和可靠性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| sudo apt install haproxy vim /etc/haproxy/ global log 127.0.0.1 local0 log 127.0.0.1 local1 notice chroot /var/lib/haproxy stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners stats timeout 30s user haproxy group haproxy daemon
# Default SSL material locations #ca-base /etc/ssl/certs #crt-base /etc/ssl/private
# See: https://ssl-config.mozilla.org/ #ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384 #ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256 #ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
defaults log global mode tcp #option httplog option tcplog option http-server-close option dontlognull
timeout connect 300000 timeout client 300000 timeout server 300000 timeout check 300000 retries 3 #最大连接数10W maxconn 100000 errorfile 400 /etc/haproxy/errors/400.http errorfile 403 /etc/haproxy/errors/403.http errorfile 408 /etc/haproxy/errors/408.http errorfile 500 /etc/haproxy/errors/500.http errorfile 502 /etc/haproxy/errors/502.http errorfile 503 /etc/haproxy/errors/503.http
listen stats mode http bind *:7070 option httplog #启用日志记录HTTP请求,默认haproxy日志记录是不记录HTTP请求 stats enable # 启用状态统计报告 stats refresh 30s # 统计页面自动刷新时间 stats hide-version # 隐藏统计页面上HAProxy的版本信息 stats show-node stats auth admin:admin # 设置统计页面用户名和密码设置 stats uri /haproxy?stats #定义统计页面的URL,默认为/haproxy?stats stats admin if TRUE #如果认证通过就做管理功能,可以管理后端的服务器 stats realm “LOGIN” #登陆页面提示信息 #浏览器访问 http://localhost:8080/haproxy?stats 即可看到控制面板
backend backend_emqx_tcp mode tcp balance roundrobin server emqx_node_1 127.0.0.1:1883 check inter 10000 fall 2 rise 5 server emqx_node_2 127.0.0.1:1884 check inter 10000 fall 2 rise 5 server emqx_node_3 127.0.0.1:1885 check inter 10000 fall 2 rise 5 #frontend frontend_emqx_tcp # bind *:1883 # option tcplog # mode tcp # default_backend backend_emqx_tcp frontend frontend_emqx_tls bind *:8884 ssl crt /home/ubuntu/cert/server.pem #bind *:8884 option tcplog mode tcp default_backend backend_emqx_tcp
#http配置 #frontend balancer # bind 0.0.0.0:1883 # default_backend web_backends #backend web_backends # balance roundrobin # 启动了 10个容器 进行web测试, # server server1 192.168.8.14:80 check
|
主要看frontend中的证书配置和backend中的轮询负载均衡即可,这里我们使用1883,,1884,,1885搭建伪集群。而Emqx的搭建可以看我另外一篇文章
emqx集群搭建及nginx负载均衡。
测试
我们将生成的server.crt文件放到本地,使用mqttx连接,ssl mqttx连接成功,如下所示
