Hands-on: DevOps with Vagrant

Standard

Welcome to another post of my blog. In this post, we’ll talk about a topic widely spoken in the midst of IT in recent times, called DevOps.

DevOps: what it is

Speaking in general terms, DevOps is a new way to integrate the work among software development teams and operating teams (Production) that support software. Through tools that make the act of starting / end servers, install / uninstall packages, configure servers and deploy software updated quick and automated, we have an IT environment where changes can be deployed into production more quickly and to a lesser granularity – by delivering less features – which becomes also lower the risk of impact on the production environment.

For this hands-on, we will use one of the most popular technologies DevOps have, Vagrant, along with Puppet. The function of Vagrant is to make the start, stop and / or restart of servers, while the Puppet is responsible for making the necessary settings on the servers, such as the rise of application servers – JBoss, etc. – port configuration and threshoulds memory, among other actions. For the hands-on, we used the ubuntu 14:04.

Installing Vagrant

The installation of Vagrant is quite simple and fast. First, we have to install VirtualBox, which is the virtualization technology used by default by Vagrant. For that, on the Ubuntu command prompt, type the following command:

sudo apt-get install virtualbox

After running the command – remember to enter “Y” (yes) when the prompt request – virtualbox will be installed on the OS. Next, you must install Vagrant, using the following command:

sudo apt-get install vagrant

After running the command, the last two configuration steps for the Virtualbox are necessary because it uses OS kernel modules that are not loaded by default. To perform the configuration, run the two commands below in sequence:

sudo apt-get install linux-headers-$(uname -r)

sudo dpkg-reconfigure virtualbox-dkms

After running these commands, the installation is complete.

Starting with Vagrant

Within the Vagrant pattern for the creation of servers, we have the concept of boxes. Through the boxes, you can create a basic server with an OS already installed, instead of performing all zero configuration. For this hands-on, we will use a 32-bit ubuntu 12  box, also called “precise”. To add a box, run the following command:

vagrant box add precise32 http://files.vagrantup.com/precise32.box

After downloading and including the box, the next step is to initialize the Vagrant with the box. To do this, navigate the terminal to a folder of your choice – in my example, I created a “vagrant” folder within my desktop and moved the terminal there – and enter the following command:

vagrant init precise32

After running the command, the reader will notice that was created in the folder chosen a file called “Vagrantfile”. This file is the central file, where the servers are set to be deployed. We will demonstrate this hands-on with a simple example, where it will be created a machine with a tomcat server. To begin, let’s create two folders at the same level of the file “vagrantfile”, called “modules” and “manifests”. In the main file, “vagrantfile”, insert the following code:

VAGRANTFILE_API_VERSION = “2”

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = “hashicorp/precise32”

config.vm.define :web do |web_config|
web_config.vm.network :private_network, :ip => “192.168.33.12”
web_config.vm.provision “puppet” do |puppet|
puppet.module_path = “modules”
puppet.manifest_file = “web.pp”
end
end

end

As shown above, the code configured version of Vagrant API that we will use, the box used, and set up a machine to be started using the box on the ip “192.168.33.12”. In addition, we set the folder modules, which we’ll discuss later, and the name of the provisioning file, of .pp extension. This file has instructions for Puppet to perform the settings on the machine, as in our case, will perform the installation and rise of the Tomcat. Before we see the Puppet file, we will discuss the concept of modules within the Puppet.

Puppet modules

Within Puppet, we have the concept of modules. A Puppet module can be explained as something analogous to a class in the OO paradigm, where classes and methods that encapsulate the configuration commands are defined, so that it is not necessary to repeat the provisioning code to each newly created Puppet project. In the puppet forge there are several modules ready and made available by the community. For this hands-on, we will use the modules of the links below:

https://forge.puppetlabs.com/puppetlabs/stdlib

https://forge.puppetlabs.com/puppetlabs/apt

PS: For this hands-on, we are not using the Vagrant plugin called librarian, in order to maintain the most simple and compact explanation. In a similar manner with other dependency management technologies such as maven, the librarian manages dependencies of Puppet modules. The reader can find a link to the project page on GitHub in the section read more.

The above modules consist of a module of basic utility functions of the Puppet (“stdlib”) and basic functions of OS (“apt”) as “apt-update sudo”. To manually install the modules, download the same as a tar.gz file and unpack them in the “modules” folder. After extracting the files, adjust the names of the modules by removing the prefix and the version, for example by modifying the “puppetlabs-apt-1.6.0” to “apt”. At the end of this post, I will provide a link  with all the necessary files and modules.

Finally, we have the puppet file below, which makes server provisioning:

class { ‘apt’:
always_apt_update => true,
}

Class[‘apt’] -> Package <| |>

Exec {
path => ‘/usr/bin:/usr/sbin/:/bin:/sbin:/usr/local/bin:/usr/local/sbin’,
}

package { [“tomcat7”]:
ensure => installed,
}

service { “tomcat7”:
ensure => running,
enable => true,
hasstatus => true,
hasrestart => true,
require => Package[“tomcat7”],
}

This file should be saved as “web.pp” and saved in the folder “manifests” .In the above code, we make the following sequence of actions: We created an apt class, stating that it must perform an apt-update (“always_apt_update “). On the next line, we indicate that the command should be run whenever a package is installed. In the following lines, we set the package installation tomcat7, perform the startup, and set an OS service for the same.

To start the server with provisioning, run the following command in the command prompt:

vagrant up

The result, when we put the address 192.168.33.12:8080, that is, the ip we set for the machine, and the default port of the tomcat server, is the welcome screen of the server, as can be seen below:

successTo stop the server, simply run the command “vagrant suspend”. With the command “vagrant destroy”, not just the server is stopped, but the instance is also destroyed. The full list of vagrant commands can be found in the links section read more.

Conclusion

In this post we had a brief introduction of the concept of DevOps and the Vagrant technology. With an intuitive language and easy to maintain, it allows us to automate complex infrastructure operations, thereby facilitating the “fearful” process of deploying applications in the production environment. As a concept still growing, it may take some time for the market as a whole to adopt his philosophy, but without a doubt, has many benefits for IT processes within companies.

Source code from the hands-on

Puppet forge

Librarian

Vagrant

One thought on “Hands-on: DevOps with Vagrant

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.