DevOps Se­ries An­si­ble De­ploy­ment of Rab­bitMQ

Rab­bitMQ, which is free and open source, is the world’s most widely de­ployed mes­sage bro­ker. It is used by sev­eral big com­pa­nies like Ford, In­sta­gram, Cisco, etc. Be­ing easy to de­ploy, it can be used in situ or on the cloud.

OpenSource For You - - Contents - By: Shak­thi Kan­nan The au­thor is a free soft­ware en­thu­si­ast and blogs at shak­thi­maan.com.

In this fourth ar­ti­cle in the DevOps se­ries, we will learn to in­stall Rab­bitMQ us­ing An­si­ble. Rab­bitMQ is a free and open source mes­sage bro­ker sys­tem that sup­ports a num­ber of pro­to­cols such as the Ad­vanced Mes­sage Queu­ing Pro­to­col (AMQP), Stream­ing Text Ori­ented Mes­sag­ing Pro­to­col (STOMP) and Mes­sage Queue Teleme­try Trans­port (MQTT). The soft­ware has sup­port for a large num­ber of client li­braries for dif­fer­ent pro­gram­ming lan­guages. Rab­bitMQ is writ­ten us­ing the Er­lang pro­gram­ming lan­guage and is re­leased un­der the Mozilla Pub­lic Li­cense.

Set­ting it up

A Cen­tOS 6.8 vir­tual ma­chine (VM) run­ning on KVM is used for the in­stal­la­tion. Do make sure that the VM has ac­cess to the In­ter­net. The An­si­ble ver­sion used on the host (Parabola GNU/Linux-li­bre x86_64) is 2.2.1.0. The an­si­ble/folder con­tains the fol­low­ing files:

an­si­ble/in­ven­tory/kvm/in­ven­tory an­si­ble/play­books/con­fig­u­ra­tion/rab­bitmq.yml an­si­ble/play­books/ad­min/unin­stall-rab­bitmq.yml

The IP ad­dress of the guest Cen­tOS 6.8 VM is added to the in­ven­tory file as shown be­low:

rab­bitmq an­si­ble_host=192.168.122.161 an­si­ble_­con­nec­tion=ssh an­si­ble_user=root an­si­ble_­pass­word=pass­word Also, add an en­try for the rab­bitmq host in the /etc/hosts file as in­di­cated be­low:

192.168.122.161 rab­bitmq

In­stal­la­tion

Rab­bitMQ re­quires the Er­lang en­vi­ron­ment, and uses the Open Tele­com Plat­form (OTP) frame­work. There are mul­ti­ple sources for in­stalling Er­lang — the EPEL repos­i­tory, Er­lang So­lu­tions, and the zero-de­pen­dency Er­lang pro­vided by Rab­bitMQ. In this ar­ti­cle, we will use the EPEL repos­i­tory for in­stalling Er­lang.

--name: In­stall Rab­bitMQ server hosts: rab­bitmq gath­er_­facts: true tags: [server]

tasks:

- name: Im­port EPEL GPG key rp­m_key: key: http://dl.fe­do­rapro­ject.org/pub/epel/RPM-GPGKEY-EPEL-6 state: present

- name: Add YUM repo yum_repos­i­tory:

name: epel de­scrip­tion: EPEL YUM repo baseurl: https://dl.fe­do­rapro­ject.org/pub/ epel/$re­lea­sever/$basearch/

gpgcheck: yes

- name: Up­date the soft­ware pack­age repos­i­tory yum: name: ‘*’ up­date_­cache: yes

- name: In­stall Rab­bitMQ server pack­age: name: “{{ item }}” state: lat­est with­_items:

- rab­bitmq-server

- name: Start the Rab­bitMQ server ser­vice: name: rab­bitmq-server state: started

- wait­_­for: port: 5672

Af­ter im­port­ing the EPEL GPG key and adding the

EPEL repos­i­tory to the sys­tem, the yum up­date com­mand is ex­e­cuted. The Rab­bitMQ server and its de­pen­den­cies are then in­stalled. We wait for the Rab­bitMQ server to start and lis­ten on port 5672. The above play­book can be in­voked as fol­lows:

$ an­si­ble-play­book -i in­ven­tory/kvm/in­ven­tory play­books/ con­fig­u­ra­tion/rab­bitmq.yml --tags “server”

Dash­board

The Rab­bitMQ man­age­ment user in­ter­face (UI) is avail­able through plug­ins.

- name: Start Rab­bitMQ Man­age­ment UI hosts: rab­bitmq gath­er_­facts: true tags: [ui]

tasks:

- name: Start man­age­ment UI com­mand: /usr/lib/rab­bitmq/bin/rab­bitmq-plug­ins en­able rab­bit­mq_­man­age­ment

- name: Res­tart Rab­bitMQ server ser­vice: name: rab­bitmq-server state: restarted

- wait­_­for: port: 15672

- name: Al­low port 15672 shell: ipt­a­bles -I IN­PUT 5 -p tcp --dport 15672 -m state --state NEW,ES­TAB­LISHED -j AC­CEPT

Af­ter en­abling the man­age­ment plugin, the server needs to be restarted. Since we are run­ning it in­side the VM, we need to al­low the man­age­ment user in­ter­face (UI) port 15672 through the fire­wall. The play­book in­vo­ca­tion to set up the man­age­ment UI is given be­low:

