Highly Available RabbitMQ messaging cluster

What is RabbitMQ? Robust messaging for applications; it is easy to use, runs on all major OSes, open source and commercially supported.

Why HA? For redundancy, in case one fails would like to preserve the queues.

This time I will take another approach and deploy, configure with Ansible.

On your ansible machine make sure you have the following 2 files and 2 LXC containers already installed (DNS records included).

[root@ansible01 ansible]# ls
files hosts tasks

[root@ansible01 ansible]# less hosts

[rabbitmqs]
rabbitmq01.example.com
rabbitmq02.example.com

[root@ansible01 tasks]# ls
install-rabbitmq-cluster.yml

[root@ansible01 tasks]# less install-rabbitmq-cluster.yml

---
#
# Config RabbitMQ Cluster
#
- hosts: rabbitmqs

sudo: no

tasks:

- name: Config rabbitmq01 basic openstack packages
yum: name=centos-release-openstack-mitaka state=latest

- name: Force a yum upgrade
shell: yum -y upgrade

- name: Install python-openstackclient
yum: name=python-openstackclient state=latest

- name: Install openstack-selinux
yum: name=openstack-selinux state=latest

- name: Install mariadb
yum: name=mariadb state=latest

- name: Install python2-PyMySQL
yum: name=python2-PyMySQL state=latest

- name: Install EPEL repo
yum: name=epel-release state=latest

- name: Install net-tools - reprequisites for RabbitMQ
yum: name=net-tools state=latest

- name: Install Erlang - prerequisite for RabbitMQ
yum: name=python2-PyMySQL state=latest

- name: Install RabbitMQ
yum: name=rabbitmq-server state=latest

- name: Start RabbitMQ
shell: systemctl enable rabbitmq-server

- hosts: rabbitmq01.example.com

sudo: no

tasks:

- name: Copy erlang cookie to secondary node
shell: scp /var/lib/rabbitmq/.erlang.cookie root@rabbitmq02.example.com:/var/lib/rabbitmq/.erlang.cookie
- hosts: rabbitmqs

sudo: no

tasks:

- name: Start RabbitMQ
shell: systemctl start rabbitmq-server
- hosts: rabbitmq02.example.com

sudo: no

tasks:

- name: Join secondary node to cluster - stage 1
shell: rabbitmqctl stop_app

- name: Join secondary node to cluster - stage 2
shell: rabbitmqctl join_cluster --ram rabbit@rabbitmq01

- name: Join secondary node to cluster - stage 3
shell: rabbitmqctl start_app

- hosts: rabbitmq01.example.com

sudo: no

tasks:

- name: Ensure that all queues are mirrored across all running nodes
shell: rabbitmqctl set_policy ha-all '^(?!amq\.).*' '{"ha-mode": "all"}'

 

Then you execute the playbook like this:

ansible-playbook ansible/tasks/install-rabbitmq-cluster.yml -i /root/ansible/hosts --ask-pass

 

And Voila! your RabbitMQ cluster is UP.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s