Operating System CentOS

How to Set Up a RabbitMQ Cluster on AlmaLinux 9 or Rocky Linux 9

Build a three-node RabbitMQ 4.x cluster on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9 and replicate queues with quorum queues.

This guide builds a three-node RabbitMQ cluster on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9, using the RabbitMQ team's own RPM repositories for RabbitMQ 4.x and a matching Erlang. At the end you will have three brokers that share users, virtual hosts and exchanges, and queues that are replicated across the nodes with quorum queues, so the loss of one server does not stop messaging.

CentOS 7 reached end of life in June 2024 and current RabbitMQ releases do not support it. Two things also changed in RabbitMQ itself: classic mirrored queues, the "master and mirrors" model configured with ha-mode policies in older tutorials, were removed in RabbitMQ 4.0, and since 4.1 a node no longer has to be stopped and reset before it joins a cluster.

Requirements

  • Three servers running AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9 on x86_64, ideally on a private network. Use an odd number of nodes: the RabbitMQ documentation strongly advises against two-node clusters because a majority cannot be established when the two lose contact.
  • A user with sudo rights on each server.
  • Synchronised clocks (chronyd is enabled by default on these distributions).

The examples use these names and addresses. Replace them with your own:

HostnameAddress
rabbit1198.51.100.11
rabbit2198.51.100.12
rabbit3198.51.100.13

Step 1: Set hostnames and name resolution (all nodes)

A RabbitMQ node is named rabbit@ followed by the short hostname, and every node must be able to resolve its own hostname and those of all its peers. Set the hostname on each server, using the right name on each:

sudo hostnamectl set-hostname rabbit1

Then add all three nodes to /etc/hosts on every server (or create equivalent DNS records):

sudo tee -a /etc/hosts <<'EOF'
198.51.100.11 rabbit1
198.51.100.12 rabbit2
198.51.100.13 rabbit3
EOF

Set the hostname before RabbitMQ starts for the first time. The node name is part of the node's data directory, so renaming a host later is not a simple change.

Step 2: Install Erlang and RabbitMQ (all nodes)

RabbitMQ needs a supported Erlang release (Erlang 27 or newer for RabbitMQ 4.4), which is newer than what distribution repositories usually carry. The RabbitMQ team publishes a zero-dependency Erlang package and the server itself in one set of repositories. Import the signing keys:

sudo rpm --import 'https://github.com/rabbitmq/signing-keys/releases/download/3.0/rabbitmq-release-signing-key.asc'
sudo rpm --import 'https://github.com/rabbitmq/signing-keys/releases/download/3.0/cloudsmith.rabbitmq-erlang.E495BB49CC4BBE5B.key'
sudo rpm --import 'https://github.com/rabbitmq/signing-keys/releases/download/3.0/cloudsmith.rabbitmq-server.9F4587F226208342.key'

Create the repository file:

sudo tee /etc/yum.repos.d/rabbitmq.repo <<'EOF'
[modern-erlang]
name=modern-erlang-el9
baseurl=https://yum1.rabbitmq.com/erlang/el/9/$basearch
        https://yum2.rabbitmq.com/erlang/el/9/$basearch
repo_gpgcheck=1
enabled=1
gpgkey=https://github.com/rabbitmq/signing-keys/releases/download/3.0/cloudsmith.rabbitmq-erlang.E495BB49CC4BBE5B.key
gpgcheck=1
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
pkg_gpgcheck=1
autorefresh=1
type=rpm-md

[modern-erlang-noarch]
name=modern-erlang-el9-noarch
baseurl=https://yum1.rabbitmq.com/erlang/el/9/noarch
        https://yum2.rabbitmq.com/erlang/el/9/noarch
repo_gpgcheck=1
enabled=1
gpgkey=https://github.com/rabbitmq/signing-keys/releases/download/3.0/cloudsmith.rabbitmq-erlang.E495BB49CC4BBE5B.key
       https://github.com/rabbitmq/signing-keys/releases/download/3.0/rabbitmq-release-signing-key.asc
gpgcheck=1
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
pkg_gpgcheck=1
autorefresh=1
type=rpm-md

[rabbitmq-el9]
name=rabbitmq-el9
baseurl=https://yum2.rabbitmq.com/rabbitmq/el/9/$basearch
        https://yum1.rabbitmq.com/rabbitmq/el/9/$basearch
repo_gpgcheck=1
enabled=1
gpgkey=https://github.com/rabbitmq/signing-keys/releases/download/3.0/cloudsmith.rabbitmq-server.9F4587F226208342.key
       https://github.com/rabbitmq/signing-keys/releases/download/3.0/rabbitmq-release-signing-key.asc
