(Last Updated On: December 10, 2018)

In this blog post, we will discuss the process of installing and configuring a RabbitMQ Cluster on Ubuntu 18.04 LTS server. RabbitMQ is an open source message broker software that implements the Advanced Message Queuing Protocol (AMQP) and Streaming Text Oriented Messaging Protocol, Message Queuing Telemetry Transport, and other protocols via a Plugins.

The work of a Messaging broker is to receive messages from publishers (applications that publish them) and route them to consumers (applications that process them). AMQP is a messaging protocol that enables conforming client applications to communicate with conforming messaging middleware brokers.

By setting a RabbitMQ cluster on Ubuntu 18.04, you avoid a single point of failure and achieve higher throughput when compared to single instance RabbitMQ setup. Without further ado, let’s dive to RabbitMQ cluster setup on Ubuntu 18.04 LTS

Setup Requirements

This setup has the following requirements

  • Installed Ubuntu 18.04 LTS servers
  • At least two RabbitMQ servers
  • A user with sudo privileges
  • The servers should have internet access

This setup of RabbitMQ Cluster on Ubuntu 18.04 is based on two servers with the following IP addresses and hostnames.

ServerHostnameIP Address
MQ Server 1mq1.example.com192.168.121.11
MQ Server 2mq2.example.com192.168.121.8

Step 1: Setup Hostnames and DNS

The first step in the installation of the RabbitMQ cluster on Ubuntu 18.04 is to configure correct hostnames and DNS.

MQ Server 1:

sudo hostnamectl set-hostname mq1.example.com --static

MQ Server 2:

sudo hostnamectl set-hostname mq2.example.com --static

If you don’t have a DNS server, you can add the records to the /etc/hosts file

echo "192.168.121.11 mq1.example.com mq1" >> /etc/hosts
echo "192.168.121.8 mq2.example.com mq2" >> /etc/hosts

Then update your systems:

sudo apt update
sudo apt -y upgrade

Step 2: Install RabbitMQ Server on both nodes

Login to your servers and install RabbitMQ server on all nodes using the guide below:

How to install Latest RabbitMQ Server on Ubuntu 18.04 LTS

There are two parts to installing RabbitMQ on Ubuntu 18.04:

  1. Installing Erlang/OTP
  2. Installing RabbitMQ server

The status of your RabbitMQ servers should be running:

