Files
Ubuntu_docs/rabbitmq_replication_setup_instructions.md
2025-02-21 02:46:41 +05:00

6.7 KiB
Raw Blame History

Heres a step-by-step guide to setting up a RabbitMQ cluster with replication correctly:


Step 1: Prepare the Servers

Ensure you have at least three nodes (recommended for HA) with:

  • Ubuntu 22.04 (or a supported OS)
  • Sufficient CPU/RAM based on workload
  • Open necessary firewall ports (5672, 15672, 25672)

Set hostnames for clarity:

sudo hostnamectl set-hostname rabbitmq-node1  # Change for each node

Step 2: Install Erlang and RabbitMQ

Run the following on all nodes:

1. Add the RabbitMQ Repository

sudo apt update
sudo apt install -y curl gnupg
curl -fsSL https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey | sudo tee /usr/share/keyrings/rabbitmq-key.asc
echo "deb [signed-by=/usr/share/keyrings/rabbitmq-key.asc] https://packagecloud.io/rabbitmq/rabbitmq-server/ubuntu/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/rabbitmq.list

2. Install Erlang and RabbitMQ

sudo apt update
sudo apt install -y rabbitmq-server

3. Enable RabbitMQ

sudo systemctl enable --now rabbitmq-server

Step 3: Configure Clustering

1. Stop RabbitMQ on All Nodes

sudo systemctl stop rabbitmq-server

Run on all nodes (same cookie ensures clustering works):

echo "MY_CLUSTER_COOKIE" | sudo tee /var/lib/rabbitmq/.erlang.cookie
sudo chmod 600 /var/lib/rabbitmq/.erlang.cookie

Replace "MY_CLUSTER_COOKIE" with a strong, identical value on all nodes.


Step 4: Join Nodes to the Cluster

Perform this on nodes 2 and 3, joining them to node1:

sudo rabbitmqctl stop_app
sudo rabbitmqctl join_cluster rabbit@rabbitmq-node1
sudo rabbitmqctl start_app

Check cluster status:

sudo rabbitmqctl cluster_status

Step 5: Enable High Availability (HA) Mirroring

To replicate all queues, run on any one node:

rabbitmqctl set_policy ha-all "^.*" '{"ha-mode":"all","ha-sync-mode":"automatic"}'

This ensures all queues are replicated across all nodes.


Step 6: Enable Management UI

Run on each node to enable the web interface:

sudo rabbitmq-plugins enable rabbitmq_management

Access at:
http://[NODE_IP]:15672
(Default login: guest/guest, change this for security.)


Step 7: Test the Cluster

Run on each node:

rabbitmqctl list_queues

Queues should be visible and synchronized.


Step 8: Enable Auto-Recovery (Optional)

Edit /etc/rabbitmq/rabbitmq.conf on each node and add:

cluster_formation.peer_discovery_backend = classic_config
cluster_formation.classic_config.nodes.1 = rabbit@rabbitmq-node1
cluster_formation.classic_config.nodes.2 = rabbit@rabbitmq-node2
cluster_formation.classic_config.nodes.3 = rabbit@rabbitmq-node3

Then restart RabbitMQ:

sudo systemctl restart rabbitmq-server

1. Create an Admin User

rabbitmqctl add_user admin StrongPassword123!
rabbitmqctl set_user_tags admin administrator
rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"

Then disable the guest account:

rabbitmqctl delete_user guest

2. Enable TLS (Optional)

For security, configure TLS in /etc/rabbitmq/rabbitmq.conf. Refer to RabbitMQs TLS guide.


Step 10: Setup Monitoring (Optional)

Install Prometheus & Grafana or use RabbitMQ Prometheus plugin:

sudo rabbitmq-plugins enable rabbitmq_prometheus

Now your RabbitMQ cluster is fully set up with replication and high availability! 🚀






Instructions to fix unexpected configuration errors

If you can't find the RabbitMQ config file under /etc/rabbitmq, it may not exist by default. You need to create it manually. Here's how:

1 Create the Configuration File

RabbitMQ uses either a .conf file (modern format) or a .config file (legacy format). The recommended format is .conf.

Create the file if it doesn't exist:

sudo nano /etc/rabbitmq/rabbitmq.conf

Then add the following cluster configuration:

cluster_formation.peer_discovery_backend = classic_config
cluster_formation.classic_config.nodes.1 = rabbit@rabbitmq-main
cluster_formation.classic_config.nodes.2 = rabbit@rabbitmq-replica

Save and exit (CTRL+X, then Y, then Enter).


2 Set Correct Permissions

Ensure the RabbitMQ user can read it:

sudo chown rabbitmq:rabbitmq /etc/rabbitmq/rabbitmq.conf
sudo chmod 644 /etc/rabbitmq/rabbitmq.conf

3 Restart RabbitMQ

After modifying the configuration, restart the RabbitMQ service:

sudo systemctl restart rabbitmq-server

Check the status:

sudo systemctl status rabbitmq-server

4 Verify the Cluster Configuration

After restarting, verify that clustering is working:

rabbitmqctl cluster_status

If the nodes are listed correctly, your setup is working.


5 If Using the Legacy .config Format

Some older installations use an Erlang-based configuration file (rabbitmq.config). If you prefer that, create:

sudo nano /etc/rabbitmq/rabbitmq.config

Add this:

[
 {rabbit, [
   {cluster_formation, [
     {peer_discovery_backend, classic_config},
     {classic_config, [
       {nodes, ['rabbit@rabbitmq-main', 'rabbit@rabbitmq-replica']}
     ]}
   ]}
 ]}
].

Then restart RabbitMQ:

sudo systemctl restart rabbitmq-server

🔍 Troubleshooting

RabbitMQ doesn't restart?
Check logs for errors:

sudo journalctl -u rabbitmq-server --no-pager | tail -50

Cluster not forming?
Try forcing a node to join manually:

rabbitmqctl stop_app
rabbitmqctl join_cluster rabbit@rabbitmq-main
rabbitmqctl start_app





Instructions to fix unexpected management UI authentication errors

stackoverflow answer link

Answer

Cannot login with guest/guest credentials

I had the same Problem..

I installed RabbitMQ and Enabled Web Interface also but still couldn't sign in with any user i newly created, this is because you need to be administrator to access this.

Do not create any config file and mess with it..

This is what i did then,

  1. Add a new/fresh user, say user test and password test:
rabbitmqctl add_user test test
  1. Give administrative access to the new user:
rabbitmqctl set_user_tags test administrator
  1. Set permission to newly created user:
rabbitmqctl set_permissions -p / test ".*" ".*" ".*"

That's it, enjoy :)