3 node cluster - Zerpet/docker-rabbitmq-cluster GitHub Wiki

This is the most basic template. It just contains 3 nodes. It is a good starting point to get a RabbitMQ cluster up and running. To use this template, download the file 3-disc-nodes.yml and rename it to docker-compose.yml. Once you have the YAML file, simply run docker-compose up -d. The output should be similar to this:

$ docker-compose up -d
Creating composetemplates_rabbitmq-server1_1 ... done
Creating composetemplates_rabbitmq-server2_1 ... done
Creating composetemplates_rabbitmq-server3_1 ... done

It is recommended to read Get started with Docker Compose to understand the rest of this section.

The service definition of a RabbitMQ node looks like this:

  rabbitmq-server1:
    environment:
      RABBITMQ_ERLANG_COOKIE: secret
    hostname: rabbitmq-server1
    image: zerpetfakename/rabbitmq-cluster:3.7
    ports:
    - 5672:5672
    - 15672:15672

This defines a service rabbitmq-server1 and it will forward the container ports 5672 (AMQP) and 15672 (HTTP UI) to the local host ports. It is very important to set the hostname property and the RABBITMQ_ERLANG_COOKIE to a specific value. Otherwise the other nodes won't be able to join the cluster.

This service will be the first and other nodes will join it to form a cluster. The second RabbitMQ node looks like this in the YAML file:

  rabbitmq-server2:
    environment:
      CLUSTER_WITH: 'rabbit@rabbitmq-server1'
      RABBITMQ_ERLANG_COOKIE: secret
    hostname: rabbitmq-server2
    image: zerpetfakename/rabbitmq-cluster:3.7
    ports:
    - 5673:5672
    - 15673:15672

In this piece of code, we added the variable CLUSTER_WITH: 'rabbit@rabbitmq-server1'. This makes the entry point script check if this node belongs to a cluster and to run join_cluster command if it doesn't. It is important to note that RABBITMQ_ERLANG_COOKIE is the same as in rabbitmq-server1.

In this image, the local host ports 5673 and 15673 are forwarded to container ports 5672 and 15672. The increment in the local port numbers is to avoid port collisions.

The third node is identical to the second, just changing the service name and the port forwarding.