To establish a RabbitMQ mirror cluster, it is essential to first configure a standard RabbitMQ cluster. In a standard cluster setup, metadata such as exchanges, bindings, and queues are replicated across all nodes in the cluster. However, the actual contents of a queue reside only on its designated node. Clients can connect to any node in the cluster to produce or consume messages from any queue, as each node maintains metadata for all queues and can fetch queue contents remotely when necesssary.
While a standard cluster allows for better distribution of workloads across nodes, it does not provide high availability for queues. If the node hosting a particular queue goes down, that queue becomes unavailable until the node recovers. To ensure availability during node failures, mirroring the queue across all nodes is required. This is achieved by creating mirrored queues.
Environment Requirements
- Operating System: CentOS 7
- Docker Version: 1.13.1
Setting Up a Standard RabbitMQ Cluster
Pull the latest RabbitMQ image with the management plugin:
docker pull rabbitmq:management
Run three RabbitMQ containers with the same Erlang cookie to ensure they can form a cluster:
docker run -d --hostname node1 --name mqnode1 -p 15672:15672 -p 5672:5672 -e RABBITMQ_ERLANG_COOKIE='shared_cookie' rabbitmq:management
docker run -d --hostname node2 --name mqnode2 -p 15673:15672 -p 5673:5672 --link mqnode1:node1 -e RABBITMQ_ERLANG_COOKIE='shared_cookie' rabbitmq:management
docker run -d --hostname node3 --name mqnode3 -p 15674:15672 -p 5674:5672 --link mqnode1:node1 --link mqnode2:node2 -e RABBITMQ_ERLANG_COOKIE='shared_cookie' rabbitmq:management
Ensure the containers are connected via the --link parameter to allow internal communication.
Next, configure the second and third nodes to join the cluster:
# On mqnode1
docker exec -it mqnode1 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
exit
# On mqnode2
docker exec -it mqnode2 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster rabbit@node1
rabbitmqctl start_app
exit
# On mqnode3
docker exec -it mqnode3 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster rabbit@node1
rabbitmqctl start_app
exit
Access the web-based management enterface at http://ip:15672 to confirm that the standard cluster is successfully configured.
Configuring a RabbitMQ Mirror Cluster
Once the standard cluster is ready, the next step is to configure mirrored queues via the RabbitMQ management console. This is done by defining a policy under the Admin tab with the following key parameters:
- Pattern: Queue name pattern (wildcard match).
- ha-mode: Mirroring strategy:
all: Mirror to all nodes.exactly: Mirror to a specific number of nodes.nodes: Mirror to specific named nodes.
- ha-sync-mode: Synchronization mode between mirrors. Use
automaticfor auto-synchronization.
After setting up this policy, both existing and newly created queues will adhere to the mirroring configuration, completing the RabbitMQ mirror cluster setup.