Skip to content

Deploy EMQX cluster with docker-compose

To create an EMQX cluster to provide MQTT service and keep it as simple as much, we can do it with docker and docker-compose only.

Create /volumes/emqx-data/ on each node for persistent storage of configuration. Make sure to give write permissions.

On the first node (192.168.31.61), create a docker-compose.yaml file with content below

version: '3'

services:
  emqx1:
    image: emqx:5.5.0
    container_name: emqx1
    environment:
    - "EMQX_NAME=emqx1"
    - "EMQX_HOST=192.168.31.61"
    - "EMQX_NODE_NAME=emqx1@192.168.31.61"
    - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
    - "EMQX_CLUSTER__STATIC__SEEDS=[emqx1@192.168.31.61,emqx2@192.168.31.62,emqx3@192.168.31.63]"
    network_mode: "host"
    healthcheck:
      test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
      interval: 5s
      timeout: 25s
      retries: 5
    volumes:
      - /volumes/emqx-data/emqx1_data:/opt/emqx/data

On the second node (192.168.31.62), create a similar docker-compose.yaml file with content below

version: '3'

services:
  emqx1:
    image: emqx:5.5.0
    container_name: emqx2
    environment:
    - "EMQX_NAME=emqx2"
    - "EMQX_HOST=192.168.31.62"
    - "EMQX_NODE_NAME=emqx2@192.168.31.62"
    - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
    - "EMQX_CLUSTER__STATIC__SEEDS=[emqx1@192.168.31.61,emqx2@192.168.31.62,emqx3@192.168.31.63]"
    network_mode: "host"
    healthcheck:
      test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
      interval: 5s
      timeout: 25s
      retries: 5
    volumes:
      - /volumes/emqx-data/emqx2_data:/opt/emqx/data

On the third node (192.168.31.63), similarly

version: '3'

services:
  emqx1:
    image: emqx:5.5.0
    container_name: emqx3
    environment:
    - "EMQX_NAME=emqx3"
    - "EMQX_HOST=192.168.31.63"
    - "EMQX_NODE_NAME=emqx3@192.168.31.63"
    - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
    - "EMQX_CLUSTER__STATIC__SEEDS=[emqx1@192.168.31.61,emqx2@192.168.31.62,emqx3@192.168.31.63]"
    network_mode: "host"
    healthcheck:
      test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
      interval: 5s
      timeout: 25s
      retries: 5
    volumes:
      - /volumes/emqx-data/emqx3_data:/opt/emqx/data

Enter the folder where the docker-compose.yaml file is located, execute docker compose up -d

If nothing goes wrong, you should be able to see a running container with STATUS as healthy.

By visiting the webpage http://192.168.31.61:18083 (default credentials: admin/public), you should be able to see 3 nodes in cluster.

If you need high-availability, you should carry on deploying nginx or HAProxy, or alternatively, utilize load balancers on cloud to distribute traffic to ports on these EMQX nodes.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*