Skip to main content

Deployment of RabbitMQ using Ansible

What is RabbitMQ

RabbitMQ, which is free and open source, is the world’s most widely deployed message broker. It is used by several big companies like Ford, Instagram, Cisco, etc. Being easy to deploy, it can be used in situ or on the cloud.


 

Here we will learn to install RabbitMQ using Ansible. RabbitMQ is a free and open source message broker system that supports a number of protocols such as the Advanced Message Queuing Protocol (AMQP), Streaming Text Oriented Messaging Protocol (STOMP) and Message Queue Telemetry Transport (MQTT). The software has support for a large number of client libraries for different programming languages. RabbitMQ is written using the Erlang programming language and is released under the Mozilla Public License.

Configuring it up 

A CentOS 6.8 virtual machine (VM) running on KVM is used for the installation. Do make sure that the VM has access to the Internet. The Ansible version used on the host (Parabola GNU/Linux-libre x86_64) is 2.2.1.0. The ansible/folder contains the following files:


ansible/inventory/kvm/inventory
ansible/playbooks/configuration/rabbitmq.yml
ansible/playbooks/admin/uninstall-rabbitmq.yml

The IP address of the guest CentOS 6.8 VM is added to the inventory file as shown below:

rabbitmq ansible_host=192.168.122.161 ansible_connection=ssh ansible_user=root ansible_password=password



Also, add an entry for the rabbitmq host in the /etc/hosts file as indicated below:
Installation

RabbitMQ requires the Erlang environment, and uses the Open Telecom Platform (OTP) framework. There are multiple sources for installing Erlang — the EPEL repository, Erlang Solutions, and the zero-dependency Erlang provided by RabbitMQ. In this article, we will use the EPEL repository for installing Erlang.


---
- name: Install RabbitMQ server
hosts: rabbitmq
gather_facts: true
tags: [server]
tasks:
- name: Import EPEL GPG key
rpm_key:
key: http://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6
state: present
- name: Add YUM repo
yum_repository:
name: epel
description: EPEL YUM repo
baseurl: https://dl.fedoraproject.org/pub/epel/$releasever/$basearch/
gpgcheck: yes
- name: Update the software package repository
yum:
name: ‘*’
update_cache: yes
- name: Install RabbitMQ server
package:
name: “{{ item }}”
state: latest
with_items:
- rabbitmq-server
- name: Start the RabbitMQ server
service:
name: rabbitmq-server
state: started
- wait_for:

port: 5672



After importing the EPEL GPG key and adding the EPEL repository to the system, the yum update command is executed. The RabbitMQ server and its dependencies are then installed. We wait for the RabbitMQ server to start and listen on port 5672. The above playbook can be invoked as follows:

$ ansible-playbook -i inventory/kvm/inventory playbooks/configuration/rabbitmq.yml --tags “server”
Dashboard


The RabbitMQ management user interface (UI) is available through plugins.
- name: Start RabbitMQ Management UI
hosts: rabbitmq
gather_facts: true
tags: [ui]
tasks:
- name: Start management UI
command: /usr/lib/rabbitmq/bin/rabbitmq-plugins enable rabbitmq_management
- name: Restart RabbitMQ server
service:
name: rabbitmq-server
state: restarted
- wait_for:
port: 15672
- name: Allow port 15672
shell: iptables -I INPUT 5 -p tcp --dport 15672 -m state --state NEW,ESTABLISHED -j ACCEPT

After enabling the management plugin, the server needs to be restarted. Since we are running it inside the VM, we need to allow the management user interface (UI) port 15672 through the firewall. The playbook invocation to set up the management UI is given below:
$ ansible-playbook -i inventory/kvm/inventory playbooks/configuration/rabbitmq.yml --tags “ui”

The default user name and password for the dashboard are ‘guest:guest’. From your host system, you can start a browser and open http://192.168.122.161:15672 to view the login page as shown in Figure 1. The default ‘Overview’ page is shown in Figure 2.



Ruby


We will use a Ruby client example to demonstrate that our installation of RabbitMQ is working fine. The Ruby Version Manager (RVM) will be used to install Ruby as shown below:
- name: Ruby client
hosts: rabbitmq
gather_facts: true
tags: [ruby]
tasks:
- name: Import key
command: gpg2 --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
- name: Install RVM
shell: curl -sSL https://get.rvm.io | bash -s stable
- name: Install Ruby
shell: source /etc/profile.d/rvm.sh && rvm install ruby-2.2.6
- name: Set default Ruby
command: rvm alias create default ruby-2.2.6
- name: Install bunny client
shell: gem install bunny --version “>= 2.6.4”


After importing the required GPG keys, RVM and Ruby 2.2.6 are installed on the CentOS 6.8 VM. The bunny Ruby client for RabbitMQ is then installed. The Ansible playbook to set up Ruby is given below:

$ ansible-playbook -i inventory/kvm/inventory playbooks/configuration/rabbitmq.yml --tags “ruby”




