Step Into Vagrant

I have idea about what Vagrant is because a training used it providing a environment long time ago, but thinking how to play it now, the bad is that my memory is empty, now i try to recover those basic things, and step into a little bit with others.

Vagrant

Create and configure lightweight, reproducible, and portable development environments
Written in Ruby, a computer software that creates and configures virtual development environments
It can be seen as a higher-level wrapper around virtualization software such as

  • VirtualBox
  • VMware
  • KVM
  • Linux Containers (LXC)

and around configuration management software such as

  • Ansible
  • Chef
  • Salt
  • Puppet

supports server environments like Amazon EC2 , Natively support Docker containers.
Refer

Act on

  • for Dev, unify development environment for team
  • for Ops, standardize same server environment

common commands

1
2
3
4
5
6
7
8
$ vagrant init		// initialize 
$ vagrant up // start vm
$ vagrant halt // stop vm
$ vagrant reload // restart vm
$ vagrant ssh // access vm by ssh
$ vagrant status // check vm status
$ vagrant destroy // destory current vm
$ vagrant package // package.box can be distributed

Step by Step

1
2
3
4
5
6
7
8
9
10
11
12
// create vagrantfile
$ vagrant init // create a box image file on current directory
// add vagrant image
$ vagrant box add hashicorp/precise64 // download from vagrant cloud, Ubuntu OS
// configure vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
end
// launch vagrant
$ vagrant up
// access virtual machine by ssh
$ vagrant ssh // directory sychronized ( /vagrant of VM <> current directory Vagrantfile located)

Automated Provisioning

automatically install softwares required

step 1

create a Bash script(bootstrap.sh) at directory Vagrantfile located

1
2
3
4
5
//#!/usr/bin/env bash
apt-get update
apt-get install -y apache2
rm -rf /var/www
ln -fs /vagrant /var/www

step 2

configure Vagrant

1
2
3
4
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.provision : shell, path: "bootstrap.sh" //vagrant use shell provisioner
end

Vagrant network

allow Host machine access the Vagrant VM
forwarded_port map host’s port 4567 to guest’s (VM) port 80.

1
2
3
4
5
6
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.network :forwarded_port, host: 4567, guest: 80
// or > config.vm.network : private_network, ip:"192.168.1.101"
end

visit http://127.0.0.1:4567 , access VM after it restarts // http://192.168.1.101

1
$vagrant reload // restart VM

docker vs vagrant

comparison 1
comparison 2
comparison 3

  • Vagrant

for development stage, allow you to install all you need to develop your application in an easy and simple way
a virtual machine manager, it allows you to script the virtual machine configuration as well as the provisioning. However, it is still a virtual machine depending on Virtual Box (or others) with a huge overhead. It requires you to have a hard drive file that can be huge, it takes a lot of ram, and performance can be not very good.

  • Docker

for production stage, a technology that allows you to run self-contained applications eliminating worries about dependencies and libraries
uses kernel cgroup and namespacing via lxc. It means that you are using the same kernel as the host and the same file system. You can use Dockerfile with the docker build command in order to handle the provisioning and configuration of your container. You have example at docs.docker.com on how to make your Dockerfile, it is very intuitive.

The only reason you could want to use vagrant is if you need to do BSD, Windows or other non-linux development on your ubuntu box. Otherwise, go for Docker.

docker & vagrant complementing each other

  1. Install a Vagrant virtual machine in your computer containing the same OS you will have in your server ( normally Ubuntu Linux 12.04 LTS 64 bits). This means that you can program in any OS you want and still expect your program will run in your server.
  2. Install your Docker packages to create Docker containers inside your virtual machine created for Vagrant. This step is better if you can install them through an script.
  3. Inside your containers put your applications ( Nginx, Memcached, MongoDB, etc)
  4. Configure a shell script, Puppet or Chef script to install Docker and run your Docker containers each time Vagrant begins.
  5. Test your containers in your Vagrant VM inside your computer.
  6. Thanks to providers now you can take the same file ( your Vagrant file ) and just type vagrant up —provider=“provider” where the provider is your next host and Vagrant will take care of everything. For example, if you choose AWS then Vagrant will: Connect to your AMI in AWS, install the same OS you used in your computer, install Docker, launch your Docker containers and give you a ssh session.
  7. Test your containers in AWS and look that they behave exactly as you expect.