DEV Community

Cover image for A cluster-friendly MQTT Broker deployment with Docker
J Fowler
J Fowler

Posted on

A cluster-friendly MQTT Broker deployment with Docker

In this post, I show a docker deployment for an MQTT broker that will support adding multiple instances.

In a previous post, I showed a simple Dockerfile for deploying the Eclipse Mosquitto MQTT broker. While Mosquitto can be configured to operate in a cluster, there are other solutions that are more straightforward to implement.

We will be using EMQX, an open-source MQTT broker with built-in clustering capability and is known to work well in a Kubernetes environment.

The docker-compose file is show below:

version: '3'

services:
  emqx1:
    image: emqx:5.8.0
    container_name: emqx1
    hostname: emqx1
    environment:
      - "EMQX_NODE_NAME=emqx@172.20.0.2"
      - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
      - "EMQX_CLUSTER__STATIC__SEEDS=[emqx@172.20.0.2]"
    healthcheck:
      test: ["CMD", "/opt/emqx/bin/emqx", "ctl", "status"]
      interval: 5s
      timeout: 25s
      retries: 5
    networks:
      emqx-bridge:
        aliases:
          - emqx1
    ports:
      - 1883:1883
      - 8083:8083
      - 8084:8084
      - 8883:8883
      - 18083:18083

networks:
  emqx-bridge:
    driver: bridge
Enter fullscreen mode Exit fullscreen mode

This pulls the official emqx image, configures basic settings for the instance, and exposes necessary ports. You can also see that it sets up the settings and network that will be used in a cluster if more instances get added.

To start the broker, simply run:

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

You can check the status using:

docker exec -it emqx1 sh -c "emqx ctl cluster status"
Cluster status: #{running_nodes => ['emqx@172.20.0.2'],stopped_nodes => []}
Enter fullscreen mode Exit fullscreen mode

This is slightly different from the setup shown in the emqx docs. We had to add a hostname and use the internal container ip for the node name.

The full docker-compose file can be found in this repository.

Not sure about why we needed to change the node name to get the cluster status. If you know why, please add a comment below. What else would you change?

Thanks!

Top comments (0)