RabbitMQ is a robust message broker widely used in distributed systems. This guide provides a detailed, step-by-step tutorial on installing RabbitMQ 3.10.8 with Erlang 25.2.3 on Debian 12 and configuring a Master-Slave cluster. The setup will involve two servers: a Master at 10.11.33.83
and a Slave at 10.11.33.93
.
1. Configure Hostname Resolution
Ensure both servers can resolve each other’s hostnames by editing /etc/hosts
.
On both Master and Slave servers, add the following lines to /etc/hosts
:
0 1 2 3 4 5 6 7 8 9 10 11 |
127.0.0.1 localhost # IPv6 settings ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters # RabbitMQ Cluster Nodes 10.11.33.83 rabbit.master.x1.demo.internal 10.11.33.93 rabbit.slave.x1.demo.internal |
Save and exit the file.
Verify the hostname resolution by running:
0 1 2 3 |
ping -c 3 rabbit.master.x1.demo.internal ping -c 3 rabbit.slave.x1.demo.internal |
2. Install Required Dependencies
Update the package list and install necessary dependencies:
0 1 2 |
sudo apt update && sudo apt install -y curl gnupg apt-transport-https |
3. Install Erlang 25.2.3
RabbitMQ requires Erlang. Install version 25.2.3 by adding the RabbitMQ package repository.
0 1 2 3 4 |
curl -fsSL https://packages.erlang-solutions.com/ubuntu/erlang_solutions.asc | sudo tee /usr/share/keyrings/erlang.gpg > /dev/null echo "deb [signed-by=/usr/share/keyrings/erlang.gpg] https://packages.erlang-solutions.com/debian $(lsb_release -cs) contrib" | sudo tee /etc/apt/sources.list.d/erlang.list sudo apt update sudo apt install -y erlang=1:25.2.3-1 |
Verify installation:
0 1 2 |
erl -version |
4. Install RabbitMQ 3.10.8
Add the RabbitMQ repository:
0 1 2 3 4 |
curl -fsSL https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey | sudo tee /usr/share/keyrings/rabbitmq-keyring.asc > /dev/null echo "deb [signed-by=/usr/share/keyrings/rabbitmq-keyring.asc] https://packagecloud.io/rabbitmq/rabbitmq-server/debian/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/rabbitmq.list sudo apt update sudo apt install -y rabbitmq-server=3.10.8-1 |
Enable and start RabbitMQ:
0 1 2 |
sudo systemctl enable --now rabbitmq-server |
Check RabbitMQ status:
0 1 2 |
sudo systemctl status rabbitmq-server |
5. Configure RabbitMQ Environment
Set RabbitMQ to use long hostnames and define a fixed node name.
On Master (10.11.33.83):
0 1 2 |
sudo nano /etc/rabbitmq/rabbitmq-env.conf |
Add:
0 1 2 3 |
RABBITMQ_NODENAME=rabbit@rabbit.master.x1.demo.internal RABBITMQ_USE_LONGNAME=true |
Save and exit.
On Slave (10.11.33.93):
0 1 2 |
sudo nano /etc/rabbitmq/rabbitmq-env.conf |
Add:
0 1 2 3 |
RABBITMQ_NODENAME=rabbit@rabbit.slave.x1.demo.internal RABBITMQ_USE_LONGNAME=true |
Save and exit.
Restart RabbitMQ on both servers:
0 1 2 |
sudo systemctl restart rabbitmq-server |
Verify the node name:
0 1 2 |
sudo rabbitmqctl status |
6. Set Erlang Cookie for Clustering
Ensure the Erlang cookie is the same on both servers:
0 1 2 3 4 |
echo "SUPER_SECRET_COOKIE_VALUE" | sudo tee /var/lib/rabbitmq/.erlang.cookie sudo chmod 600 /var/lib/rabbitmq/.erlang.cookie sudo chown rabbitmq:rabbitmq /var/lib/rabbitmq/.erlang.cookie |
Restart RabbitMQ:
0 1 2 |
sudo systemctl restart rabbitmq-server |
7. Enable RabbitMQ Management Plugin
Enable the RabbitMQ management plugin to monitor the cluster:
0 1 2 3 |
sudo rabbitmq-plugins enable rabbitmq_management sudo systemctl restart rabbitmq-server |
Access the RabbitMQ web UI at:
0 1 |
http://<server-ip>:15672 |
Default credentials:
- Username:
guest
- Password:
guest
8. Configure Cluster (Master-Slave)
Ensure RabbitMQ is running on the Slave before attempting to stop the application:
0 1 2 |
sudo systemctl start rabbitmq-server |
Stop RabbitMQ application on the Slave server:
0 1 2 |
sudo rabbitmqctl stop_app |
Join the Slave node to the Master:
0 1 2 3 |
sudo rabbitmqctl join_cluster rabbit@rabbit.master.x1.demo.internal sudo rabbitmqctl start_app |
Check the cluster status:
0 1 2 |
sudo rabbitmqctl cluster_status |
9. Configure RabbitMQ Cluster in /etc/rabbitmq/rabbitmq.conf
By default, RabbitMQ does not create /etc/rabbitmq/rabbitmq.conf
, so you must create it manually:
0 1 2 |
sudo nano /etc/rabbitmq/rabbitmq.conf |
Add the following lines to define the cluster settings:
0 1 2 3 4 |
cluster_formation.peer_discovery_backend = rabbit_peer_discovery_classic_config cluster_formation.classic_config.nodes.1 = rabbit@rabbit.master.x1.demo.internal cluster_formation.classic_config.nodes.2 = rabbit@rabbit.slave.x1.demo.internal |
Save the file and restart RabbitMQ on both servers:
0 1 2 |
sudo systemctl restart rabbitmq-server |
Verify the configuration:
0 1 2 |
sudo rabbitmqctl cluster_status |
10. Set a High Availability Policy
On the Master node, apply an HA policy:
0 1 2 |
sudo rabbitmqctl set_policy ha-all ".*" '{"ha-mode":"all","ha-sync-mode":"automatic"}' --priority 0 |
Verify policies:
0 1 2 |
sudo rabbitmqctl list_policies |
11. Test the Cluster Setup
On Master, create a test queue:
0 1 2 |
sudo rabbitadmin -u guest -p guest declare queue name=test-ha durable=true |
Check queues on both Master and Slave:
0 1 2 |
sudo rabbitmqctl list_queues |
12. Simulate Failover
To test failover, stop RabbitMQ on the Master:
0 1 2 |
sudo systemctl stop rabbitmq-server |
Publish a message from the Slave:
0 1 2 |
sudo rabbitmqadmin -u guest -p guest publish routing_key=test-ha payload="Hello, RabbitMQ!" |
Restart the Master and verify the message persists:
0 1 2 3 |
sudo systemctl start rabbitmq-server rabbitmqctl list_queues |