How to setup RabbitMq and RabbitMq Cluster

What is RabbitMq ?

Rabbitmq is a Messaging Broker. It works as a intermediator in between the multiple services or applications.
It provides the common plateform to exchange the messages. And also have the messages until these get delivered to the respective reciever.

It includes the architecture of containing Sender/Publisher, Reciever/Consumer and RabbitMq Broker.

rabbitmq-p-c

Features:-

  • RabbitMq is very much reliable in performance as it is persistent and always have the delivery acknowledgements and Mq is highly available.
  • If multiple rabbitmq servers are in same local network then it can make a cluster to perform as a single logical broker.
  • All the queues can be mirrored to all the machines in the cluster to avoid any failure.
  • Rabbitmq supports multiple protocols for messaging.
  • RabbitMq also has its own Mangament Console UI to manage and control it. Also can monitor all the exchanges and queues in it.
  • RabbitMq have multiple plugins and also supports customization in plugins.

Plugins :-

MANAGEMENT
MQTT
STOMP
FEDERATION
SHOVEL
RabbitMq contains below components:-
(Publisher,Consumer,Exchanges,routing key,queues,)

Publisher:- There are always two ends of a communication channel. So here in RabbitMQ the sender is treated as Publisher who actually sends the messages for the clients. Publisher has the queue name mentioned in it and it just put the messages to those queues.

Consumer:- The other end in RabbitMQ is reciever called as Consumer. Consumer will recieve the messages from Broker as per the respective queue.

Exchanges:- Publishers send the messages to the exchanges and then exchanges routes those messages to the queues by using routing key.

Routing Key:- Exchanges always bind to the queues with the help of the routing key.

Queues:- Queues are the place where all the messages get stored for sometime until the consumer recieve them from it. So it works on FIFO (First come first serve) algorithm. The message which will come first to the queue will get consumed first.
Below is the installtion steps of RabbitMQ:-

 

To install RabbitMQ we need to enable epel repo to our system.

vi /etc/yum.repos.d/epel.repo

### Add below lines to it.

[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch/debug
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
#baseurl=http://download.fedoraproject.org/pub/epel/7/SRPMS
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1
--------------------------------------------------------

Now get the repository certificate and install it

wget --no-check-certificate https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7

mv RPM-GPG-KEY-EPEL-7 /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

yum clean all

sudo yum-complete-transaction --skip-broken

yum upgrade

Now Install RabbitMQ Server:-

yum install rabbitmq-server

It will Gets installed in /usr/lib/rabbitmq/bin

Install the devel package:-

yum install librabbitmq-devel

If RabbitMQ is configured as a service then start with following command:

service rabbitmq-server start

Change permissions of some directories

chown –R rabbitmq: /usr/lib/rabbitmq/

chown –R rabbitmq: /var/lib/rabbitmq/

Enable Web Admin Management:-

cd /usr/lib/rabbitmq/bin/

./rabbitmq-plugins enable rabbitmq_management
The main configuration file of Rabbitmq server is :-

/etc/rabbitmq/rabbitmq.config

Now you can access your RabbitMq graphical console on below url:-

http://localhost:15672

Below are the default login credentials to access it.

user: guest
password: guest
You can also create users and modify them.

#To create and change password

rabbitmqctl create_user username password
rabbitmqctl change_password username newpassword

# To set tag for the user as per the roles

rabbitmqctl set_user_tags rmqadmin management
Now give Administrator users access to default (/) virtual host. If this step isn’t done then Spring AMQP will not be able to create exchange and queues on RabbitMQ.

- Go to RabbitMQ admin console
- Go to Admin tab
- Go to Virtual Hosts option
- Now click on / Virtial host to open its properties
- In Permission section, give Administrator user .* access for “Configure regexp”, “Write regexp”, and “Read regexp”.
- Click on Set permission button
How to setup RabbitMQ cluster:-

Before starting rabbitmq cluster make sure you are having atleast 3 nodes and all the nodes are having their hostname.

# change hostname

hostnamectl --transient set-hostname <hostname>
hostnamectl --pretty set-hostname <hostname>
hostnamectl --static set-hostname <hostname>
Now add these hostnames of all the 3 machines to each others /etc/hosts (enable local dns for them)file.

vi /etc/hosts

x.x.x.x node01
y.y.y.y node02
z.z.z.z node03

Copy the cookie files from first cluster to other members and ensure they are same on both servers

scp /var/lib/rabbitmq/.erlang.cookie node02:/var/lib/rabbitmq/.erlang.cookie
scp /var/lib/rabbitmq/.erlang.cookie node03:/var/lib/rabbitmq/.erlang.cookie

Now change the hosts file and add mutual addresses to the file. Erlang looks using host names and not by IP address, so this change is mandatory. Modified /etc/hosts and added mutual addresses: (As described above)

Now let us join the cluster

cd /usr/lib/rabbitmq/bin

on 1st RabbitMQ Node (Master)

sudo ./rabbitmqctl -n rabbit@<hostname> status

On 1st Member Node (Joining Nodes)

sudo ./rabbitmqctl -n rabbit@<hostname> stop_app

sudo ./rabbitmqctl -n rabbit@<hostname> reset

sudo ./rabbitmqctl -n rabbit@<hostname> join_cluster rabbit@<Master Hostname>

sudo ./rabbitmqctl -n rabbit@<hostname> start_app

So on both servers, if you run following you should see the cluster details

sudo ./rabbitmqctl cluster_status

Output will be like this:

[root@test-rabbitmq-clust02 bin]# rabbitmqctl cluster_status
Cluster status of node 'rabbit@test-rabbitmq-clust02' ...
[{nodes,[{disc,['rabbit@node01',
'rabbit@node02']}]},
{running_nodes,['rabbit@node01',
'rabbit@node02']},
{cluster_name,<<"rabbit@node01">>},
{partitions,[]}]
...done.
Follow the same step on the 3rd node. To create a complete cluster.

Now to sync the queues in between all these 3 nodes of cluster, we need to set HA policy on all of them.

rabbitmqctl set_policy ha-all "." '{"ha-mode":"all", "ha-sync-mode":"automatic"}'

Now we can test the cluster by stopping any one node of them.

 

Done. 🙂

4 thoughts on “How to setup RabbitMq and RabbitMq Cluster

Leave a Reply

Your email address will not be published. Required fields are marked *