Deploying a RabbitMQ Cluster with Three Nodes Using Docker Compose

  ·   3 min read

Deploying a RabbitMQ cluster can significantly enhance your messaging capabilities by providing high availability and load balancing. In this article, we will walk through the steps to set up a RabbitMQ cluster with three nodes using Docker Compose. We will also investigate the fundamental configurations, and how to ensure that the nodes communicate properly.

Prerequisites

Before we start, make sure you have the following requirements:

  • Docker: Installed and running on your machine.
  • Docker Compose: Installed as well. You can check the versions by running docker --version and docker-compose --version in your terminal.

Docker Compose File Configuration

We will use a docker-compose.yml file to define our RabbitMQ nodes. The configuration for the three nodes would look something like this:

version: '3'

services:
  rabbitmq1:
    image: rabbitmq:3-management
    container_name: rabbitmq1
    environment:
      RABBITMQ_DEFAULT_USER: user
      RABBITMQ_DEFAULT_PASS: password
      RABBITMQ_ERLANG_COOKIE: secretcookie
    ports:
      - "5672:5672"
      - "15672:15672"
    networks:
      - rabbitmq-network
    volumes:
      - rabbitmq1-data:/var/lib/rabbitmq

  rabbitmq2:
    image: rabbitmq:3-management
    container_name: rabbitmq2
    environment:
      RABBITMQ_DEFAULT_USER: user
      RABBITMQ_DEFAULT_PASS: password
      RABBITMQ_ERLANG_COOKIE: secretcookie
    ports:
      - "5673:5672"
      - "15673:15672"
    networks:
      - rabbitmq-network
    volumes:
      - rabbitmq2-data:/var/lib/rabbitmq

  rabbitmq3:
    image: rabbitmq:3-management
    container_name: rabbitmq3
    environment:
      RABBITMQ_DEFAULT_USER: user
      RABBITMQ_DEFAULT_PASS: password
      RABBITMQ_ERLANG_COOKIE: secretcookie
    ports:
      - "5674:5672"
      - "15674:15672"
    networks:
      - rabbitmq-network
    volumes:
      - rabbitmq3-data:/var/lib/rabbitmq

networks:
  rabbitmq-network:
    driver: bridge

volumes:
  rabbitmq1-data:
  rabbitmq2-data:
  rabbitmq3-data:

Explanation of the Configuration

  • Services: We define three RabbitMQ instances (rabbitmq1, rabbitmq2, rabbitmq3).
  • Image: We use rabbitmq:3-management, which comes with the management plugin enabled, allowing us to access the management UI.
  • Environment Variables:
    • RABBITMQ_DEFAULT_USER and RABBITMQ_DEFAULT_PASS: Specifies the default user credentials.
    • RABBITMQ_ERLANG_COOKIE: A shared secret that RabbitMQ nodes need to communicate with one another. Ensure that this value is the same across all nodes.
  • Ports:
    • Port 5672 is the standard RabbitMQ port for messaging.
    • Ports 15672, 15673, and 15674 are used for the management UI of the respective nodes.
  • Networks: A custom bridge network is created (rabbitmq-network) to allow communication between the nodes.
  • Volumes: These are used to persist the RabbitMQ data on your host.

Starting the Cluster

To start the cluster, create a directory for your Docker Compose file, save the above content in a file named docker-compose.yml, and run:

docker-compose up -d

This command will run the containers in detached mode. You can check if the containers are running using:

docker ps

Clustering the Nodes

After the containers are up, we need to cluster the RabbitMQ nodes. Access any of the RabbitMQ management UIs in your web browser. For example, you can view RabbitMQ1 by navigating to http://localhost:15672.

  1. Log in with the username user and the password password.
  2. Go to the “Admin” tab.
  3. Select “Cluster” and choose “Reset” on each node to ensure they are all in a clean state.
  4. Use the CLI in the Docker container shell to join the nodes to the cluster.

For example, running the following commands inside the rabbitmq1 container:

docker exec -it rabbitmq1 bash

# Inside the rabbitmq1 container
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster rabbit@rabbitmq2
rabbitmqctl join_cluster rabbit@rabbitmq3
rabbitmqctl start_app

You should perform similar commands on rabbitmq2 and rabbitmq3, joining all nodes together.

Verifying the Cluster Status

To verify that the clustering has been successful, use:

rabbitmqctl cluster_status

This command will show the current status of the cluster including all running nodes.

Conclusion

You have successfully created a RabbitMQ cluster with three nodes using Docker Compose. This setup provides redundancy and horizontal scalability for your messaging needs. You can always expand this architecture based on your application requirements.

For more information, check out RabbitMQ’s official documentation and the Docker RabbitMQ image.

Resources