How to Set up RabbitMQ Cluster on Ubuntu 18.04 LTS

RabbitMQ is an open source message-broker software that originally implemented the AMQP (Advanced Message Queuing Protocol) protocol, and while it has been developed and extended in order to support other protocols such as STOMP (Streaming Text Oriented Messaging Protocol), and MQTT (Message Queuing Telemetry Transport).

A message-queueing / message-broker software is used for sending and receiving messages between distributed systems, applications, and services. RabbitMQ is written in the Erlang programming language, it offers support for client interfaces and libraries for all major programming languages including Python, NodeJS, Java, PHP etc.

In this tutorial, I will show you how to set up a RabbitMQ Cluster on Ubuntu 18.04 Server. I will install a RabbitMQ Cluster using three Ubuntu servers, enable the RabbitMQ Management, and Setup the HA policy for all nodes.

Prerequisites

  • 3 or more Ubuntu 18.04 Servers
    • 10.0.15.21 hakase-ubuntu01
    • 10.0.15.22 hakase-ubuntu02
    • 10.0.15.23 hakase-ubuntu03
  • Root privileges

What we will do?

  1. Setup Hosts File
  2. Install RabbitMQ Server
  3. Enable Management Plugins
  4. Setup UFW Firewall
  5. Setup RabbitMQ Cluster
  6. Setup New Administrator User
  7. RabbitMQ Setup Queue Mirroring
  8. Testing

Step 1 - Setup Hosts File

In this step, we will edit the '/etc/hosts' file on all servers and map each server IP address as a hostname.

Edit the '/etc/hosts' file using vim editor.

sudo vim /etc/hosts

Now paste the following configuration there.

10.0.15.21 hakase-ubuntu01
10.0.15.22 hakase-ubuntu02
10.0.15.23 hakase-ubuntu03

Save and exit.

Step 2 - Install RabbitMQ Server

Before installing RabbitMQ server, make sure all repositories are updated.

Run the following command.

sudo apt update
sudo apt upgrade

Now install the RabbitMQ server packages from the Ubuntu repository using the apt command below.

sudo apt install rabbitmq-server -y

And after the installation is complete, start the RabbitMQ service and enable it to launch everytime at system boot.

sudo systemctl start rabbitmq-server
sudo systemctl enable rabbitmq-server

The RabbitMQ Server has been installed on Ubuntu 18.04.

Install RabbitMQ Server

Step 3 - Enable RabbitMQ Management Plugins

In this step, we will enable RabbitMQ management plugins. It's an interface that allows you to monitor and handle RabbitMQ server from the web browser, running on the default TCP port '15672'.

Enable the RabbitMQ management plugins by running the command below.

sudo rabbitmq-plugins enable rabbitmq_management

Make sure there is no error, then restart the RabbitMQ service.

sudo systemctl restart rabbitmq-server

RabbitMQ Management plugins have been enabled.

Enable RabbitMQ Management Plugins

Step 4 - Setup UFW Firewall

In this tutorial, we will enable the Ubuntu UFW firewall. We need to open some ports that will be used by the RabbitMQ server.

Add the ssh service to the UFW firewall and enable the firewall service.

sudo ufw allow ssh
sudo ufw enable

Now add new RabbitMQ tcp ports '5672,15672,4369,25672'.

sudo ufw allow 5672,15672,4369,25672/tcp

Then check the UFW firewall ports list.

sudo ufw status

Setup UFW Firewall

The Ubuntu UFW firewall configuration has been completed, and we're ready to set up the RabbitMQ Cluster.

Step 5 - Setup RabbitMQ Cluster

In order to setup the RabbitMQ cluster, we need to make sure the '.erlang.cookie' file is same on all nodes. We will copy the '.erlang.cookie' file on the '/var/lib/rabbitmq' directory from 'hakase-ubuntu01' to other node 'hakase-ubuntu02' and 'hakase-ubuntu03'.

Copy the '.erlang.cookie' file using scp commands from the 'hakase-ubuntu01'.

scp /var/lib/rabbitmq/.erlang.cookie [email protected]:/var/lib/rabbitmq/
scp /var/lib/rabbitmq/.erlang.cookie [email protected]:/var/lib/rabbitmq/

Make sure there is no error on both servers.

Setup RabbitMQ Cluster

Next, we need to setup 'hakase-ubuntu02' and 'hakase-ubuntu03' to join the cluster 'hakase-ubuntu01'.

Note:

  • Run commands below on hakase-ubuntu02' and 'hakase-ubuntu03' servers.

