How to set up a RabbitMQ cluster on an Ubuntu 20.04 cloud server?

18-02-2024 02:48:30

RabbitMQ is an open-source message queuing software based on Erlang OTP, utilizing the Advanced Message Queuing Protocol (AMQP) for communication between client applications and message middleware agents. In addition, RabbitMQ interacts with plugins and popular messaging protocols such as MQTT (Message Queuing Telemetry Transport) and STOMP (Streaming Text-Oriented Messaging Protocol).

We are aware that deploying on a single server may lead to a single point of failure, hence the need for a RabbitMQ cluster setup. A clustered approach offers higher reliability and throughput compared to a standalone setup. This guide outlines how to install and configure a RabbitMQ cluster on an Ubuntu 20.04 cloud server. You will require two updated Ubuntu 20.04 cloud servers, each with the RabbitMQ server installed.

Setting Hostnames for Nodes

All cloud servers within the cluster are considered equal nodes with similar roles and no functional differences. Assuming the IP addresses and hostnames of the two cloud servers are as follows:

  • 192.0.2.10 rabbitmq-1
  • 192.0.2.11 rabbitmq-2

Run the following commands to set the hostnames for the two cloud servers.

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

Then remotely log in to these servers and edit the /etc/hosts file.

$ sudo nano /etc/hosts

Add the following code snippets to map hostnames to IP addresses.

192.0.2.10 rabbitmq-1
192.0.2.11 rabbitmq-2

Restart the cloud servers for the hostname changes to take effect.

Copying RabbitMQ Cookie

Communication between RabbitMQ nodes relies on cookie technology. To enable nodes to communicate with each other, they must share the same secret text known as a cookie. Hence, we need to copy the cookie information from the rabbitmq-1 node to the rabbitmq-2 node.

Remotely log in to the rabbitmq-1 node, open the specified file, and copy its contents.

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

Next, log in to the rabbitmq-2 node, open the specified file, and paste the copied content from the rabbitmq-1 node. Repeat this process for any additional nodes.

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

Adding Nodes to the Cluster

Further steps involve adding all nodes to the cluster. Except for the first rabbitmq-1 node, log in to other nodes and execute the provided command.

$ sudo systemctl restart rabbitmq-server
$ sudo rabbitmqctl stop_app
$ sudo rabbitmqctl reset
$ sudo rabbitmqctl join_cluster rabbit@rabbitmq-1
$ sudo rabbitmqctl start_app

After completion, verify the status of the nodes.

$ sudo rabbitmqctl cluster_status

Creating Queue Mirrors

To ensure high availability, all nodes in the cluster need to create queue mirrors. After remotely logging into each node, execute the provided command.

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

Enabling Management Platform

Enable the RabbitMQ management platform on one cloud server to manage all nodes through a unified interface. Remember to update SecurePassword with the initial password.

$ sudo rabbitmq-plugins enable rabbitmq_management
$ sudo rabbitmqctl add_user admin SecurePassword
$ sudo rabbitmqctl set_user_tags admin administrator

At this stage, you have completed the installation and configuration of the RabbitMQ cluster. You can now log in using the administrator account to access the management platform and configure various settings for the RabbitMQ cluster.