gpgcheck=1
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
pkg_gpgcheck=1
autorefresh=1
type=rpm-md

[rabbitmq-el9-noarch]
name=rabbitmq-el9-noarch
baseurl=https://yum2.rabbitmq.com/rabbitmq/el/9/noarch
        https://yum1.rabbitmq.com/rabbitmq/el/9/noarch
repo_gpgcheck=1
enabled=1
gpgkey=https://github.com/rabbitmq/signing-keys/releases/download/3.0/cloudsmith.rabbitmq-server.9F4587F226208342.key
       https://github.com/rabbitmq/signing-keys/releases/download/3.0/rabbitmq-release-signing-key.asc
gpgcheck=1
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
pkg_gpgcheck=1
autorefresh=1
type=rpm-md
EOF

Install the packages:

sudo dnf update -y
sudo dnf install -y logrotate
sudo dnf install -y erlang rabbitmq-server

Do not start the service on rabbit2 and rabbit3 yet. All nodes of a cluster must run the same RabbitMQ series, and the Erlang packages on these mirrors are built for x86_64 only.

Step 3: Open the firewall between the nodes (all nodes)

Cluster nodes talk to each other on these ports, none of which should be reachable from the internet:

  • 4369 — epmd, the Erlang peer discovery daemon
  • 25672 — inter-node and CLI communication
  • 35672-35682 — CLI tools connecting from another host
  • 6000-6500 — stream replication, needed only if you use streams

Create a firewalld zone that applies only to traffic from the cluster members and open the ports there:

sudo firewall-cmd --permanent --new-zone=rabbitmq
sudo firewall-cmd --permanent --zone=rabbitmq --add-source=198.51.100.11
sudo firewall-cmd --permanent --zone=rabbitmq --add-source=198.51.100.12
sudo firewall-cmd --permanent --zone=rabbitmq --add-source=198.51.100.13
sudo firewall-cmd --permanent --zone=rabbitmq --add-port=4369/tcp --add-port=25672/tcp --add-port=35672-35682/tcp --add-port=6000-6500/tcp
sudo firewall-cmd --reload

Clients connect on port 5672 (AMQP) and the management interface listens on 15672. Open those only to the networks that need them, such as your application servers, in a second zone so that those hosts do not get access to the inter-node ports:

sudo firewall-cmd --permanent --new-zone=rabbitmq-clients
sudo firewall-cmd --permanent --zone=rabbitmq-clients --add-source=203.0.113.0/24
sudo firewall-cmd --permanent --zone=rabbitmq-clients --add-port=5672/tcp --add-port=15672/tcp
sudo firewall-cmd --reload

Step 4: Start the first node and share the Erlang cookie

Nodes authenticate to each other with a shared secret, the Erlang cookie. It is created on first start and must be identical on every node. On rabbit1, start RabbitMQ and display the cookie:

sudo systemctl enable --now rabbitmq-server
sudo cat /var/lib/rabbitmq/.erlang.cookie

The file has no trailing newline, so the value runs into your shell prompt; copy only the cookie string. On rabbit2 and rabbit3, write that value to the same file, replacing COOKIE_VALUE, and restrict it to the rabbitmq user:

sudo systemctl stop rabbitmq-server
echo -n 'COOKIE_VALUE' | sudo tee /var/lib/rabbitmq/.erlang.cookie > /dev/null
sudo chown rabbitmq:rabbitmq /var/lib/rabbitmq/.erlang.cookie
sudo chmod 600 /var/lib/rabbitmq/.erlang.cookie

Treat the cookie like a password: anyone who has it and can reach the inter-node ports can control the cluster. Now start RabbitMQ on rabbit2 and rabbit3:

sudo systemctl enable --now rabbitmq-server

At this point you have three independent single-node brokers.

Step 5: Join the nodes into a cluster

On rabbit2, and then on rabbit3, run:

sudo rabbitmqctl join_cluster rabbit@rabbit1

Joining wipes any data the joining node already holds, which is harmless on a fresh installation. On RabbitMQ 4.0 and older the node has to be stopped with rabbitmqctl stop_app and reset with rabbitmqctl reset first, and started again with rabbitmqctl start_app afterwards; from 4.1 the single command is enough.

Check the result from any node:

sudo rabbitmqctl cluster_status

All three nodes should be listed under both the disk nodes and the running nodes. If joining fails with an authentication or "nodedown" error, the usual causes are a cookie that differs between nodes, a hostname that does not resolve, or a blocked port 4369 or 25672.

Step 6: Enable the management UI and create an administrator

Enable the management plugin on every node on which you want the web UI and HTTP API. No restart is needed:

