Tuesday, January 10, 2017

Introducing Vagrant

Have you ever faced or heard about below problems:
  • Environment setup on multiple systems whether it is development, staging or QA which includes multiple server setup, software and database installation as well as their configuration. We all agree on one point that traditional way of Environment setup is very time consuming and effort taking.
  • The process/scripts for setting up the server / database / software that worked in one system are now failing in another. I am referring to “it does not work on my machine” excuse that developers used to raise for any kind of issues. :-)
  • New member joins the team - you have to again setup the same development environment; you have to recall the complete setup process. Even if you are happy and ready to set up the development environment in traditional way, if it is not properly documented and you miss a minor thing then boom...red flag!  
  • Unable to replicate the issues in your local development environment that's happening in production or in other team members workstation
If yes, then this article/tutorial is a must for you. Vagrant addresses these problems and helps us to create and manage portable, reproducible development environment. If you haven’t faced or heard about above problems or Vagrant, then this is the right time to get acquainted to it. Vagrant is very popular open source project.

In this article, we would discuss:
What is Vagrant?
Why do we need Vagrant?
What are the main features offered by Vagrant?


What is Vagrant?

Vagrant is an open-source software for creating and managing virtual development  environments.
  From above definition, it’s clear that Vagrant needs/uses some tool/utility :
  • For creating the virtual environment (Providers) 
  • For managing/configuring the development environment (Provisioners)
We would discuss about the Providers & Provisioners some time later in this tutorial. 

Let's discuss it in some details:

We all agree that every developer has faced problems (more or less) when it comes to setting up a development environment. The work environment for all developers in a team should be identical and same as of production environment so that any issues raised in production or in any developer's machine could be replicated and resolved. But generally we all face lot of issues in making the work environment identical for all the developers. Vagrant provides solution for this problem. Vagrant changes the way how developers setup their work environments and maintain them. Vagrant helps us create a configurable, reproducible and portable development environment easily by using the vagrantfile. In this file, the developer could specify everything like how the environment should be set up & configured, which software should be installed, which provisioning tools should be used and which guest OS should be used  etc. This Vagrantfile can then be distributed among other developers to set up the same development environment on their own machine.

Why do we need Vagrant?

  • It provides easy, efficient and effective way to create and configure reproducible, portable development environments 
  • Provides multiple options for provisioning the development environment (i.e. virtual machine), from simple shell script to more complex, industry standard configuration management systems like Puppet, Chef and Ansible
  • Easily replicate exactly the same development, staging and QA environment as of production for all the developers and testers that means consistent environment for all
  • Greatly reduce the effort and time required for environment setup
  • Increase the team’s productivity and flexibility as they need to concentrate on their work (actual development) rather than wasting hours on setting up the complex environment

 

What are the main features offered by Vagrant?


Following are the main features offered by Vagrant:

Vagrant sharing: Vagrant sharing allow users to share their vagrant environment with anyone in the world using HTTP, SSH or General sharing. These features can be used in any combination with each other (not mutually exclusive). For example:
  • With HTTP Sharing, one can create a publicly accessible URL endpoint to access an HTTP server running in the Vagrant environment which could be useful for testing webhooks or showing our work to clients, teammates, managers etc.
  • With SSH sharing, one can get instant SSH access to the Vagrant environment which could be useful for pair programming and for debugging. 
  • With General Sharing, one can access any exposed port of the Vagrant environment.

Synced folders:
Vagrant provides the capability to sync a folder on the host machine to the guest machine. But how does it help the user?
  • One can version control all the projects in the synced folder. In this way, one can work on their project’s files on the host machine but use the resources in the guest machine to compile or run their project. For Example: we can clone our project  in the synced folder, create java project in eclipse by browsing these projects and setup the debug configuration by providing the IP address of guest machine. Work on our projects from host machine in eclipse and built it from guest machine. If we find any errors in the project files,  we can debug for errors in host machine. In this way, we are working in our host machine but it will use resources from guest machine. 
  • Projects and files are preserved in our host machine even after destroying / crashing the guest machine.

Boxes:
Boxes are the package format for Vagrant environments. One can not only package their vagrant environment in boxes but also version control these boxes.This has many advantages like:
  • Boxes can be shared among the developers to have an identical work environment.
  • Team can update the underlying box easily with the latest version of box.
  • One can efficiently push latest version of boxes with bug or issue fixes
Vagrant also provides a publicly accessible catalog of vagrant boxes. One can also package a currently running environment by executing vagrant package command which packages such an environment into a reusable box.

Provisioning: Provisioning means customizing the configuration of virtual machines. Using provisioners, one can automatically install software or alter configurations of the virtual machines. Vagrant provides us multiple options for provisioning our virtual machine, from simple shell script to more complex, industry standard configuration management systems like Puppet, Chef and Ansible. But the main question here is , what is the need of these provisioning tool or why one should use these tool if one can install software or configure the virtual machine by simply using the shell script or by using vagrant ssh. Here are few reasons that rings the bell:
  • Automation:  Using provisioning scripts, one can automate most, if not all of repeated task that we do for customizing our work environment.
  • Eradicate duplicate work or rework: In traditional way, everyone needs to setup and configure their work environment manually. Even with the proper and thorough documentation, it is very error prone, cumbersome and time consuming process. With provisioning, one time effort is required for translating the entire environment setup and configuration into the provisioning scripts. Once it’s done then these scripts can be distributed among team members to replicate the identical work environment which have the exact same software and configurations.
  • Version Control: It is good practice to version control the provisioning scripts. If we follow this approach then we can have consistency in our infrastructure setup.

Providers: It’s a software product used by Vagrant for creating and managing the virtual machines. Vagrant ships with out of box support for VirtualBox, Hyper-V, and Docker while VMWare and AWS are supported via plugins. By default Vagrant uses VirtualBox. Providers are also configured in the Vagrantfile.
 
Multi-machine: Using vagrant, one can also define and control multiple guest machines per Vagrantfile. It means, we can set up vagrant environment with multiple machine. That’s why it is also called "multi-machine" environment. It obvious that if we use multi-machine option then the behavior of some commands would slightly change. For example, vagrant ssh will require the name of the machine to control.

Networking:  It allows for the communication between the host and the Vagrant environment. Using available networking options such as forwarded ports, private networks and public networks, we can connect our guest machine to the network. The network configuration is done in the Vagrantfile.

Plugins: Plugins are used as the extension point of Vagrant. One can use it if wants to changes default behavior of Vagrant or wants to add additional functionality to its vagrant environment. 
 
Thank you for reading this article. Hope you would like it. If you have any suggestion or have any question do let me know in the comment box. Happy Learning!!

 

4 comments:

  1. Well explained and helpful for beginners.

    ReplyDelete
  2. It helped me to get clear and crisp idea about the vagrant and it's uses. Waiting for your next article on this.

    ReplyDelete
    Replies
    1. Thank you Subhasish for the feedback. I'm glad it helped. I have recently augmented Vagrant Features section in this article. You can check out those. I am also planning to write an article on Vagrant Architecture after stating vagrant installation and getting our first environment ready using vagrant up in this article so stay tuned!

      Delete