﻿# RabbitMQ cluster

> [HTML Version](configure-rabbitmq.html)

This article describes how to install RabbitMQ 3.12.0 and Erlang 25.3.2.2-1 for Ubuntu Linux 20.04 and 22.04. 

Installing RabbitMQ version 4.0 is not supported in the current BRIX version. Check the supported versions of RabbitMQ in the [System requirements for BRIX On-Premises](elma365-enterprise-on-premises.md#rabbit) article.

You can also refer to the guide in the [official RabbitMQ documentation](https://www.rabbitmq.com/install-debian.html) 

Installation consists of six steps:

1. [Prepare nodes (servers)](#preparation-node).

2. [Install RabbitMQ](#install-rabbitmq).

3. [Prepare RabbitMQ cluster](#preparation-cluster-rabbitmq).

4. [Configure RabbitMQ](#setting-rabbitmq).

5. [Configure HAproxy rabbitmq block)](#config-haproxy).

6. [Connect to RabbitMQ](#connect-to-rabbitmq).

## Step 1: Prepare nodes (servers)

````
начало внимание

````
The minimum number of servers to organize a cluster is three.

````
конец внимание

1. ````
Create three nodes (servers) with sequentially numbered host names:

- **rabbitmq-server1.your\_domain**;

- **rabbitmq-server2.your\_domain**;

- **rabbitmq-server3.your\_domain**.

2. Create the necessary host name mappings in DNS. If this is not possible, add the required entries to** /etc/hosts**.

## Step 2: Install RabbitMQ

1. Install the necessary packages:

````
sudo apt-get install curl gnupg apt-transport-https -y

2. ````
Import all necessary keys:

````
curl -1sLf "https://keys.openpgp.org/vks/v1/by-fingerprint/0A9AF2115F4687BD29803A206B73A36E6026DFCA" | sudo gpg --dearmor | sudo tee /usr/share/keyrings/com.rabbitmq.team.gpg > /dev/null  
curl -1sLf https://ppa1.novemberain.com/gpg.E495BB49CC4BBE5B.key | sudo gpg --dearmor | sudo tee /usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg > /dev/null  
curl -1sLf https://ppa1.novemberain.com/gpg.9F4587F226208342.key | sudo gpg --dearmor | sudo tee /usr/share/keyrings/rabbitmq.9F4587F226208342.gpg > /dev/null

3. ````
Add **RabbitMQ** repositories:

````
sudo tee /etc/apt/sources.list.d/rabbitmq.list <<EOF  
deb \[signed-by=/usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg\] https://ppa1.novemberain.com/rabbitmq/rabbitmq-erlang/deb/ubuntu \$(lsb\_release -cs) main  
deb-src \[signed-by=/usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg\] https://ppa1.novemberain.com/rabbitmq/rabbitmq-erlang/deb/ubuntu \$(lsb\_release -cs) main  
  
deb \[signed-by=/usr/share/keyrings/rabbitmq.9F4587F226208342.gpg\] https://ppa1.novemberain.com/rabbitmq/rabbitmq-server/deb/ubuntu \$(lsb\_release -cs) main  
deb-src \[signed-by=/usr/share/keyrings/rabbitmq.9F4587F226208342.gpg\] https://ppa1.novemberain.com/rabbitmq/rabbitmq-server/deb/ubuntu \$(lsb\_release -cs) main  
EOF

4. ````
Update the package cache:

````
sudo apt-get update -y

5. ````
Install **Erlang** packages:

````
sudo apt-get install -y erlang-base=1:25.3.2.2-1 erlang-asn1=1:25.3.2.2-1 erlang-crypto=1:25.3.2.2-1 erlang-eldap=1:25.3.2.2-1 erlang-ftp=1:25.3.2.2-1 erlang-inets=1:25.3.2.2-1 erlang-mnesia=1:25.3.2.2-1 erlang-os-mon=1:25.3.2.2-1 erlang-parsetools=1:25.3.2.2-1 erlang-public-key=1:25.3.2.2-1 erlang-runtime-tools=1:25.3.2.2-1 erlang-snmp=1:25.3.2.2-1 erlang-ssl=1:25.3.2.2-1 erlang-syntax-tools=1:25.3.2.2-1 erlang-tftp=1:25.3.2.2-1 erlang-tools=1:25.3.2.2-1 erlang-xmerl=1:25.3.2.2-1

6. ````
Install **rabbitmq-server** and its dependencies:

````
sudo apt-get install rabbitmq-server=3.12.0-1 -y --fix-missing

7. ````
Start **rabbitmq-server**:

````
sudo systemctl enable --now rabbitmq-server

## ````
Step 3: Prepare RabbitMQ cluster

1. On each **rabbitmq** node, create the file \[OBJECT\] and add the environment variables \[OBJECT\] and \[OBJECT\].

Example content for **rabbitmq-server1.your\_domain**:

````
RABBITMQ\_NODENAME=rabbit@rabbitmq-server1.your\_domain  
RABBITMQ\_USE\_LONGNAME=true

````
Example content for** rabbitmq-server2.your\_domain**:

````
RABBITMQ\_NODENAME=rabbit@rabbitmq-server2.your\_domain  
RABBITMQ\_USE\_LONGNAME=true

````
Example content for **rabbitmq-server3.your\_domain**:

````
RABBITMQ\_NODENAME=rabbit@rabbitmq-server3.your\_domain  
RABBITMQ\_USE\_LONGNAME=true

2. ````
Copy Cookie **/var/lib/rabbitmq/.erlang.cookie** from the first node** rabbitmq-server1.your\_domain** to all the other nodes in the cluster.

````
Начало внимание

````
For the RabbitMQ cluster to work, all nodes participating in the cluster must have the same content in the **/var/lib/rabbitmq/.erlang.cookie **file.

````
Конец внимание

3. ````
Restart the RabbitMQ service on each node:

````
sudo systemctl restart rabbitmq-server

4. ````
Stop the application on nodes **rabbitmq-server2.your\_domain** and **rabbitmq-server3.your\_domain** for subsequent joining to the cluster:

````
sudo rabbitmqctl stop\_app

5. ````
Reset **rabbitmq** on nodes **rabbitmq-server2.your\_domain** and **rabbitmq-server3.your\_domain**:

````
sudo rabbitmqctl reset

6. ````
Join nodes **rabbitmq-server2.your\_domain** and **rabbitmq-server3.your\_domain** to the cluster:

````
sudo rabbitmqctl join\_cluster rabbit@rabbitmq-server1.your\_domain

7. ````
Start the application on nodes **rabbitmq-server2.your\_domain** and **rabbitmq-server3.your\_domain**:

````
sudo rabbitmqctl start\_app

8. ````
Check the cluster status:

````
sudo rabbitmqctl cluster\_status

## ````
Step 4: Configure RabbitMQ

````
начало примечание

````
**Note**

For the password, the following characters are allowed:

- Uppercase Latin letters: A to Z

- Lowercase Latin letters: a to z

- Digits: 0 to 9

- Symbols: -\_

Reserved (invalid) symbols:

\! \* ' ( ) ; : @ \& = + \$ , / ? % # \[ \]

````
конец примечание

1. ````
On each node, enable the necessary plugins: 

````
sudo rabbitmq-plugins enable \\  
rabbitmq\_management

2. ````
Create **vhost** nd give the user access to it, executing commands on the first node **rabbitmq-server1.your\_domain**:

````
sudo rabbitmqctl add\_vhost brix365vhost  
sudo rabbitmqctl add\_user brix365user SecretPassword  
sudo rabbitmqctl set\_permissions -p brix365vhost brix365user ".\*" ".\*" ".\*"  
sudo rabbitmqctl set\_user\_tags brix365user administrator

3. ````
Create a policy that allows mirroring queues for all nodes in the cluster, executing the command on the first node  **rabbitmq-server1.your\_domain**:

````
sudo rabbitmqctl set\_policy -p 'brix365vhost' MirrorAllQueues ".\*" '\{"ha-mode":"all"\}'

````
If the High Availability Mode policy is not supported, use the Quorum policy:

````
rabbitmqctl set\_policy –p 'brix365vhost' QuorumDefault “^.\*” ‘\{“queue-type”:“quorum”\}’ --priority 0 --apply-to queues

````
To  view the configured policies, use the command:

````
sudo rabbitmqctl list\_policies --vhost brix365vhost

````
Enabling TLS/SSL in RabbitMQ

To enable TLS/SSL support in RabbitMQ, you need to edit the configuration file **/etc/rabbitmq/rabbitmq.conf**:

1. Add SSL configuration to the** rabbit** (port 5671) and **management** (port 15671) blocks.

2. In the **ssl\_options.certfile and management.ssl.certfile** parameters, specify the path to the server certificate file.

3. In the **ssl\_options.keyfile **и** management.ssl.keyfile** parameters, specify the path to the private key file.

4. In the **ssl\_options.cacertfile **и** management.ssl.cacertfile** parameters, specify the path to the root CA file.

````
listeners.tcp = none  
listeners.ssl.default = 5671  
ssl\_options.cacertfile = /path/to/ca\_certificate.pem  
ssl\_options.certfile = /path/to/server\_certificate.pem  
ssl\_options.keyfile = /path/to/server\_key.pem  
ssl\_options.verify = verify\_peer  
ssl\_options.fail\_if\_no\_peer\_cert = true  
  
management.ssl.port = 15671  
management.ssl.cacertfile = /path/to/ca\_certificate.pem  
management.ssl.certfile = /path/to/server\_certificate.pem  
management.ssl.keyfile = /path/to/server\_key.pem

5. ````
Restart the RabbitMQ service:

````
sudo systemctl restart rabbitmq-server

````
For more details on configuring TLS/SSL in RabbitMQ, refer to the[ official RabbitMQ documentation](https://www.rabbitmq.com/ssl.html).  
  
For enabling TLS/SSL between RabbitMQ nodes "Securing Cluster (Inter-node)," you need to:

1. Combine the public and private keys of the node into one file, for example, in** **\[OBJECT\]:

````
cat server\_certificate.pem server\_key.pem > combined\_keys.pem

2. ````
Extract the path to the Erlang TLS library and export it to the environment variable ````
\[OBJECT\]````
 в файл ````
\[OBJECT\]````
:

````
erl -noinput -eval 'io:format("ERL\_SSL\_PATH=~s~n", \[filename:dirname(code:which(inet\_tls\_dist))\])' -s init stop > /tmp/ssl-path.txt  
cat /tmp/ssl-path.txt /etc/rabbitmq/rabbitmq-env.conf > /tmp/new-rabbitmq-env.conf  
mv -f /tmp/new-rabbitmq-env.conf /etc/rabbitmq/rabbitmq-env.conf

3. ````
In the ````
\[OBJECT\]````
 file, add parameters for the environment variables ````
\[OBJECT\]````
 and ````
\[OBJECT\]````
:

````
SERVER\_ADDITIONAL\_ERL\_ARGS="-pa \$ERL\_SSL\_PATH \\  
  -proto\_dist inet\_tls \\  
  -ssl\_dist\_opt server\_certfile /path/to/combined\_keys.pem \\  
  -ssl\_dist\_opt server\_secure\_renegotiate true client\_secure\_renegotiate true"  
RABBITMQ\_CTL\_ERL\_ARGS="-pa \$ERL\_SSL\_PATH \\  
  -proto\_dist inet\_tls \\  
  -ssl\_dist\_opt server\_certfile /path/to/combined\_keys.pem \\  
  -ssl\_dist\_opt server\_secure\_renegotiate true client\_secure\_renegotiate true"

4. ````
 Restart the RabbitMQ service on each node:

````
sudo systemctl restart rabbitmq-server

````
For more details on configuring TLS/SSL between RabbitMQ nodes, refer to the [official RabbitMQ documentation](https://www.rabbitmq.com/clustering-ssl.html).

## Step 5: Configure HAProxy (rabbitmq block)

Load balancing between RabbitMQ cluster nodes will be performed using HAProxy. Configure it according to [Configure HAProxy for RabbitMQ](haproxy-rabbitmq.md).

## Step 6: Connect to RabbitMQ

Connection string to connect to the RabbitMQ cluster (via HAProxy):

````
amqp://brix365user:SecretPassword@haproxy-server.your\_domain:5672/brix365vhost

````
Connection string to connect to the RabbitMQ cluster with TLS/SSL (via HAProxy with TLS/SSL):

````
amqps://brix365user:SecretPassword@haproxy-server.your\_domain:5671/brix365vhost
````