How to create RabbitMQ cluster in Docker/AWS Linux

Saurabh Singh
4 min readFeb 16, 2020

--

RabbitMQ is the most widely deployed open source message broker.

RabbitMQ is lightweight and easy to deploy on premises and in the cloud. It supports multiple messaging protocols. RabbitMQ can be deployed in distributed and federated configurations to meet high-scale, high-availability requirements.

RabbitMQ runs on many operating systems and cloud environments, and provides a wide range of developer tools for most popular languages.

We are going to create a 3 nodes cluster running in docker containers, deployed in AWS EC2 Linux instance.

Connect to the server, please run following commands to update the packages and install docker.

  • Update the installed packages and package cache on your instance
sudo yum update -y
  • Install and start docker service
sudo yum install docker
sudo service docker start
  • Add the ec2-user to the docker group so you can execute Docker commands without using sudo
sudo usermod -a -G docker ec2-user
  • Verify that the ec2-user can run Docker commands without sudo
docker info
  • Create custom bridge network — mynet
docker network create mynetdocker network inspect mynet

Now the server is ready for the RabbitMQ installation.

Node 1

rabbit1 is hostname

erlang cookie is defined to facilitate cluster

Port 15672 is mapped to server to access GUI for RabbitMQ management.

docker run -d --hostname rabbit1 --name myrabbit1 -p 15672:15672 -p 5672:5672 --network mynet -e RABBITMQ_ERLANG_COOKIE=’rabbitcookie’ rabbitmq:management

Node 2

docker run -d --hostname rabbit2 --name myrabbit2 -p 5673:5672 --link myrabbit1:rabbit1 --network mynet -e RABBITMQ_ERLANG_COOKIE=’rabbitcookie’ rabbitmq:management

Node 3

docker run -d --hostname rabbit3 --name myrabbit3 -p 5674:5672 — link myrabbit1:rabbit1 --link myrabbit2:rabbit2 --network mynet -e RABBITMQ_ERLANG_COOKIE=’rabbitcookie’ rabbitmq:management

Now the nodes are up and running but cluster is still not configured. We have to connect to each running instance to configure and restart each node. We do following steps:

  • connect to running instance
  • stop the instance
  • join cluster
  • start the instance

Node 1

docker exec -it myrabbit1 bashrabbitmqctl stop_apprabbitmqctl resetrabbitmqctl start_app

Node 2

docker exec -it myrabbit2 bashrabbitmqctl stop_apprabbitmqctl resetrabbitmqctl join_cluster --ram rabbit@rabbit1rabbitmqctl start_app

Node 3

docker exec -it myrabbit3 bashrabbitmqctl stop_apprabbitmqctl resetrabbitmqctl join_cluster --ram rabbit@rabbit1rabbitmqctl start_app

Cluster is now configured and running. You can access the GUI od RabbitMQ manager using the IP address or name of server at port 15672.

Custom username/password

Default username/password is guest/guest respectively, but you can set custom credentials while setting up the nodes by using following attributes

-e RABBITMQ_DEFAULT_USER=username -e RABBITMQ_DEFAULT_PASS=password

command may look something like this

docker run -d --hostname rabbit1 --name myrabbit1 -p 15672:15672 -p 5672:5672 --network mynet -e RABBITMQ_ERLANG_COOKIE=’rabbitcookie’ rabbitmq:management -e RABBITMQ_DEFAULT_USER=username -e RABBITMQ_DEFAULT_PASS=password

You can find more details on other configuration settings and environment variables at https://registry.hub.docker.com/_/rabbitmq/

Connect to any one of the running instance and check the cluster status using following command

rabbitmdctl cluster_status
root@rabbit1:/# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit1 ...
Basics
Cluster name: rabbit@rabbit1Disk Nodesrabbit@rabbit1
rabbit@rabbit2
rabbit@rabbit3
Running Nodesrabbit@rabbit1
rabbit@rabbit2
rabbit@rabbit3
Versionsrabbit@rabbit1: RabbitMQ 3.8.2 on Erlang 22.2.6
rabbit@rabbit2: RabbitMQ on Erlang
rabbit@rabbit3: RabbitMQ on Erlang
Alarms(none)Network Partitions(none)ListenersNode: rabbit@rabbit1, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@rabbit1, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: rabbit@rabbit1, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Feature flagsFlag: drop_unroutable_metric, state: enabled
Flag: empty_basic_get_metric, state: enabled
Flag: implicit_default_bindings, state: enabled
Flag: quorum_queue, state: enabled
Flag: virtual_host_metadata, state: enabled

Cheers.

--

--