PermalinkIntroduction
In this article, let's understand "Vagrant" and its significance along with the key concepts around it.
PermalinkNecessity of Vagrant
Precisely, Vagrant is a command line tool to create and manage VMs. If we wish to provision VMs, we usually use virtual box, VMware, or any cloud service provider. This involves launching either of these and then manually provisioning the resources for the VM(s) by configuring from the UI. This is where Vagrant comes into the picture by automating or codifying the process of creating VM(s). For people who create VMs manually, Vagrant is magic for you.
PermalinkVM Management Problems
- OS Installations
- Time Consumption
- Manual Setup
- Tough replication for multiple VMs
- Creating and maintaining documentation for multi VMs
PermalinkAdvantage of Vagrant for VMs
- No OS installations
- VM setup through images (vagrant boxes)
- Images/boxes available in vagrant cloud
- Manage VMs with a file (vagrantfile)
- VM configuration change through Vagrantfile
- Vagrant commands to manage VMs
- Regular execution of scripts inside VMs
PermalinkWhat is Vagrantfile?
The Vagrantfile is a Ruby file used to configure the VMs and contains instructions on how to provision these machines. Although the contents of a Vagrantfile are Ruby, knowledge of Ruby is not necessary to make modifications to the file since it is mostly simple variable assignment.
Vagrantfiles are meant to be committed directly to version control and are expected to behave identically on any system which runs Vagrant. The idea is that when a developer checks out some code from version control that has a Vagrantfile, that developer can simply vagrant up to get a fully provisioned virtual environment to develop that product.
PermalinkSome of the Jargons used in Vagrant
- Box: A box is a ready-made base image of a Virtual Machine Environment.
- Provider: Providers are the services that Vagrant uses to set up and create virtual environments. Support for VirtualBox, Hyper-V, and Docker virtualization ships with Vagrant, while VMware and AWS are supported via plugins.
- Provisioner: Launching a blank virtual machine is not very useful and hence provisioners allow you to easily set up your virtual machine with everything it needs to run your software.
PermalinkVagrant Workflow
To start with, we have assumed that the vagrant tool has been installed in our local system. Once we issue the command
vagrant up
, vagrant is going to read the Vagrantfile which would be located in our local directory. Vagrant is going to look for a box name within Vagrantfile and then it is going to look for the box in the local system. In case it is not available locally, vagrant will go to the vagrant cloud and download that box. Once the download is completed, it is going to read the other configurations like network provisioning, memory and disk size, and so on. Following this, it would look for the type 2 hypervisor (oracle virtual box) and try connecting to it. Vagrant is going to send all information regarding the virtual system. The oracle virtual box would then create VMs as per the information received from vagrant.
PermalinkVM setup with Vagrant
- Download and install the vagrant tool in your system.
- Download any hypervisor tool like Oracle VirtualBox.
- Search for a box name. It's recommended to use official boxes only.
- Create a directory somewhere in your system.
- Open your terminal/cmd prompt and navigate to the created directory and enter the command
vagrant init <box_name>
. This would create a Vagrantfile for the specified image name within the directory. - Then trigger the command
vagrant up
and wait for some time while the virtual machine is being created and configured as per the Vagrantfile.