ELMA365 On-Premises > Prepare infrastructure > High availability infrastructure / RabbitMQ cluster

RabbitMQ cluster

Several servers are combined into a cluster to load balance the processing and delivery of messages. Read more in the official documentation: https://www.rabbitmq.com/clustering.html.

The instructions on installing and configuring RabbitMQ can be found on the official website: https://www.rabbitmq.com/install-debian.html.

Requirements for the RabbitMQ Erlang version: https://www.rabbitmq.com/which-erlang.html#compatibility-matrix.

Basic information

начало внимание

You need at least three servers to create a RabbitMQ cluster.

конец внимание

In this example, three nodes with the following hostnames and IP addresses are used:

  • rabbitmq-server1, 192.168.1.31
  • rabbitmq-server2, 192.168.1.32
  • rabbitmq-server3, 192.168.1.33

Step 1: Install RabbitMQ

  1. Install the required packages:

sudo apt-get install curl gnupg apt-transport-https -y

  1. Import all the required 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://keyserver.ubuntu.com/pks/lookup?op=get&search=0xf77f1eda57ebb1cc" | sudo gpg --dearmor | sudo tee /usr/share/keyrings/net.launchpad.ppa.rabbitmq.erlang.gpg > /dev/null
curl -1sLf "https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey" | sudo gpg --dearmor | sudo tee /usr/share/keyrings/io.packagecloud.rabbitmq.gpg > /dev/null

  1. Add the RabbitMQ repositories:

sudo tee /etc/apt/sources.list.d/rabbitmq.list <<EOF
deb [signed-by=/usr/share/keyrings/net.launchpad.ppa.rabbitmq.erlang.gpg] http://ppa.launchpad.net/rabbitmq/rabbitmq-erlang/ubuntu bionic main
deb-src [signed-by=/usr/share/keyrings/net.launchpad.ppa.rabbitmq.erlang.gpg] http://ppa.launchpad.net/rabbitmq/rabbitmq-erlang/ubuntu bionic main
 
deb [signed-by=/usr/share/keyrings/io.packagecloud.rabbitmq.gpg] https://packagecloud.io/rabbitmq/rabbitmq-server/ubuntu/ bionic main
deb-src [signed-by=/usr/share/keyrings/io.packagecloud.rabbitmq.gpg] https://packagecloud.io/rabbitmq/rabbitmq-server/ubuntu/ bionic main
EOF

  1. Update the packages’ cache:

sudo apt-get update -y

  1. Set the Erlang packages:

sudo apt-get install -y erlang-base erlang-asn1 erlang-crypto erlang-eldap erlang-ftp erlang-inets erlang-mnesia erlang-os-mon erlang-parsetools erlang-public-key erlang-runtime-tools erlang-snmp erlang-ssl erlang-syntax-tools erlang-tftp erlang-tools erlang-xmerl

  1. Install rabbitmq-server and its dependencies:

sudo apt-get install rabbitmq-server -y --fix-missing

  1. Run rabbitmq-server:

sudo systemctl enable --now rabbitmq-server

Step 2: Add nodes to the RabbitMQ cluster

начало внимание

To make the RabbitMQ cluster work correctly, the content of the /var/lib/rabbitmq/.erlang.cookie file has to be the same on all nodes.

конец внимание

  1. Copy Cookie from the primary node to all the other nodes in the cluster.
  1. Reconfigure RabbitMQ on each node and add these nodes to the cluster. Restart the RabbitMQ service again:

sudo systemctl restart rabbitmq-server

  1. Stop the application on each node you want to add to the cluster:

sudo rabbitmqctl stop_app

  1. Reset rabbitmq:

sudo rabbitmqctl reset

  1. Add a new node to the cluster:

sudo rabbitmqctl join_cluster rabbit@rabbitmq-server1.your_domain

  1. Start the RabbitMQ application:

sudo rabbitmqctl start_app

  1. Check the cluster’s status:

sudo rabbitmqctl cluster_status

Step 3: Configure RabbitMQ

  1. On each node, enable the required plugins:

sudo rabbitmq-plugins enable \
rabbitmq_management

  1. It is required that you add a vhost and give a user access to it. For example:

sudo rabbitmqctl add_vhost elma365vhost
sudo rabbitmqctl add_user elma365user SecretPassword
sudo rabbitmqctl set_permissions -p elma365vhost elma365user ".*" ".*" ".*"
sudo rabbitmqctl set_user_tags elma365user administrator

  1. Set a policy that allows mirrorring queues for all nodes in the cluster:

sudo rabbitmqctl set_policy -p 'elma365vhost' MirrorAllQueues ".*" '{"ha-mode":"all"}'

To see all the policies you set, run the following command:

sudo rabbitmqctl list_policies --vhost elma365vhost

Step 4: HAProxy configuration (rabbitmq)

Example of a configuration intended for load balancing using HAProxy:

listen rabbitmq
    bind haproxy-server.your_domain:5672
    mode tcp
    balance roundrobin
    server rabbitmq-server1 rabbitmq-server1.your_domain:5672  check inter 2s rise 2 fall 3
    server rabbitmq-server2 rabbitmq-server2.your_domain:5672  check inter 2s rise 2 fall 3
    server rabbitmq-server3 rabbitmq-server3.your_domain:5672  check inter 2s rise 2 fall 3
    
listen rabbitmq_management
    bind haproxy.your_domain:15672
    balance source
    server rabbitmq-server1 rabbitmq-server1.your_domain:15672 check check inter 2s
    server rabbitmq-server2 rabbitmq-server2.your_domain:15672 check check inter 2s
    server rabbitmq-server3 rabbitmq-server3.your_domain:15672 check check inter 2s

Step 5. Connect to ELMA365

To connect to the RabbitMQ cluster, run the following:

amqp://elma365user:SecretPassword@haproxy-server.your_domain:5672/elma365vhost

Found a typo? Highlight the text, press ctrl + enter and notify us