Deploy and configure a multi-node RabbitMQ cluster on Ubuntu Server

1. Introduction

Reliable, fast messaging is integral to all complex application stacks. RabbitMQ is an implementation of the AMQP protocol that offers both.

This tutorial takes you through the process of installing RabbitMQ in a high-availability RabbitMQ cluster on Ubuntu Server using Juju.

Traditionally, the initial install process has felt brittle and difficult. By the end of this tutorial, you shouldn’t have those worries.

2. Requirements

Juju software

This tutorial assumes that you have installed Juju and registered your security credentials.

If you are new to Juju, consider our Getting Started with Juju tutorial.

Running controller

Duration 5:00

You should have access to a live Juju controller. This can be achieved in 2 ways:

  1. Run juju login jaas to make use of the Canonical-hosted controller. This service is offered at no cost, but credentials are provided to Canonical.
  2. Run juju bootstrap. This option may incur hosting costs, but the controller runs fully in your control.

3. Add a Juju model

The Juju “model” is a workspace for inter-related applications. It houses machines, containers, applications and other components such as firewall rules.

To add a model to the controller, run the juju add-model command:

$ juju add-model messaging

Model name is arbitrary: In the example, we used the name “messaging” for the model. You are welcome to use another name.

4. Deploy RabbitMQ

The juju deploy command deploys charms. A charm is software that manages software. The rabbitmq-server charm manages RabbitMQ.

$ juju deploy rabbitmq-server -n3 --config min-cluster-size=3 rabbitmq

The rabbitmq-server manages RabbitMQ for every OpenStack installation managed by Canonical.

5. Configure users

Juju provides the ability to interact with our cluster without knowing the host IP addresses beforehand. One of the units of the rabbitiq-server charm that’s been deploed is designated as the “leader”.

The juju exec --unit <application>/leader syntax enables commands to be executed on the host itself. The connection information is resolved by Juju.

Remove the guest user account

RabbitMQ comes with a default user account, guest with the password guest. This should be removed from production environments.

$ juju exec --unit 'rabbitmqctl delete_user guest' 

Add other user accounts

User accounts --in this case a demo account–can be added with a similar command, rabbitmqctl add_user.

$ export RABBIT_PASSWORD=$(head -c20 /dev/urandom | hex)
$ juju exec --unit rabbitmq/leader "rabbitmqctl add_user demo $RABBIT_PASSWORD"

To add any user tags, provide them to the rabbitmqctl set_user_tags command:

$ juju exec --unit rabbitmq/leader "rabbitmqctl set_user_tags demo administrator"

6. Add virtual host(s)

Virtual hosts (vhosts) are the primary mechanism that access control is enforced with RabbitMQ. Replace “vh1” with the name of the virtual host you wish to create:

$ juju run --unit rabbitmq/leader "rabbitmqctl add_vhost vh1"

Users are provided with permissions to specific vhosts via the rabbitmqctl set_permissions command:

$ juju run --unit rabbitmq/leader "rabbitmqctl set_permissions -p 'vh1' 'demo' '.*' '.*' '.*'"

7. Declare exchange(s)

An AMQP exchange is a message routing construct, created in RabbitMQ with the rabbitmqadmin command. You can use Juju to access this:

$ juju run --unit rabbitmq/leader "rabbitmqadmin --vhost=vh1 declare exchange name=hub type=fanout -u demo -p $RABBIT_PASSWORD" 

8. (Optional) Enable Management Plugin

The RabbitMQ management plugin enables a monitoring dashboard.

Enable the dashboard

The rabbitmq-plugins command can enable to management plugin for your cluster.

$ juju run --unit rabbitmq/leader "rabbitmq-plugins enable rabbitmq_management" 

Access the dashboard from the Internet

To access the management dashboard from the Internet, ask Juju to open the firewall:

$ juju exec --unit rabbitmq/0 'open-port 15672'
$ juju expose rabbitmq