What is Vagrant and How does it work
Vagrant is a software written in Ruby, for creating a full development environment within minutes. Let’s imagine that you are a developer, working on a software project, that involves setting up, installing and configuring multiple services.
Most of the times, you will be working in your own development environment entirely configured inside your laptop.
If that is the case, you need to install all the required dependency softwares on your laptop/or workstation, all your own. Modern web applications has got a lot of moving parts to it, like web servers, application servers, backend API services etc.
Installing all of these manually and configuring them over and over again is a tedious task for a developer. Most of the times, the development work carried out by developers are on either Mac or Windows(well majority of them are still using Windows...). Most of the times it's a combination of both(If you are working in a development team, some of them might be using a Mac, and some of them Windows). And the applications developed will be running on Linux servers in production.
Basically a huge disparity between environments used by different developers and the actual production environment.
Majority of the times, it’s not the installation of application components that’s difficult, it's the configuration part that creates the trouble, as it should resemble the production environment(which most of the developers might be unaware of). This kind of changes in configuration can lead to problems where things are working perfect in development environment, but not in production.
To complicate this more, consider a developer working in multiple projects, which means his local environment needs to be configured with different configurations applicable to those different projects. This can make things much more messier.
What about keeping the application dependency configuration and environment completely in perfect sync with other developers working in the project. Because ultimately every developer will be setting up his own test environment locally in his/her system.
These are the problems, that vagrant is trying to solve. Vagrant will let developers work on the same exact mirror configuration that is running in production environment( without caring about what operating system the developers are using. )
Vagrant achieves this, by leveraging virtualization.
With the help of vagrant, developers can simply checkout a repository containing a Vagrantfile(a file that has all configuration details, for setting up an entire development environment), and run a command called vagrant up, and within minutes a brand new development environment is ready without any manual intervention.
Even operations engineers can leverage vagrant to test their automation scripts and configuration management modules and recipes (for puppet or chef), by bringing a complete production copy sandbox environment within seconds by runnning vagrant up.
So the bottom line is...Vagrant can help you create development environments and share the environment with anybody else with ease. Bringing up a brand new development environment, and destroying it after running tests is now a cakewalk.
Yes. Vagrant achieves all of this by using virtualization. Most of you might already know what virtualization is. Virtualization has reached to a point, that majority of applications and websites that you access online are using virtualized hardware in one or the other way.
Virtualization is nothing but a method to create a vitual hardware, with required resources allocated, to run another operating system on top of it. To make it much more simple, consider virutalization as a method to run an operating system on top of another operating system, by slicing up the hardware.
There are different virualization softwares available in the market, some of them are mentioned below.
- VirtualBox (The virtualization Software developed by Oracle)
- Microsoft Hyder V
- Amazon EC2 (a virutalized public cloud environment)
Virtualization sounds to be the best suitable technology, for developers and operations guys to create a development/or test environment. Its simply because you can get a brand new environment on your system, without messing up with the actual underlying operating system that you are using for everything else.
So why is vagrant required?
Virtualization can help you set up a new environment without impacting or modifying anything on your underlying main operating system(because virtualization will create a brand new test machine, with independent operating system, by utilizing a small required amount of resources from the underlying system hardware, and it will not touch the main operating system.)
Still setting up an environment for development work is not that simple. Because you still have to go through the hassle of installing required softwares, configuring them etc, on top of the new virtual machine.
Apart from this, as mentioned earlier, a modern application has multiple independent components and moving parts. Often these different components are configured as seperate machines(so you need multiple virtual machines, in a network that will enable intercommunication between all of them.)
Agreed. Virtualization softwares mentioned earlier, does provide options to do all of these...But still its not fast enough for a developer to create and terminate environments with all required building blocks and interconnecting machines. Also a developer should only be spending his valuable time on developing and designing things, rather than setting up machines and environments.
Here is where vagrant comes into the picture...Using vagrant developers can create test environments within minutes, by using a simple one line command. Vagrant will use virtualization software of your interest under the hood, to achieve all of this.
Think of vagrant as an automation engine, for your virtualization platform.
Vagrant does the following, with one single command.
- Installing a brand new operating system using virtualization is also time consuming. Vagrant solves this problem by using a base image. In the vagrant terminology this particular base image (for example Ubuntu 12.04 base image, Ubuntu 14.04 base image, Centos 6 Base image), used for setting up a virtual machine really fast, is called a "Box". Boxes are distributed using boxfiles.
Boxes contain already installed operating system, which is the reason box files are usually a few hundred megabytes. Consider box files as a source code to setup, a virtualized base operating system inside the virtualization platform that you are using.
- Configures the virutal machine specific settings like Hostnames, Resource allocation, Network configuration etc.
- Vagrant also is capable of executing configuration management softwares like Puppet or Chef once the base system is ready(using box). Using these configuration management utilities, you will be able to setup the development environment with required application settings.
The workflow of Vagrant is depicted in the below diagram.
Apart from the things mentioned in the Vagrantfile(in the above image), It also contains details about the configuration of the virtual machine, the network settings etc. Also it includes multi VM configurations, where you need multiple independent virtual machines running different services.
As depicted in the diagram, Vagrant can work with many virtualization technologies like VMWare, VirtualBox, and even AWS Cloud. The end user(the developer for example) will simply fire up a command called #vagrant up, . Vagrant will then take care of reading the Vagrantfile in the current directory and then execute virtualization operations(ie: setting up virtual machines), and run provisioning scripts, to make the development environment ready.
There are a large number of base images available for you to use with vagrant(boxes - that provides a full base operating system upon which you can have further configurations using vagrant). You can find them here: List of Vagrant Base Images - Boxes
As everything is sitting inside virtual machines, once you have finished your work, you can simply do a #vagrant destroy, which will terminate the environment altogether. This will make the base machine, clean from the environment you created using #vagrant up
Rather than directly using virtualbox or any other virtualization platforms to create machines, and then configuring required softwares on top of it(without having any sort of consistency), developers can now simply checkout a repository(containing a Vagrantfile for setting up development environment for the project A and the configuration management recipes and modules), and then do #vagrant up(which will create a production copy in minutes.)