We shall create a ‘temperature’ queue to send the values in Celsius. The consumer.rb code to receive the values from the queue is given below:
#!/usr/bin/env ruby
require “bunny”
conn = Bunny.new(:automatically_recover => false)
conn.start
chan = conn.create_channel
queue = chan.queue(“temperature”)
begin
puts “ ... waiting. CTRL+C to exit
queue.subscribe(:block => true) do |info, properties, body|
puts “ Received #{body}”
end
rescue Interrupt => _
conn.close
exit(0)
end
The producer.rb code to send a sample of five values in degree Celsius is as follows:
#!/usr/bin/env ruby
require “bunny”
conn = Bunny.new(:automatically_recover => false)
conn.start
chan = conn.create_channel
queue = chan.queue(“temperature”)
values = [“33.5”, “35.2”, “36.7”, “37.0”, “36.4”]
values.each do |v|
chan.default_exchange.publish(v, :routing_key => queue.name)
end
puts “Sent five temperature values.”
conn.close
As soon as you start the consumer, you will get the following output:
$ ruby consumer.rb
... waiting. CTRL+C to exit
You can then run the producer.rb script that writes the values to the queue:
$ ruby producer.rb
Sent five temperature values.
The received values at the consumer side are printed out as shown below:
$ ruby consumer.rb
... waiting. CTRL+C to exit
Received 33.5
Received 35.2
Received 36.7
Received 37.0
Received 36.4

We can observe the available connections and the created queue in the management user interface as shown in Figure 3 and Figure 4, respectively.
               
Uninstall
It is good to have an uninstall script to remove the RabbitMQ server for administrative purposes. The Ansible playbook for the same is available in the playbooks/admin folder and is shown below:
---
- name: Uninstall RabbitMQ server
hosts: rabbitmq
gather_facts: true
tags: [remove]
tasks:
- name: Stop the RabbitMQ server
service:
name: rabbitmq-server
state: stopped
- name: Uninstall rabbitmq
package:
name: “{{ item }}”
state: absent
with_items:
- rabbitmq-server
The script can be invoked as follows:

$ ansible-playbook -i inventory/kvm/inventory playbooks/admin/uninstall-rabbitmq.yml



Thanks for reading my blog

 For more updates press the follow button 




Comments

Post a Comment

Popular posts from this blog

How to setup eth server for PEATIO EXCHANGE

What do you need? An instance with 4G of ram at least 100 G of hard disk Ubuntu 16.04   Install geth sudo apt-get install software-properties-common sudo add-apt-repository -y ppa:ethereum/ethereum sudo apt-get update sudo apt-get install ethereum Run geth copy the geth service file to /etc/systemd/system/geth.service sudo systemctl start geth sudo systemctl enable geth Install Nginx + fcgi sudo apt install nginx -y fcgiwrap copy default file to /etc/nginx/sites-enabled/default sudo systemctl restart nginx cgi files copy the cgi files to /var/www/html/cgi-bin and update total.cgi with your username sudo chown www-data:www-data -R /var/www/html/cgi-bin sudo chmod +x /var/www/html/cgi-bin/* Install filter service copy total.js to /var/www sudo chown www-data:www-data /var/www/total.js don't forget to edit service.rb with your url sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libr...

Tether(USDT) Installation | Command-line Tutorial

What is USDT Tether’s Omni layer constructed by bitcoin block network. On this overlay network, all parties can issue token, tether company. Token, codenamed USDT, was issued on Omni layer, anchoring in US$1:1. Tether’s idea is very simple. You give me the dollar and give me one dollar. I issue 1 USDT. The dollar is deposited in the designated asset account. Everyone can inquire about it. The total amount of USDT issued can also be inquired on the block. When converting, give me 1 USDT and I return 1 dollar. Omni(USDT) Wallet installation 1.Download Omni Layer Wallet    wget https://bintray.com/artifact/download/omni/OmniBinaries/omnicore-0.3.0-x86_64-linux-gnu.tar.gz ux-gn 2.Decompress & Run    tar - xzvf omnicore - 0.3 . 0 - x86_64 - linux - gnu . tar . gz Now, copy Dependent library to Local sudo CP omnicore-0.3.0/lib/*/lib Open the wallet directory cd omnicore-0.3.0/bin ./omnicored Initial star...

Market Research, Financial Insights, Technology Trends, and Consumer Analytics

This list covers market research, financial insights, technology trends, and consumer analytics . 🔹 General Market Research & Industry Reports   1. Market Research Future - https://www.marketresearchfuture.com/   2. Allied Market Research - https://www.alliedmarketresearch.com/   3. Mordor Intelligence - https://www.mordorintelligence.com/   4. Grand Vie w Research - https://www.grandviewresearch.com/   5. Research and Markets - https://www.researchandmarkets.com/   6. IBISWorld - https://www.ibisworld.com/   7. Statista - https://www.statista.com/   8. Fact.MR - https://www.factmr.com/   9. Transparency Market Research -https://www.transparencymarketresearch.com/   10. Global Market Insights - https://www.gminsights.com/   🔹 Technology & Innovation Research    11. Technavio - https://www.technavio.com/   12. Forrester Research - https://www.forrest...