How to Setup RabbitMQ Cluster on Ubuntu 20.04. This article will explain what RabbitMQ is with its benefits and how to install RabbitMQ Cluster.
What is RabbitMQ?
RabbitMQ is a message broker software that stores and passes on the messages to the receiver only when it connects with the queue and there is a demand for transferring the message. It is open source software that works as a broker between the sender and the receiver. The software is highly beneficial as it helps reduce the excessive load and improves the delivery time.
Before the introduction of RabbitMQ, TCP connections were used to connect the application components and deliver messages. However, the technique was not good enough, and dropping the idea was the only solution due to certain limitations. The sender could forward the information only when he received the request. If the receiver went down, the sender kept transferring messages unless a connection was generated. As a result, there were high chances of system failure. Thus, to avoid such mishaps, the Message Queuing system was introduced.
RabbitMQ software defines and configures the queue to help create a better communication system between the sender and the receiver.
2. Delivery and order message guaranteed. It helps applications to connect with the queues and transfer single or multiple messages without bogging down the system. The messaging queue system runs a consumer/producer model where the sender (producer) publishes a message to the queue to forward and process by the receiver (consumer). The message can be a simple test or binary information that is retained in the queue system unless the consumer passes a transfer request. The main purpose of introducing this system was to create a space that could store or fill in N number of messages from the sender without failing the system. The idea got successful and came into practice. However, there is a minor twist, RabbitMQ uses exchanges to receive messages from the sender to the queue.
3. Type of RabbitMQ exchanges: Direct, Fanout, Topic and Headers. These exchanges receive messages from the sender and use binary keys to link queues and transfer information.
4. Deployment in public and private clouds is much easier
5. Supports multiple languages
6. Uses HTTP API, command line tool, and UI for management and monitoring of the Message Queuing system.
7. Supports Asynchronous Messaging
8. Flexible Routing.
9. The RabbitMQ broker is scalable and flexible.
How Does RabbitMQ Cluster Works?
Next in how to Setup RabbitMQ Cluster on Ubuntu 20.04 is to introduce RabbitMQ cluster. It is a group of nodes that share users, queues, exchanges, virtual hosts, runtime parameters, bindings and additional distributed states. The software must include infra for the availability of the RabbitMQ queues. A minimum of three nodes cluster must be formed to create it. It helps in retaining quorum amid the various servers. Unlike other clusters, RabbitMQ cluster mode does not follow the concept of master and slave.
In the beginning, a single node is used to run all RabbitMQ brokers. As the nodes start connecting, a cluster is generated. However, these clusters can later be converted into a single broker as per the preference. The only exception in the RabbitMQ cluster mode is message queues reside on a single node by default. However, these are reachable to all nodes in the cluster.
To begin the process, make sure you check two things:
Hostname Resolution
RabbitMQ nodes have individual names to address each other in the cluster mode. They often use command line tools to discover and direct one another. Make sure that the hostname of each cluster must be resolved. DNS records or Localhost files methods are used in most cases for Hostname resolution. One can also opt for Erlang VM if there is any restriction in the DNS record method.
Port Access
Each RabbitMQ node must be linked to the ports for smooth communication and functioning. The binding between the nodes and the port helps the receiver connect with the message broker and transfer request. For open connections, one can use any of the three sources:
Command Line Tools.
Client Libraries.
RabbitMQ nodes.
Next, we will show you how to install two node RabbitMQ cluster on Ubuntu 20.04.
Before setting up the RabbitMQ cluster, you will need to set up hostname resolution on both servers. So each server can communicate with each other by hostname.
To do so, edit the /etc/hosts file on both servers:
nano /etc/hosts
Add the following lines:
104.219.55.246 server1
104.219.54.225 server2
Save and close the file when you are finished.
Where:
104.219.55.246 is the IP address of the first server.
104.219.54.225 is the IP address of the second server.
RabbitMQ provides a management plugin to monitor and manage RabbitMQ nodes and clusters through a web browser. But, it is disabled by default. So you will need to enable it on both servers.
Run the following command to enable the RabbitMQ management plugin:
rabbitmq-plugins enable rabbitmq_management
You will get the following output:
Enabling plugins on node rabbit@server1:
rabbitmq_management
The following plugins have been configured:
rabbitmq_management
rabbitmq_management_agent
rabbitmq_web_dispatch
Applying plugin configuration to rabbit@server1...
The following plugins have been enabled:
rabbitmq_management
rabbitmq_management_agent
rabbitmq_web_dispatch
started 3 plugins.
Next, restart the RabbitMQ service to apply the changes:
systemctl restart rabbitmq-server
At this point, the RabbitMQ management plugin is enabled and listens on port 15672. You can check it with the following command:
In order to access the RabbitMQ web interface, you will need to set an admin username and password. To set an admin user, password, and proper permissions, run the following command on server1:
Setting policy "ha-all" for pattern ".*" to "{"ha-mode":"all"}" with priority "0" for vhost "/" ...
Now, run the following command to list all policies:
rabbitmqctl list_policies;
You will get the following output:
Listing policies for vhost "/" ...
vhost name pattern apply-to definition priority
/ ha-all .* all {"ha-mode":"all"} 0
Access RabbitMQ Dashboard
Now, open your web browser and access the RabbitMQ dashboard using the URL http://104.219.55.246:15672/. You will be redirected to the RabbitMQ login page:
Provide your admin username, password, and click on the Login button. You should see the RabbitMQ cluster dashboard on the following page:
You have successfully setup RabbitMQ Cluster on Ubuntu 20.04.
How to Setup RabbitMQ Cluster on Ubuntu 20.04 Conclusion
In the above guide, we explained how to set up a two-node RabbitMQ cluster on Ubuntu 20.04 server. You can now add more nodes to scale the cluster. For more information, visit the RabbitMQ Clustering guide.
I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.