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
- Deploy two or more fully updated Rcs Ubuntu 20.04 Servers.
- Create a non-root user with sudo access on both servers.
- Install RabbitMQ Server Ubuntu 20.04 LTS on all the servers.
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-2On node rabbitmq-1 change the static hostname to rabbitmq-1.
$ sudo hostnamectl set-hostname rabbitmq-1 --staticOn node rabbitmq-2 change the static hostname to rabbitmq-2.
$ sudo hostnamectl set-hostname rabbitmq-2 --staticSSH 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/hostsAdd the following code in the file.
192.0.2.10 rabbitmq-1
192.0.2.11 rabbitmq-2Ping all the nodes using their hostnames.
$ ping -c 3 rabbitmq-1
$ ping -c 3 rabbitmq-2Reboot all the nodes for hostname changes to reflect.
$ sudo reboot2. Copy RabbitMQ Cookie
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.cookieGo 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.cookie3. 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-serverStop the application.
$ sudo rabbitmqctl stop_appReset rabbitmq.
$ sudo rabbitmqctl resetJoin the node to the cluster.
$ sudo rabbitmqctl join_cluster rabbit@rabbitmq-1Start the application process.
$ sudo rabbitmqctl start_appCheck the status of the cluster.
$ sudo rabbitmqctl cluster_status4. 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_policiesEnable RabbitMQ Management Dashboard for all the nodes to access all peer node stats from a specific dashboard.
$ sudo rabbitmq-plugins enable rabbitmq_managementAfter 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:15672Login 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 administratorConclusion
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.