Restart the RabbitMQ service and stop the app.

sudo systemctl restart rabbitmq-server
sudo rabbitmqctl stop_app

Now let RabbitMQ server on both nodes join the cluster on 'hakase-ubuntu01', then start the app.

sudo rabbitmqctl join_cluster [email protected]
sudo rabbitmqctl start_app

When it's complete, check the RabbitMQ cluster status.

sudo rabbitmqctl cluster_status

And you will get the results as below.

start rabbitmq

The RabbitMQ Cluster has been created, with hakase-ubuntu01, hakase-ubuntu02, and hakase-ubuntu03 as members.

Step 6 - Setup New Administrator User

In this tutorial, we will create a new admin user for our RabbitMQ server and delete the default 'guest' user. We will be creating a new user from 'hakase-ubuntu01', and it will be automatically replicated to all nodes on the cluster.

Add a new user named 'hakase' with password '[email protected]'.

sudo rabbitmqctl add_user hakase [email protected]

Setup the 'hakase' user as an administrator.

sudo rabbitmqctl set_user_tags hakase administrator

And grant the 'hakase' user permission to modify, write, and read all vhosts.

sudo rabbitmqctl set_permissions -p / hakase ".*" ".*" ".*"

Now delete the default 'guest' user.

sudo rabbitmqctl delete_user guest

And check all available users.

sudo rabbitmqctl list_users

And you will get the result as below.

Add admin user

A new RabbitMQ administrator user has been created, and the default 'guest' user is deleted.

Step 7 - RabbitMQ Setup Queue Mirroring

By default, contents of a queue within a RabbitMQ cluster are located on a single node (the node on which the queue was declared).

This setup is must, we need to configure the 'ha policy' cluster for queue mirroring and replication to all cluster nodes. If the node that hosts queue master fails, the oldest mirror will be promoted to the new master as long as it synchronized, depends on the 'ha-mode' and 'ha-params' policies.

Below some example about RabbitMQ ha policies.

Setup ha policy named 'ha-all' which all queues on the RabbitMQ cluster will be mirroring to all nodes on the cluster.

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

Setup ha policy named 'ha-two' which all queue name start with 'two.' will be mirroring to the two nodes on the cluster.

sudo rabbitmqctl set_policy ha-two "^two\." \
   '{"ha-mode":"exactly","ha-params":2,"ha-sync-mode":"automatic"}'

Setup a high availability policy named 'ha-nodes' which will contain all queues where the name starts with 'nodes.' We will be mirroring to two specific nodes 'hakase-ubuntu02' and 'hakase-ubuntu03' in the cluster.

sudo rabbitmqctl set_policy ha-nodes "^nodes\." \
   '{"ha-mode":"nodes","ha-params":["[email protected]", "[email protected]"]}'

RabbitMQ list ha policies.

sudo rabbitmqctl list_policies;

RabbitMQ delete specific ha policy.

sudo rabbitmqctl clear_policy ha-two

Setup replication

Step 8 - Testing

Open your web browser and type the IP address of the node with port '15672'.

http://10.0.15.21:15672/

Type the username 'hakase' with password '[email protected]'.

Login to RabbitMQ

And you will get the RabbitMQ admin dashboard as shown below.

RabbitMQ dashboard

All cluster nodes status is up and running.

Now click on the 'Admin' tab menu, and click the 'Users' menu on the side.

And you will get hakase user on the list.

User list

Now click on the 'Admin' tab menu, and click the 'Policies' menu on the side.

And you will get all RabbitMQ ha policies we've created.

Policies

The installation and configuration of RabbitMQ Cluster on Ubuntu 18.04 servers have been completed successfully.

Reference

About Muhammad Arul

Muhammad Arul is a freelance system administrator and technical writer. He is working with Linux Environments for more than 5 years, an Open Source enthusiast and highly motivated on Linux installation and troubleshooting. Mostly working with RedHat/CentOS Linux and Ubuntu/Debian, Nginx and Apache web server, Proxmox, Zimbra Administration, and Website Optimization. Currently learning about OpenStack and Container Technology.

Share this page:

Suggested articles

2 Comment(s)

Add comment

Comments

By: Alan Deng at: 2018-10-09 14:22:38

Hi,

Is it possible to do this using Hyper-V and running all three VMs on the same machine?

 

Thanks

By: Muhammad at: 2018-10-11 21:30:37

Sure, it's possible to run the RabbitMQ cluster thourgh vms.