$ sudo systemctl status rabbitmq-server.service 
* rabbitmq-server.service - RabbitMQ broker
   Loaded: loaded (/lib/systemd/system/rabbitmq-server.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2018-12-10 10:33:30 PST; 1min 28s ago
 Main PID: 9634 (beam.smp)
   Status: "Initialized"
    Tasks: 87 (limit: 505)
   CGroup: /system.slice/rabbitmq-server.service
           |-9634 /usr/lib/erlang/erts-10.1/bin/beam.smp -W w -A 64 -MBas ageffcbf -MHas ageffcbf -MBlmbcs 512 -MHlmbcs 512 -MMmcs 30 -P 1048576 -t 500
           |-9733 /usr/lib/erlang/erts-10.1/bin/epmd -daemon
           |-9883 erl_child_setup 32768
           |-9906 inet_gethost 4
           `-9907 inet_gethost 4

Dec 10 10:33:28 mq1.example.com rabbitmq-server[9634]:   ##  ##
Dec 10 10:33:28 mq1.example.com rabbitmq-server[9634]:   ##  ##      RabbitMQ 3.7.9. Copyright (C) 2007-2018 Pivotal Software, Inc.
Dec 10 10:33:28 mq1.example.com rabbitmq-server[9634]:   ##########  Licensed under the MPL.  See http://www.rabbitmq.com/
Dec 10 10:33:28 mq1.example.com rabbitmq-server[9634]:   ######  ##
Dec 10 10:33:28 mq1.example.com rabbitmq-server[9634]:   ##########  Logs: /var/log/rabbitmq/[email protected]
Dec 10 10:33:28 mq1.example.com rabbitmq-server[9634]:                     /var/log/rabbitmq/[email protected]_upgrade.log
Dec 10 10:33:28 mq1.example.com rabbitmq-server[9634]:               Starting broker...
Dec 10 10:33:30 mq1.example.com rabbitmq-server[9634]: systemd unit for activation check: "rabbitmq-server.service"
Dec 10 10:33:30 mq1.example.com systemd[1]: Started RabbitMQ broker.
Dec 10 10:33:30 mq1.example.com rabbitmq-server[9634]:  completed with 0 plugins.

Step 3: Copy RabbitMQ Server 1 Cookie RabbitMQ Server2

For RabbitMQ cluster to work, all the nodes participating in the cluster should have the same Cookie. Copy Cookie on your first node to all other nodes in the cluster.

On mq1 run:

sudo scp /var/lib/rabbitmq/.erlang.cookie mq2:/var/lib/rabbitmq/.erlang.cookie

Step 4: Reset RabbitMQ on Node2

Reconfigure RabbitMQ on Node 2 and join it to the cluster.

1. Restart RabbitMQ service

sudo systemctl restart rabbitmq-server

2. Stop application

$ sudo rabbitmqctl stop_app
Stopping rabbit application on node [email protected] ...

3. Reset rabbitmq

$ sudo rabbitmqctl reset
Resetting node [email protected] ...

3. Join the node to cluster

$ sudo rabbitmqctl join_cluster [email protected]
Clustering node [email protected] with [email protected]

4. Start the application process

$ sudo rabbitmqctl start_app
Starting node [email protected] ...
 completed with 0 plugins.

Check Cluster Status:

[email protected]:~# rabbitmqctl cluster_status
warning: the VM is running with native name encoding of latin1 which may cause Elixir to malfunction as it expects utf8. Please ensure your locale is set to UTF-8 (which can be verified by running "locale" in your shell)
Cluster status of node [email protected] ...
[{nodes,[{disc,[[email protected],[email protected]]}]},
 {running_nodes,[[email protected],[email protected]]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]},
 {alarms,[{[email protected],[]},{[email protected],[]}]}]

Step 5: Configure RabbitMQ HA Policy

Create a policy that allows for queues mirroring to all nodes in the cluster.

$ sudo rabbitmqctl set_policy ha-all ".*" '{"ha-mode":"all"}'
Setting policy "ha-all" for pattern ".*" to "{"ha-mode":"all"}" with priority "0" for vhost "/" ...

You can list configured policies using:

[email protected]:~# sudo rabbitmqctl list_policies
Listing policies for vhost "/" ...
vhost	name	pattern	apply-to	definition	priority
/	ha-all	.*	all	{"ha-mode":"all"}	0

To drop a policy, use:

sudo rabbitmqctl clear_policy <policyname>

Step 5: Testing

Finally, test your RabbitMQ cluster setup on Ubuntu 18.04.  Enable the RabbitMQ Management Web dashboard for easy management.

sudo rabbitmq-plugins enable rabbitmq_management

If you have an active UFW firewall, allow TCP ports 5672 and 15672

sudo ufw allow proto tcp from any to any port 5672,15672

Access it by opening the URL http://[server IP|Hostname]:15672

By default, the guest user exists and can connect only from localhost. You can login with this user locally with the password “guest”

To be able to login on the network, create an admin user like below:

sudo rabbitmqctl add_user admin StrongPassword
sudo rabbitmqctl set_user_tags admin administrator

Use the created user to login to the RabbitMQ management interface. You should get status of all Cluster nodes.

More details about a node can also be viewed from the web console.

If you login to RabbitMQ node2 and check for created RabbitMQ users, you should see output similar to below.

# rabbitmqctl list_users
Listing users ...
user	tags
admin	[administrator]
guest	[administrator]

You have successfully installed RabbitMQ cluster on Ubuntu 18.04. Enjoy and stay connected for more informative content.