当前位置:   article > 正文

EMQX-5.3.1单机集群部署并基于Nginx实现负载均衡_emqx nginx

emqx nginx

本例单机集群部署使用三个节点,分别为node1、node2、node3

一、安装与配置

1 创建数据目录

  1. mkdir -p node1/data node1/logs
  2. mkdir -p node2/data node2/logs
  3. mkdir -p mode3/data node3/logs

2 数据目录授权

  1. chown 1000 node1/ node2/ node3/
  2. chown 1000 node1/data/ node1/logs/
  3. chown 1000 node2/data/ node2/logs/
  4. chown 1000 node3/data/ node3/logs/

3 创建docker-compose.yml文件

  1. version: '3'
  2. services:
  3. emqx1:
  4. image: emqx:5.3.1
  5. container_name: emqx1
  6. environment:
  7. - "EMQX_NODE_NAME=emqx@node1.emqx.io"
  8. - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
  9. - "EMQX_CLUSTER__STATIC__SEEDS=[emqx@node1.emqx.io,emqx@node2.emqx.io,emqx@node3.emqx.io]"
  10. healthcheck:
  11. test: ["CMD", "/opt/emqx/bin/emqx ctl", "status"]
  12. interval: 5s
  13. timeout: 25s
  14. retries: 5
  15. networks:
  16. emqx-bridge:
  17. aliases:
  18. - node1.emqx.io
  19. ports:
  20. - 1883:1883
  21. - 8083:8083
  22. - 8084:8084
  23. - 8883:8883
  24. - 18083:18083
  25. volumes:
  26. - /etc/localtime:/etc/localtime:ro
  27. - ./node1/logs:/opt/emqx/log
  28. - ./node1/data:/opt/emqx/data
  29. emqx2:
  30. image: emqx:5.3.1
  31. container_name: emqx2
  32. environment:
  33. - "EMQX_NODE_NAME=emqx@node2.emqx.io"
  34. - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
  35. - "EMQX_CLUSTER__STATIC__SEEDS=[emqx@node1.emqx.io,emqx@node2.emqx.io,emqx@node3.emqx.io]"
  36. healthcheck:
  37. test: ["CMD", "/opt/emqx/bin/emqx ctl", "status"]
  38. interval: 5s
  39. timeout: 25s
  40. retries: 5
  41. networks:
  42. emqx-bridge:
  43. aliases:
  44. - node2.emqx.io
  45. ports:
  46. - 1873:1883
  47. - 8073:8083
  48. - 8074:8084
  49. - 8873:8883
  50. - 18073:18083
  51. volumes:
  52. - /etc/localtime:/etc/localtime:ro
  53. - ./node2/logs:/opt/emqx/log
  54. - ./node2/data:/opt/emqx/data
  55. emqx3:
  56. image: emqx:5.3.1
  57. container_name: emqx3
  58. environment:
  59. - "EMQX_NODE_NAME=emqx@node3.emqx.io"
  60. - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
  61. - "EMQX_CLUSTER__STATIC__SEEDS=[emqx@node1.emqx.io,emqx@node2.emqx.io,emqx@node3.emqx.io]"
  62. healthcheck:
  63. test: ["CMD", "/opt/emqx/bin/emqx ctl", "status"]
  64. interval: 5s
  65. timeout: 25s
  66. retries: 5
  67. networks:
  68. emqx-bridge:
  69. aliases:
  70. - node3.emqx.io
  71. ports:
  72. - 1863:1883
  73. - 8063:8083
  74. - 8064:8084
  75. - 8863:8883
  76. - 18063:18083
  77. volumes:
  78. - /etc/localtime:/etc/localtime:ro
  79. - ./node3/logs:/opt/emqx/log
  80. - ./node3/data:/opt/emqx/data
  81. networks:
  82. emqx-bridge:
  83. driver: bridge

4 启动运行EMQX

docker-compose up -d

5 EMQX的Dashboard访问地址 http://${ip}:${port}   参考:http://192.168.1.xxx:18083/

默认用户名: admin

默认密码: public

登录后提示修改密码

6 配置Nginx的负载均衡策略

nginx.conf内容如下:

  1. user nginx;
  2. worker_processes auto;
  3. error_log /var/log/nginx/error.log warn;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. }
  8. stream {
  9. upstream emqx_tcp_cluster {
  10. server 10.10.1.100:1883 weight=1 max_fails=3 fail_timeout=30s;
  11. server 10.10.1.110:1873 weight=1 max_fails=3 fail_timeout=30s;
  12. server 10.10.1.120:1863 weight=1 max_fails=3 fail_timeout=30s;
  13. }
  14. server {
  15. listen 1893;
  16. proxy_pass emqx_tcp_cluster;
  17. proxy_buffer_size 8k;
  18. tcp_nodelay on;
  19. }
  20. }

7 扩展其他负载均衡策略

随机轮询

随机轮询就是从待选列表中随机分配连接

  1. upstream emqx_tcp_cluster {
  2. random;
  3. # emqx的三个实例
  4. server 10.10.1.100:1883;
  5. server 10.10.1.110:1873;
  6. server 10.10.1.120:1863;
  7. }

带权轮询

部署的服务器存在性能差异,我们可以通过配置权重 weight 来修改轮询的几率

  1. upstream emqx_tcp_cluster {
  2. # emqx的三个实例
  3. server 10.10.1.100:1883 weight=1;
  4. server 10.10.1.110:1873 weight=2;
  5. server 10.10.1.120:1863 weight=3;
  6. }

least_conn 策略

最小连接数策略,优先分配给获得连接数较少的服务器

  1. upstream emqx_tcp_cluster {
  2. least_conn;
  3. # emqx的三个实例
  4. server 10.10.1.100:1883;
  5. server 10.10.1.110:1873;
  6. server 10.10.1.120:1863;
  7. }

IP Hash 策略

有特殊需求,希望同一个客户端连接至同一个服务器,则可以使用 IP Hash 策略

  1. upstream emqx_tcp_cluster {
  2. ip_hash;
  3. # emqx的三个实例
  4. server 10.10.1.100:1883;
  5. server 10.10.1.110:1873;
  6. server 10.10.1.120:1863;
  7. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/188517?site
推荐阅读
相关标签
  

闽ICP备14008679号