sudo rabbitmq-plugins enable rabbitmq_management

The built-in guest account can only log in from localhost. Create your own administrator on any one node; users and permissions are replicated to the whole cluster. Use a long random password instead of the placeholder:

sudo rabbitmqctl add_user admin 'REPLACE_WITH_A_STRONG_PASSWORD'
sudo rabbitmqctl set_user_tags admin administrator
sudo rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
sudo rabbitmqctl delete_user guest

The UI is then available at http://198.51.100.11:15672/ from an address your firewall zone allows. Its Overview page lists all cluster nodes.

Step 7: Replicate queues with quorum queues

Clustering replicates metadata (users, virtual hosts, exchanges, bindings, policies) automatically. Queue contents are replicated only if the queue is a replicated type. With mirrored classic queues gone, that means quorum queues: each queue has a leader and followers on different nodes, three replicas by default, and stays available as long as a majority of its replicas is online. A three-node cluster therefore tolerates the loss of one node.

The queue type is fixed when a queue is declared and cannot be set or changed later with a policy. Applications choose it by declaring a durable queue with the argument x-queue-type set to quorum. To make quorum the default for queues declared without a type, set it per virtual host:

sudo rabbitmqctl update_vhost_metadata / --default-queue-type quorum

Or set it for the whole node in /etc/rabbitmq/rabbitmq.conf on every node. The file does not exist by default, and a restart is required after editing it:

echo 'default_queue_type = quorum' | sudo tee -a /etc/rabbitmq/rabbitmq.conf
sudo systemctl restart rabbitmq-server

Restart one node at a time and wait for it to rejoin before moving to the next. The default applies only to queues declared afterwards; existing classic queues keep their type and have to be recreated, or their messages moved, to become quorum queues.

Verify failover

Declare a quorum queue from your application or in the management UI (choose "Quorum" in the queue type drop-down), then stop one node:

sudo systemctl stop rabbitmq-server

On another node, sudo rabbitmqctl cluster_status shows two running nodes, and the queue remains usable; if the stopped node held the leader, a new leader is elected. Start the node again and it rejoins and catches up on its own:

sudo systemctl start rabbitmq-server

Clients are not moved for you. Give your applications the addresses of all three nodes (most client libraries accept a list and reconnect automatically), or put a TCP load balancer in front of port 5672.

Maintenance

Before you restart or patch a node, check that taking it down will not cost any queue its majority. The command exits with an error if it would:

sudo rabbitmq-diagnostics check_if_node_is_quorum_critical

Restart nodes one at a time. A restarted node needs a majority of the cluster online to finish booting, so never stop all nodes and expect a single one to come back alone.

To add a fourth or fifth node later, repeat steps 1 to 5 on it, then place replicas of existing quorum queues on it:

sudo rabbitmq-queues grow rabbit@rabbit4 all

To remove a node permanently, stop RabbitMQ on it and then, on one of the remaining nodes, run:

sudo rabbitmqctl forget_cluster_node rabbit@rabbit3

For production loads, raise the open file limit for the service; the default is too low for many connections. Create /etc/systemd/system/rabbitmq-server.service.d/limits.conf with the following content, then run sudo systemctl daemon-reload and restart the node:

[Service]
LimitNOFILE=64000

Frequently asked questions

Can I run a cluster with two nodes?

It will form, but it gains you little. Quorum queues and the cluster's metadata store both need a majority, and with two nodes the majority is two, so losing either node stops writes. Three nodes is the practical minimum; four behave like three, and six like five, in terms of how many failures they survive.

What happened to the ha-mode policies from older tutorials?

They configured classic queue mirroring, which was removed in RabbitMQ 4.0. Policies with ha-mode or ha-sync-mode no longer replicate anything. Declare quorum queues (or streams, for log-style workloads) instead, and review the RabbitMQ migration guidance before upgrading a 3.x cluster that still relies on mirroring.

Can the cluster form by itself instead of running join_cluster?

Yes. RabbitMQ supports peer discovery, and its documentation recommends it over manual joins for production. The simplest backend lists the members in rabbitmq.conf on every node with cluster_formation.peer_discovery_backend = classic_config and one cluster_formation.classic_config.nodes.N = rabbit@hostname line per node. Discovery only runs on a node that has no existing data, so configure it before the first start.

Should the nodes be in different data centres?

Keep cluster nodes on a low-latency, reliable network such as one region or one site. Clustering is sensitive to network interruptions between nodes. To connect brokers across distant locations, RabbitMQ provides the Federation and Shovel plugins instead.

Share story:

Continue reading other guides on How VPS.