Part 1: Vagrant

5 Oct

I love vagrant and you should too. According to its web page, Vagrant is a tool for building and distributing virtualized development environments. It uses Virtual Box to provide virtualization and allows you to do really great things quickly. It’s literally become one of my favorite tools in the toolbox almost overnight.  I use it to rapidly spin up test infrastructure, vet my changes and to have a much higher degree of confidence in the effects of my architecture decisions.

Getting Vagrant going is pretty simple. On MacOS, I recommend you first get a more current ruby installed using rbenv or rvm. You’ll also need a copy of Virtual Box. At present, 4.1 is supported. If in doubt, you should upgrade.

Next step is to install the vagrant gem

$ gem install vagrant

Now that vagrant is installed you can download a base box. Base boxes are standard templates that are used as building blocks for a virtual machine. There are a bunch of publicly available base boxes. You can find a listing of a bunch here. You can also optionally build your own base boxes. I’ll explain how to accomplish this task in a later post. Once you’ve selected your base box(es) of choice, you can add them as follows:


$ vagrant box add lucid32 http://files.vagrantup.com/lucid32.box
[vagrant] Downloading with Vagrant::Downloaders::HTTP...
[vagrant] Downloading box: http://files.vagrantup.com/lucid32.box
[vagrant] Extracting box...
[vagrant] Verifying box...
[vagrant] Cleaning up downloaded box...

This command will download the base box from the internet and install it under $HOME/.vagrant.d/boxes/. It is important to know that this image will not be used directly by Virtual Box and that to actually make use of the base, you must create a vagrant vm. Our first step is initialize the directory we’ll be using for vagrant.


$ mkdir vagrant-test
$ cd vagrant-test
$ vagrant init lucid32
      create  Vagrantfile

This creates a Vagrantfile in the current directory. The Vagrantfile contains configuration information for the virtual box vm. You can assign things like static addresses or set up configuration management tools like puppet or chef from this configuration file. At this point, we’ve got some config data but we haven’t actually instantiated the vm. We can do this as follows:


$ vagrant up
[default] Importing base box 'lucid32'...
[default] Matching MAC address for NAT networking...
[default] Clearing any previously set forwarded ports...
[default] Forwarding ports...
[default] -- ssh: 22 => 2222 (adapter 1)
[default] Creating shared folders metadata...
[default] Running any VM customizations...
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Mounting shared folders...
[default] -- v-root: /vagrant

Cool, that seemed to work, but what have we actually accomplished? At this point we have a running VM which is based on a copy of the lucid32 base box image we downloaded. Vagrant has automatically set up some handy shared folders for us that will be helpful for moving data back and forth from the host OS to the VM. You can see that they’ve been mounted on /vagrant in the guest. It has also provisioned SSH using a port forward. You can access the VM using the following:


$ vagrant ssh
Linux lucid32 2.6.32-33-generic #70-Ubuntu SMP Thu Jul 7 21:09:46 UTC 2011 i686 GNU/Linux
Ubuntu 10.04.3 LTS

Welcome to Ubuntu!
* Documentation: https://help.ubuntu.com/
Last login: Thu Jul 21 13:07:53 2011 from 10.0.2.2
vagrant@lucid32:~$

Inside the VM, we can verify that our shared directory is mounted


vagrant@lucid32:~$ ls /vagrant/
Vagrantfile

From the host, we can easily suspend or halt the virtual machine.


$ vagrant suspend
[default] Saving VM state and suspending execution...

The real power of vagrant lies in the ability to quickly recreate environments. We can destroy our VM completely and then recreate it from the base box image using a pristine disk image using two commands.


$ vagrant destroy
[default] Discarding saved state of VM...
[default] Destroying VM and associated drives...
$ vagrant up
[default] Importing base box 'lucid32'...
[default] Matching MAC address for NAT networking...
[default] Clearing any previously set forwarded ports...
[default] Forwarding ports...
[default] -- ssh: 22 => 2222 (adapter 1)
[default] Creating shared folders metadata...
[default] Running any VM customizations...
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Mounting shared folders...
[default] -- v-root: /vagrant

Think about that for a second. You can trash your dev environment and start from scratch inside of a minute or two. That’s pretty amazing in and of itself, but with the integration with Chef and Puppet, things get even cooler. In part 2, we discuss building your own base boxes with VeeWee.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 74 other followers

%d bloggers like this: