RabbitMQ Clustering on Ubuntu 14.04


RabbitMQ is messaging service that allows you to queue and spread your data processing tasks to backend servers. For instance, this allows you to build a server cluster solely responsible for processing photo uploads (resizing, adding filters, saving to storage, etc). Each server in that cluster contacts the RabbitMQ cluster for new, unprocessed messages.

Server Configuration

The following configuration will be used for this tutorial.

Host Role CPU RAM IP Address
lb-01 HAProxy Load Balancer 1 core 1 GB
rabbit-01 RabbitMQ Server 1 core 2 GB
rabbit-02 RabbitMQ Server 1 core 2 GB

Load Balancing with HAProxy

To avoid additional and unnecessary logic your application, traffic balance and availability can be gained by using a load balancer instead. HAProxy is a fantastic load balancing solution for those without access to a hardware balancer or an AWS ELB, for example.

  1. Install HAProxy.
    sudo apt-get install haproxy
  2. Open the HAProxy configuration file in a text editor, like VI.
    sudo vi /etc/haproxy/haproxy.conf
  3. Configure the RabbitMQ cluster in HAProxy. In the example below, we’ll be creating a cluster called rabbitc1 that listens on port 5762. It will have two nodes named rabbit-01 and rabbit-02. Health checks will be done on each node. The check will be set to down any node that fails 3 consecutive checks, and to be brought back online after 2 successful consecutive checks.
    listen rabbitc1
            mode tcp
            option tcplog
            server rabbit-01 check fall 3 rise 2
            server rabbit-02 check fall 3 rise 2
  4. Save your changes and exit the text editor.
  5. Restart HAProxy
    sudo apt-get haproxy restart
  6. Verify the HAProxy RabbitMQ server is running by using netstat and checking for port 5672.
    netstat -nl
  7. The output of the netstat command should show a port at 5672 to be in a listen state.
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State
    tcp        0      0    *               LISTEN
    tcp        0      0  *               LISTEN

Building the RabbitMQ Cluster

Our cluster will consist of two nodes. To ensure high availability in the event of a node failure, our nodes will mirror each other’s message queues.

First Node

  1. Install RabbitMQ
    sudo apt-get install rabbitmq-server
  2. Set cluster policy to mirror all queues to all nodes.
    sudo rabbitmqctl set_policy ha-all "" '{"ha-mode":"all"}'
  3. Create a virtual host for your application. For example, we’ll create one called /prod.
    sudo rabbitmqctl add_vhost /prod
  4. Create your administrator account.
    sudo rabbitmqctl add_user jdoe my-super-complex-password
  5. Grant your administrator account appropriate permissions.
    sudo rabbitmqctl set_permissions -p /prod jdoe ".*" ".*" ".*"
  6. Your application will need an account to communicate with RabbitMQ. Create your application account by using the following command, ensuring you set a complex password.
    sudo rabbitmqctl add_user app1user P@ssw0rd1
  7. Grant your application’s account appropriate permissions to the virtual host.
    sudo rabbitmqctl set_permissions -p /prod jdoe ".*" ".*" ".*"

Second Node

  • Log onto the first node.
  • Make note of the Erlang Cookie value. The value can be seen by viewing the .erlange.cookie file.
    sudo cat /var/lib/rabbitmq/.erlang.cookie
  • Log onto the second node.
  • Install RabbitMQ
    sudo apt-get install rabbitmq-server
  • Stop the RabbitMQ service.
    sudo rabbitmq-server stop
  • Grant Write privileges to the RabbitMQ Erlang Cookie file.
    sudo chmod +w /var/lib/rabbitmq/.erlang.cookie
  • Open the file in a text editor and replace its contents with that of the first node.
    sudo vi /var/lib/rabbitmq/.erlang.cookie
  • Save the changes to the file and exit the text editor.
  • Remove write permissions from the file.
    sudo chmod -w /var/lib/rabbitmq/.erlang.cookie
  • Start the RabbitMQ service
    sudo service rabbitmq-server start
  • Stop the RabbitMQ App
    sudo rabbitmqctl stop_app
  • Join the node to the cluster via the first node.
    sudo rabbitmqctl join_cluster rabbit@rabbit-01
  • Start the RabbitMQ App
    sudo rabbitmqctl start_app
  • Verify the cluster’s status includes both RabbitMQ nodes.
    sudo rabbitmqctl cluster_status

Additional Nodes

All additional nodes can follow the exact same steps listed under Second Node.

Web Management Console

  1. Log onto the first node.
  2. Install the Management Console plugin for RabbitMQ.
    rabbitmq-plugins enable rabbitmq_management
  3. Restart RabbitMQ.
    sudo service rabbitmq-server restart
  4. Grant your administrator account administrative rights to the console. For example, our admin account will be named jdoe.
    sudo rabbitmqctl set_user_tags jdoe administrator
  5. Repeat steps 2 and 3 on every RabbitMQ cluster node.
  6. Log onto the web console by going to http://<server-name-or-ip>:15672.