If you are a developer or DevOps engineer, there is a very good chance that you have already used Vagrant — a personal virtualization management tool that can be used to create virtual machines on your laptop or desktop. Usually, that means Linux VMs on a Windows or Mac laptop.
Vagrant is actually an application that manages the lifecycle of virtual machines on the personal computer. Under the hood, it requires a Hypervisor to create and run a VM and most often that application is VirtualBox, because it is license-free. It also supports Hyper-V and VMWare.
Install VirtualBox and Vagrant on your personal computer — or a host machine in virtualization terminology. The VMs created on your computer are guest machines.
Download the VirtualBox image that is suitable for your machine's OS (Windows, OS-X, etc.) and platform and architecture (Intel x86, AMD64, etc.). Choose the defaults for installation.
Download Vagrant for the target OS and install it, again choosing the defaults.
With the above prep, you are ready to create your first VM and SSH into it.
Bring up Command Prompt on Windows or Terminal on Mac, and run the following commands to start an Ubuntu machine.
cd into a directory where you want to keep the Vagrant stuff. Create a new one if needed.
There are lots of things that happen behind the scenes (which we will go over soon), but that's all you need to bring up your Ubuntu host on your local machine.
On Windows, it is relatively difficult because it doesn't have a native SSH client. When a guest machine is created, the user vagrant is added to it, along with its public key. The private key for that user is available on the host machine, and the path to the public key and the port where SSHD runs on the guest machine (to which SSH clients should connect), can be listed using the command "vagrant ssh-config".
Obtain that info with an SSH client like PuTTY to access the guest machine.
If your requirement is only to create an Ubuntu host and do something with it, then there isn't much you need to know on what happens when commands like init and up are running. However, if you want to use Vagrant as an environment for development and automation, you need to know more than the simple provisioning steps above to rollout Vagrant-based solutions.
All the configurations used to provision the VMs using Vagrant are stored in the Vagrantfile. Though the name and location of this file are configurable, normally, it is in the current directory from where the Vagrant commands are run.
Though the default version of the Vagrantfile is long, most of it is commented out with explanations for the supported options. The active config settings are seen above.
The installation of VirtualBox is a one time setup on your laptop/desktop. After that you can configure the machines you need in the Vagrantfile, and those could be started up.
A guest machine would go through few phases before it would be deleted from the host machine. If the provisioning steps are fully captured in the Vagrantfile and the scripts and playbooks Vagrant would use to manage the lifecycle of the VM, it can be recreated anytime.
up - This command is used to startup a VM as defined in the Vagrantfile. If it is not already on the host, it will be created new. Once the guest machine is up and running, users can login into it.
provision - A provisioner defined in Vagrantfile can run against and already running guest machine.
reload - If the Vagrantfile is changed after a guest machine is created, this command can be used to restart the VM with the changed configurations.
suspend/resume - As the commands suggest those can be used to suspend or resume a guest machine.
halt - Shuts down the guest machine but the underlying resources are still retained.
destroy - Shuts down a guest machine and removes its related resources from the host machine.
As it is possible to create multiple guest machines on the host, various network operational tasks could be done with them as well, all managed by Vagrant.
In this case, if an HTTP server is running on port 80 on the guest machine it could be accessed as http://host-machine-name:8080.
There is a public network option also supported by Vagrant to expose the guest machines to public Internet. I am not very clear about its use as Vagrant is primarily used as a development infrastructure.
The first entry is the actual folder is on the host machine and the second entry is how it would be mounted on the guest machine. If the source path is not absolute it is relative to the root directory.
Multiple methods like rsync, NFS and and SMB are used to support sharing of the directories between host and guest machines. The behavior of a shared folder will depend on which method has been used to set it up.
Various configuration options that we had discussed above could be applied to each guest machine separately. These machines could share common configurations also as the inline shell provisioner applied to both machines in the last example.
The real power of Vagrant is unleashed by setting up multi-machine environments for development and testing on your laptop. As all the steps involved in configuring such complex environments are captured in Vagrantfile and related scripts, they could be shared between team members and put under revision control.
If CM provisioners like the Ansible option we had looked at earlier are used for building Vagrant based development environments, promoting those to production is very easy.
Also, such a process of setting up and maintaining of development environment, expressed in code, would shadow the application development process. Like the application code is managed, the Vagrant code could be shared between development team members and it could be version controlled.
If the company doesn't maintain any permanent infrastructure in a data center or colo, and use only public cloud, Vagrant can be used to minimize the computational cost by setting up development environments locally on laptops which tend to be very powerful and they can run multiple guest machines.
The Vagrant documentation is simple and short and it must be read in full before if you plan to do anything extensive using Vagrant.