/ DevOps

Learn Vagrant

Overview

Vagrant is a command line utility for managing the lifecycle of virtual machines. The official website is Vargrant.

Why vagrant?

Vagrant as DevOps tools can use for manage the lifecycle of virtual machine and it can make the environment more easier for developers and Operators. And compare with Docker, it's more convenient and more suitable for our situation.

Current situation in the team, we are using hard disk to store the virtual machine template and provide the download or just copy the virtual machine. And we have the lifecycle for the template. E.g. We have difference versions for Ubuntu 12.04 to build for M30/Z38, Ubuntu14.04 to build M30 with Python3.5 feature. Also Ubuntu16.04 for the ABOX42.IO. But we don't have the patches or file to describe the differences between 2 versions. When new virtual machine template are released, sometimes just need to install some tools or dependency libraries. But we still need to copy the virtual machine again and the whole workspace are interrupt. It's not professional.

Difference between Vagrant and Docker

In the meantime, there are two famous virtual machine management in the market. One is vagrant, another one is Docker. In the previous blog, I have already introduce the Docker, and there are also some very awesome features in the Docker. Docker is using LXC(Linux Container) to make each container are independent. Docker have the concept for image, container. Switch to Vagrant, vagrant is based on virtual box (from Oracle) first, but now more and more virtual machine tools are supported. E.g. VMware Fusion. And mapping to Docker image, vagrant named 'box'. Machine is mapping to container. We can download the box as the startup virtual machine and execute shell script to install necessary tools. In Docker, the container is not persist data for the own lifecycle, when you stop the container, it will lost data. Vagrant is more like basic virtual machine, we can use suspend, halt and resume. Only if when you destroy the vagrant machine, the VM will be lost.

Pros for Vagrant

Vagrant is virtual machine and plus management, and it's easy to use ssh, share folder or shell script. And easy to understand the process between guest and host machine. E.g. The first vagrant machine will mapping guest port 22 (which use for SSH) to the guest 2222. Then when you use vagrant ssh is just help you to SSH to the guest machine. It's very easy to use.

In Docker, SSH to the guest container is not simple as Vagrant, you need to use -i -t to make the docker container running in background and not to stop.

How to start?

Please check this page to download and install Vagrant, the latest version is 1.8.5. Vagrant is supported Mac, Windows, DEBIAN, CentOS.

The first command for vagrant is very simple, just need vagrant init and then vagrant up, there will be a new virtual machine named 'default' up and running. In the meantime, it will generate a VagrantFile in the path.

VagrantFile

The syntax of Vagrantfiles is Ruby, but knowledge of the Ruby programming language is not necessary to make modifications to the Vagrantfile.

Let's see some example for VagrantFile.

Configuration version
Vagrant.configure("2") do |config|
  # ...
end

Configuration versions are the mechanism by which Vagrant 1.1+ is able to remain backwards compatible with Vagrant 1.0.x Vagrantfiles, while introducing dramatically new features and configuration options.

Box setting
config.vm.box = "ubuntu/trusty64"

This setting is describe this box will use ubuntu14.04 as the box. If there is no available box in your host machine, it will find and download from the internet.

Another option is using config.vm.box_url which is your pointed url to download the related box. Also you can download the box first then just point the config.vm.box as the file://<path to your box>

config.vm.hostname = "ubuntu14.04"

By using hostname, Vagrant can help you to setup the hostname in the boot time.

Network
config.vm.network "forwarded_port", guest: 80, host: 8080

This setting is use for mapping the guest 80 port to host 8080, then you can visit the guest application by http://localhost:8080. There is a default port mapping for SSH. guest:22 to host:2222(or 2200, 2201...)

config.vm.network "public_network"

This public network is just like NAT bridge in VMware.

Synced Folders
config.vm.synced_folder "./shard-folder", "/mnt/shard-folder"

This setting will help you to mount the host ./shard-folder to your guest /mnt/shared-folder. It will be very useful when you are developing.

Notes:
For Vagrant 1.8.5, and use official box Ubuntu 16.04 ubuntu-xenial will meet the issue can't mount the shared-folder. The following steps can fix it.

  • vagrant plugin install vagrant-vaguest running on host
  • sudo ln -s /opt/VBoxGuestAdditions-5.0.26/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions running on guest
  • vagrant reload running on host
Provision

Provisioners in Vagrant allow you to automatically install software, alter configurations, and more on the machine as part of the vagrant up process.

config.vm.provision "shell", path:"./script/vagrant/bootstrap.sh"

This line is means vagrant will help you to run bootstrap.sh in the boot time when you are first vagrant up. Also you can run vagrant provision to ask vagrant to help you to run this script again. This can make the lifecycle of our situation be more convenient.

More detail please visit docs