By Sajid Qureshi, Alibaba Cloud Community Blog author.
RabbitMQ is a free and open-source message-broker software that originally implements the AMQP (Advanced Message Queuing Protocol). RabbitMQ is written in the Erlang programming language. It has been extended in order to support other protocols like STOMP (Streaming Text Oriented Messaging Protocol) and MQTT (Message Queuing Telemetry Transport) and other protocols.
In a nutshell, RabbitMQ can be understood as the message queuing software that offers support for sending and receiving messages between distributed systems, applications, and services. It also supports client interfaces and libraries for major programming languages such as Python, NodeJS, Java, PHP etc.
In this tutorial, we will show you how to setup a RabbitMQ cluster on Alibaba Cloud ECS instances installed with CentOS 7. Here we are using three CentOS servers.
Follow the steps outlined below to setup a RabbitMQ cluster on ECS instances running CenterOS 7.
We recommend you to install any new packages on a newly updated server. Upgrade all the available packages on each server using the sudo yum -y update
command.
For an easy installation we suppose the three servers are :
Next, you will need to set up a host file for each server and map each server IP address as a hostname. Edit the /etc/hosts
file using the nano text editor with the sudo nano /etc/hosts
command.
Add the following configuration there:
YourServerIP_1 node01
YourServerIP_2 node02
YourServerIP_3 node03
Replace YourServerIP
in the above code with your actual IP address then save and exit.
We will install the RabbitMQ server from the EPEL repository. So, you will need to first add the EPEL repository to the CentOS 7 system. Run the sudo yum -y install epel-release
command to do so.
Now install the RabbitMQ server to all the nodes using the sudo yum -y install rabbitmq-server
command.
After the installation is completed, you will need to start the RabbitMQ services and enable it to launch at the boot time. Run the following commands one-by-one and they'll do the job for you.
sudo systemctl start rabbitmq-server
sudo systemctl enable rabbitmq-server
Now the RabbitMQ server has been successfully installed on all the nodes. Next, you will need to enable management plugins. By doing so you will be able to monitor and handle RabbitMQ server from a web browser. RabbitMQ management plugins run on default TCP port 15672. Let's enable the RabbitMQ management plugins using the sudo rabbitmq-plugins enable rabbitmq_management
command.
Next, you will need to restart the RabbitMQ service using the sudo systemctl restart rabbitmq-server
command.
Now you have installed the RabbitMQ server and also enabled the management plugins successfully. Next, you will need to modify the firewalld rules to open the port required by RabbitMQ. Execute the following commands one by one and they will do the job for you.
sudo firewall-cmd --add-port=15672/tcp --permanent`
sudo firewall-cmd --add-port=5672/tcp --permanent`
sudo firewall-cmd --add-port={4369/tcp,25672/tcp} --permanent
Now reload the firewalld services to apply all the changes using the sudo firewall-cmd --reload
command.
All the dependencies are installed and everything is configured. Now we are ready to set up a RabbitMQ cluster. The .erlang.cookie
file should be the same on all the nodes to set up a RabbitMQ cluster. We will copy the .erlang.cookie
file from node01 to the other two nodes to avoid any errors.
Execute the following commands to copy .erlang.cookie
file from node01.
scp /var/lib/rabbitmq/.erlang.cookie root@node02:/var/lib/rabbitmq/
scp /var/lib/rabbitmq/.erlang.cookie root@node03:/var/lib/rabbitmq/
You should not get any error on both servers. Next, you will need to set up both servers node02 and node03 to join the cluster node01. In order to do so, execute all the below-given commands on node02 and node03 servers. First of all, restart the RabbitMQ service and then stop the app using the following commands.
sudo systemctl restart rabbitmq-server
sudo rabbitmqctl stop_app
Execute the following command to join the cluster on node1.
sudo rabbitmqctl join_cluster rabbit@node01
. You can start the app using the sudo rabbitmqctl start_app
command. Now, you have created the RabbitMQ cluster using node01, node02, and, node03 as the members of this cluster. You can also check the status of the RabbitMQ cluster using the sudo rabbitmqctl cluster_status
command. If nothing goes wrong then you won't get any errors.
Create a new admin user for your RabbitMQ cluster. You don't have to create a user on all the nodes, you will only need to create a new user on any one node and it will automatically reflect on all the other nodes. Here we will create a new admin user from node01 using the following command.
sudo rabbitmqctl add_user YourUsername YourPassword
Please replace the YourUsername
and YourPassword
according to your choice.
Next, set up the new user as an administrator with the following command:
sudo rabbitmqctl set_user_tags YourUsername administrator
Now you will need to allow your user permission to modify all the vhosts. Run the following command and it'll do the job for you.
sudo rabbitmqctl set_permissions -p / YourUsername ".*" ".*" ".*"
Now our new admin user is created, so you can delete the default guest user if you want with the sudo rabbitmqctl delete_user guest
command.
You will need to set up queue mirroring because if the node that hosts queue master fails then the oldest mirror will be allocated as the new master. Queue mirroring is configured using HA (Highly Available) polices. You need to create a policy which matches them and sets policy keys ha-mode
and (optionally) ha-params
.
You can setup ha policy named ha-all
which all queues on the RabbitMQ cluster will be mirroring to all nodes on the cluster. Execute the following command to do so.
sudo rabbitmqctl set_policy ha-all ".*" '{"ha-mode":"all"}'
Below is a policy where queues whose names begin with "two" are mirrored to any two nodes in the cluster.
sudo rabbitmqctl set_policy ha-two "^two\." \ '{"ha-mode":"exactly","ha-params":2,"ha-sync-mode":"automatic"}'
Below policy where queues whose names begin with "nodes" are mirrored to specific nodes (here node02 and node03) in the cluster.
sudo rabbitmqctl set_policy ha-nodes "^nodes\." \ '{"ha-mode":"nodes","ha-params":["rabbit@node02", "rabbit@node03"]}'
You can list all the available policies using the sudo rabbitmqctl list_policies;
command. You can also remove any policy using the sudo rabbitmqctl clear_policy ha-two
command.
We have installed and configured the RabbitMQ cluster, and now we are ready to test it through the web. Open up your favorite web browser and visit YourServerIP address with port 15672 http://YourServerIP1:15672/
. You should see the RabbitMQ login web page like this:
Enter your admin username and password that we set up earlier and then click on the Login button. On the next interface, you will see the RabbitMQ admin dashboard. All the cluster nodes will appear on the admin dashboard. Next, you can deploy more cluster nodes and manage them from the admin dashboard. You can also manage your users and HA policies from the RabbitMQ admin dashboard.
If you want to check your current ha policies then go to the RabbitMQ admin dashboard and click on the Admin tab and then click on the Policies menu on the right side. You will get all the RabbitMQ ha policies that we have created earlier.
In this tutorial, you have learned how to install and configure a RabbitMQ cluster on Alibaba Cloud ECS instances installed with CentOS 7. You also learned to set up queue mirroring with high-availability policies and manage them. We hope now you have enough knowledge to work with RabbitMQ cluster.
2,599 posts | 762 followers
FollowAlibaba Clouder - September 30, 2018
Alibaba Clouder - June 13, 2019
Alibaba Cloud Community - January 17, 2024
Alibaba Clouder - April 20, 2018
Alibaba Clouder - July 24, 2020
Alibaba Clouder - December 29, 2020
2,599 posts | 762 followers
FollowElastic and secure virtual cloud servers to cater all your cloud hosting needs.
Learn MoreVPN Gateway is an Internet-based service that establishes a connection between a VPC and your on-premise data center.
Learn MoreLearn More
More Posts by Alibaba Clouder