RabbitMQ clustering

What is a RabbitMQ cluster

A RabbitMQ cluster is a logical grouping of one or more nodes, each sharing users, virtual hosts, queues, exchanges, bindings, runtime parameters and other distributed states.

A cluster is made of N nodes and a proxy to enable seamless addressing in the stack.

How to cluster

RabbitMQ

Service creation

The first step in using a RabbitMQ cluster is creating one service per new RabbitMQ node joining the cluster. For example, if you want to update the default configuration containing only one rabbit service to a cluster containing three nodes, start by creating two new RabbitMQ service instances.

${{service-name}}:
    image: ${{image-repository}}osp-rabbitmq${{image-version}}
    ${{ports}}:
        - ${{remote-port:"5671:5671/tcp"}}
        - ${{debug-port:15672}}
    networks:
        back:
        aliases:
            - rabbit
    hostname: ${{service-name}}
    volumes:
        - "rabbitmq_data:/var/lib/rabbitmq/mnesia"
    secrets:
        - ${{auto-generated-secret-access}}

Every node of a cluster must share the same RABBITMQ_ERLANG_COOKIE to be able to communicate.

Note

Do no forget to change (and create in the stack configuration) the volume for each node.

Once the new services are added to the configuration you can push the new configuration to be able to create the cluster.

Cluster creation

Connect the newly added services to the existing RabbitMQ node by running the following commands on every (new) node:

root@modules_rabbitmq_rabbitmq-2:/# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@modules_rabbitmq_rabbitmq-2 ...

root@modules_rabbitmq_rabbitmq-2:/# rabbitmqctl reset
Resetting node rabbit@modules_rabbitmq_rabbitmq-2 ...

root@modules_rabbitmq_rabbitmq-2:/# rabbitmqctl join_cluster rabbit@modules_rabbitmq_rabbitmq-1
Clustering node rabbit@modules_rabbitmq_rabbitmq-2 with rabbit@modules_rabbitmq_rabbitmq-1

root@modules_rabbitmq_rabbitmq-2:/# rabbitmqctl start_app
Starting node rabbit@modules_rabbitmq_rabbitmq-2 ...

Check the cluster is correctly formed by running the following command on one of the nodes.

root@modules_rabbitmq_rabbitmq-3:/# rabbitmqctl cluster_status
Cluster status of node rabbit@modules_rabbitmq_rabbitmq-3 ...
Basics

Cluster name: rabbit@modules_rabbitmq_rabbitmq-1

Disk Nodes

rabbit@modules_rabbitmq_rabbitmq-1
rabbit@modules_rabbitmq_rabbitmq-2
rabbit@modules_rabbitmq_rabbitmq-3

Running Nodes

rabbit@modules_rabbitmq_rabbitmq-1
rabbit@modules_rabbitmq_rabbitmq-2
rabbit@modules_rabbitmq_rabbitmq-3
...

Note

After this step the cluster is created but the behavior will not change compared to before. Indeed, other modules will continue communicating only with one of the cluster node.