$ an­si­ble-play­book -i in­ven­tory/kvm/in­ven­tory play­books/ con­fig­u­ra­tion/rab­bitmq.yml --tags “ui”

The de­fault user name and pass­word for the dash­board are ‘guest:guest’. From your host sys­tem, you can start a browser and open http://192.168.122.161:15672 to view the lo­gin page as shown in Fig­ure 1. The de­fault ‘Overview’ page is shown in Fig­ure 2.

Ruby

We will use a Ruby client ex­am­ple to demon­strate that our in­stal­la­tion of Rab­bitMQ is work­ing fine. The Ruby Ver­sion Man­ager (RVM) will be used to in­stall Ruby as shown be­low:

- name: Ruby client hosts: rab­bitmq gath­er_­facts: true tags: [ruby]

tasks:

- name: Im­port key com­mand: gpg2 --key­server hkp://keys.gnupg.net --recvkeys 409B6B1796C275462A1703113804BB82D39DC0E3

- name: In­stall RVM shell: curl -sSL https://get.rvm.io | bash -s sta­ble

- name: In­stall Ruby shell: source /etc/pro­file.d/rvm.sh && rvm in­stall ruby2.2.6

- name: Set de­fault Ruby com­mand: rvm alias cre­ate de­fault ruby-2.2.6

- name: In­stall bunny client shell: gem in­stall bunny --ver­sion “>= 2.6.4”

Af­ter im­port­ing the re­quired GPG keys, RVM and Ruby 2.2.6 are in­stalled on the Cen­tOS 6.8 VM. The bunny Ruby client for Rab­bitMQ is then in­stalled. The An­si­ble play­book to set up Ruby is given be­low:

$ an­si­ble-play­book -i in­ven­tory/kvm/in­ven­tory play­books/ con­fig­u­ra­tion/rab­bitmq.yml --tags “ruby”

We shall cre­ate a ‘tem­per­a­ture’ queue to send the val­ues in Cel­sius. The con­sumer.rb code to re­ceive the val­ues from the queue is given be­low:

#!/usr/bin/env ruby re­quire “bunny”

conn = Bunny.new(:au­to­mat­i­cal­ly_re­cover => false) conn.start

chan = conn.cre­ate_chan­nel queue = chan.queue(“tem­per­a­ture”)

be­gin puts “... wait­ing. CTRL+C to exit” queue.sub­scribe(:block => true) do |info, prop­er­ties, body| puts “Re­ceived #{body}” end res­cue In­ter­rupt => _ conn.close

exit(0) end

The pro­ducer.rb code to send a sam­ple of five val­ues in de­gree Cel­sius is as fol­lows:

#!/usr/bin/env ruby re­quire “bunny”

conn = Bunny.new(:au­to­mat­i­cal­ly_re­cover => false) conn.start

chan = conn.cre­ate_chan­nel queue = chan.queue(“tem­per­a­ture”)

val­ues = [“33.5”, “35.2”, “36.7”, “37.0”, “36.4”]

val­ues.each do |v|

chan.de­fault­_ex­change.pub­lish(v, :rout­ing_key => queue. name) end puts “Sent five tem­per­a­ture val­ues.”

conn.close

As soon as you start the con­sumer, you will get the fol­low­ing out­put:

$ ruby con­sumer.rb

... wait­ing. CTRL+C to exit

You can then run the pro­ducer.rb script that writes the val­ues to the queue:

$ ruby pro­ducer.rb Sent five tem­per­a­ture val­ues.

The re­ceived val­ues at the con­sumer side are printed out as shown be­low:

$ ruby con­sumer.rb

... wait­ing. CTRL+C to exit Re­ceived 33.5

Re­ceived 35.2

Re­ceived 36.7

Re­ceived 37.0 Re­ceived 36.4

We can ob­serve the avail­able con­nec­tions and the cre­ated queue in the man­age­ment user in­ter­face as shown in Fig­ure 3 and Fig­ure 4, re­spec­tively.

Unin­stall

It is good to have an unin­stall script to re­move the Rab­bitMQ server for ad­min­is­tra­tive pur­poses. The An­si­ble play­book for the same is avail­able in the play­books/ad­min folder and is shown be­low:

--name: Unin­stall Rab­bitMQ server hosts: rab­bitmq gath­er_­facts: true tags: [re­move]

tasks:

- name: Stop the Rab­bitMQ server ser­vice: name: rab­bitmq-server state: stopped

- name: Unin­stall rab­bitmq pack­age: name: “{{ item }}” state: ab­sent with­_items:

- rab­bitmq-server

The script can be in­voked as fol­lows:

$ an­si­ble-play­book -i in­ven­tory/kvm/in­ven­tory play­books/ ad­min/unin­stall-rab­bitmq.yml

You are en­cour­aged to read the de­tailed doc­u­men­ta­tion at https://www.rab­bitmq.com/doc­u­men­ta­tion.html to know more about the us­age, con­fig­u­ra­tion, client li­braries and plug­ins avail­able for Rab­bitMQ.

Fig­ure 2: Rab­bitMQ overview

Fig­ure 1: Rab­bitMQ lo­gin

Fig­ure 3: Rab­bitMQ con­nec­tions

Fig­ure 4: Rab­bitMQ queues

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.