Setup RabbitMQ Cluster on Ubuntu 20.04

Updated on August 3, 2021
Setup RabbitMQ Cluster on Ubuntu 20.04 header image

Introduction

RabbitMQ is an open-source message queuing software implemented in Erlang OTP. It implements the AMQP (Advanced Message Queuing Protocol), a messaging protocol that enables conforming client applications to communicate with conforming messaging middleware brokers. In addition, RabbitMQ uses plugins to communicate with popular messaging solutions like MQTT (Message Queuing Telemetry Transport) and Streaming Text Oriented Messaging Protocol.

Failure of a single server instance can cause a single point of failure. To avoid this situation, we install a RabbitMQ cluster. This achieves higher availability and throughput compared to a single instance VPS service. In this article, you'll learn how to install and configure a RabbitMQ Cluster on Ubuntu 20.04 Server.

Prerequisites

1. Configure Cluster Hosts

For all the deployed servers, one will be used to join the other nodes to the cluster. All the nodes in the cluster are peer nodes; that is, all the nodes are the same, and none supersedes the other functionally. In this article, we will use two peer nodes, for example:

192.0.2.10 rabbitmq-1
192.0.2.11 rabbitmq-2

On node rabbitmq-1 change the static hostname to rabbitmq-1.

$ sudo hostnamectl set-hostname rabbitmq-1 --static

On node rabbitmq-2 change the static hostname to rabbitmq-2.

$ sudo hostnamectl set-hostname rabbitmq-2 --static

SSH to all the servers as a non-root user with sudo access and edit host file /etc/hosts in all the nodes.

$ sudo nano /etc/hosts

Add the following code in the file.

192.0.2.10 rabbitmq-1
192.0.2.11 rabbitmq-2

Ping all the nodes using their hostnames.

$ ping -c 3 rabbitmq-1
$ ping -c 3 rabbitmq-2

Reboot all the nodes for hostname changes to reflect.

$ sudo reboot

RabbitMQ peer nodes and CLI tools use a cookie to determine whether they can communicate with each other. For two nodes to communicate, they must have the same shared secret called the Erlang cookie.

Copy Cookie from rabbitmq-1 node to rabbitmq-2. If there are other nodes, copy to them too.

Go to rabbitmq-1 node, open the file and copy all the contents.

$ sudo nano /var/lib/rabbitmq/.erlang.cookie

Go to rabbitmq-2 node, open the file and paste contents from rabbitmq-1. Then, do that to all other remaining nodes in cases of any.

$ sudo nano /var/lib/rabbitmq/.erlang.cookie

3. Configure RabbitMQ to Join Cluster

All the other RabbitMQ peer nodes should be reconfigured to join the cluster on the first node rabbitmq-1. This step should be for all nodes except for the first one.

Restart RabbitMQ service.

$ sudo systemctl restart rabbitmq-server

Stop the application.

$ sudo rabbitmqctl stop_app

Reset rabbitmq.

$ sudo rabbitmqctl reset

Join the node to the cluster.

$ sudo rabbitmqctl join_cluster rabbit@rabbitmq-1

Start the application process.

$ sudo rabbitmqctl start_app

Check the status of the cluster.

$ sudo rabbitmqctl cluster_status

4. Configure RabbitMQ Queue Mirroring

Create a HA policy on all the nodes to allow queue mirroring to all nodes in the cluster. Perform this on all nodes.

$ sudo rabbitmqctl set_policy ha-all "." '{"ha-mode":"all"}'

List configured policies.

$ sudo rabbitmqctl list_policies

Enable RabbitMQ Management Dashboard for all the nodes to access all peer node stats from a specific dashboard.

$ sudo rabbitmq-plugins enable rabbitmq_management

After enabling the plugins for the web management portal, you can go to your browser and access the page through http://your_IP:15672. Example:

http://192.0.2.11:15672

Login with admin as your username and SecurePassword as your password. Make sure you modify the SecurePassword to your own password. Log in is only possible for servers with administrator account activated. Step 5 explains how to.

5. Add Administration Account (Optional)

To access your dashboard from your preferred server, you can add an administrator account to access it.

Default user guest can only log in via localhost. Create an administrator account to access the dashboard. Make sure you modify the SecurePassword to your own password.

$ sudo rabbitmqctl add_user admin SecurePassword
$ sudo rabbitmqctl set_user_tags admin administrator

Conclusion

You have now installed RabbitMQ Cluster on your server. Your administrator account on all the peer nodes will enable you to have all access privileges to the server. You can now configure your RabbitMQ clusters from the dashboard.

More Information

For more information on RabbitMQ clustering, please see the official documentation.