Vagrant - Simplified and mastered!
What is this Vagrant?
Official definition of it is:-"Vagrant provides easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent workflow to help maximise the productivity and flexibility of you and your team."Quite simply Vagrant is a tool using which we can create Virtual machines programatically, its like an API!
Brushing up quickly!
Virtual Machine is quite simply a "Virtual" or emulation of a computer, a smaller bits of a server
etched out of a powerful machine with huge hardware resources. Like for example suppose I want a Ubuntu, CentOS and Windows machines to test something and only have a fully loaded Mac, I can install a "Hypervisor" on my Mac and create Virtual Machines or VMs with 1GB of Memory each, a share of my processor and harddisk and then install different kinds of OSs each and run them all simultaneously having my own network of computers coming out uniquely from one mac.Like in the screenshot below we can see that on a Mac, we have different versions of Windows-- 7 and XP and then Ubuntu too.
Host machines is the powerful machines that is cut into chunks of VM instances which are called Guest Machines. And hypervisors are the ones which creates VMs out of large machines.
What's a Hypervisor?
A hypervisor also called virtual machine monitor (VMM) is a piece of computer software, firmware or hardware that can help cut the large machines resources to smaller VMs.
These are two types again:-
- Type 1 - These are run directly on the host's hardware to control the hardware and to manage guest operating systems. A guest operating-system thus runs on another level above the hypervisor.
- Type 2 - run within a conventional operating-system environment. With the hypervisor layer as a distinct second software level, guest operating-systems run at the third level above the hardware. Example is Virtualbox or VMWare Workstation
VirtualBox is a virtualisation software, it quite simply means that you can install VirutalBox on a machine with ample CPU, memory and hard disk and create several instances of machines with lesser capacity running on them. Like if your machine's power is a cake, this helps to divide it to different requirements. Like cutting out smaller desktops from a supercomputer.
You can see this video to see how a VM can be created using VirtualBox and what ever is being done manually or using the Graphical User Interface (GUI) can be done programatically using Vagrant! Quite simply put Vagrant is the API way of creating VMs.
Vagrant by definition
Vagrant provides easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent workflow to help maximize the productivity and flexibility of you and your team.
Machines are provisioned on top of VirtualBox, VMware, AWS, or any other provider. Then, industry-standard provisioning tools such as shell scripts, Chef, or Puppet, can be used to automatically install and configure software on the machine.
Which just means that what ever was done in the video to bring up the VM can be put in a config file called VagrantFile and when we do a vagrant up, a VM is spun for us in a few seconds!
Vagrant Installation!
Vagrant has been written in a programming language called Ruby, but that doesn't mean that you need to know Ruby to use it.
And we are lucky to now be able to just download the installer and install it unlike doing a rubygem install earlier.
At the time or writing this doc, I have version 1.4.3 available. This doesn't seem to be backward compatible with 1.0 versions, but 1.1.x are compatible with them. Upgrading older versions to new ones is as simple as downloading the latest version and installing it over the older versions.
However, if your version of Vagrant was installed via RubyGems, then you must
gem uninstall
On Mac OS X, remove the /Applications/Vagrant directory and the/usr/bin/vagrant file.
On Linux, remove the /opt/vagrant directory and the /usr/bin/vagrant file.
REMOVING USER DATA
Removing the user data will remove all boxes, plugins, and any stored state that may be used by Vagrant. Removing the user data effectively makes Vagrant think it is once again a fresh install.
On every platform, remove the ~/.vagrant.d directory to delete the user data.
Installing Vagrant is very simple, you just need to download the installer and run the installer
Using Vagrant:
Lets first try to spin up a CentOS VM on a VirtualBox, to confirm if Vagrant has been installed and working, just open a terminal and type
vagrant -v
this should give you the version of Vagrant
Brining up a VM with Centos installed on it.
Before we do that, lets see whats a Box?
As per the def: it is
Instead of building a virtual machine from scratch, which would be a slow and tedious process, Vagrant uses a base image to quickly clone a virtual machine. These base images are known as boxes in Vagrant, and specifying the box to use for your Vagrant environment is always the first step after creating a new Vagrantfile.
As per the def: it is
Instead of building a virtual machine from scratch, which would be a slow and tedious process, Vagrant uses a base image to quickly clone a virtual machine. These base images are known as boxes in Vagrant, and specifying the box to use for your Vagrant environment is always the first step after creating a new Vagrantfile.
Quite simply It's a binary file which you could call as a blueprint or a template using which a VM is built. Like for example now that we need a CentOS VM, someone might have created a template or a box file already and made it available for us here.
We see several flavours of it available, different versions, some have puppet already installed and so on...
We can choose what ever we need our VM to be like,
CentOS 6.4 x86_64 minimal seems to be an ideal one for us. So we copy the URL of where its located. ==> https://dl.dropbox.com/u/17738575/CentOS-5.8-x86_64.box
Someone has built the above box and kind enough to share with all, latter on we will see how we too can create these and share it with others.
And the syntax to bring up a VM from an existing Box file is
Open a terminal, and create a directory, change to that Directory lets get started!
End result is that we have two VMs created.
To connect to these VMs we once again can use vagrant,
vagrant ssh
and typing "exit" will bring you out of this VM.