Virtualization for developers, Part 1
Note: Here’s another great community post. This time, John Coggeshall covers how to get started with virtualization.
Regardless of the particular programming language you consider your flavor-of-the-month, if you do any amount of significant development one thing that’s consistently a pain is your environment. From large shops that need for developers to coordinate with DevOps for deployment changes, to the freelancer who has 10 different clients to keep track of – everyone needs to keep their development environments in check.
In this three part series we are going to show you how to create robust development environments quickly using a combination of open source tools (Vagrant, VirtualBox, and Puppet) that will make management (and getting from zero to writing code) quick and easy. To get started, let’s take a look at Vagrant.
What is Vagrant?
Vagrant is an open-source tool that manages the provisioning and creation of virtual development environments quickly, easily and most importantly in a completely reproducible way. Once you have Vagrant setup for your particular code repository (which is adding a single Vagrantfile
configuration file), you will be able to create and work with a consistent virtual environment at-will regardless of what operating system your actual machine runs on.
Getting Started
To get started creating your environment, first we need to download a few tools. Vagrant itself supports many different virtual environments (called providers in Vagrant terms) such as Amazon EC2, VMware, RackSpace Cloud, and VirtualBox. Because it is FOSS, we will be using VirtualBox as our provider so let’s get started by downloading and installing both to our local host machines:
Both VirtualBox and Vagrant support all common flavors of host operating systems, so simply pick which works best for you and get them installed.
Note: As I previously mentioned in this article we are using VirtualBox for our provider – but Vagrant supports many different providers to fit your particular needs. Check out the available provider plug-ins Here
Configuring Vagrant
Once you have installed Vagrant and VirtualBox, we can get started configuring your project to use Vagrant environments! This is a super easy process, and one of the great things about it is that you can apply this to either a brand new project or an existing project pretty easily. To get started let’s create an empty git repository to store all of our code by making a new directory on our host machine and running git init
in it:
$ mkdir vagrant-demo
$ cd vagrant-demo
$ git init
Next, we are going to initialize our repository to use Vagrant environments as follows:
$ vagrant init
A `Vagrantfile` has been placed in this directory. You
are now ready to `vagrant up` your first virtual
environment! Please read the comments in the
`Vagrantfile` as well as documentation on
`vagrantup.com` for more information on using Vagrant.
As shown, running our vagrant up
command will create a Vagrantfile
in our project’s root directory and we could fire up the virtual machine immediately by simply executing a simple vagrant up
command. However, before we do that let’s talk about what is inside of this configuration file and make a couple of changes specific to the needs of our application.
The Vagrantfile
When opening the created Vagrantfile
there are plenty of comments and documentation that help explain what’s going on, and probably too many things for us to necessarily concern ourselves with for the purposes of this article. There are however a few things that we are going to want to change right off the bat which we will focus on in this article:
- Setting up some port forwarding from our virtual environment to the host machine so we do things like access the virtual machine's web server
- Set a static IP address for our virtual environment so we can find it easily from the host machine
- Change the operating system of the virtual environment to Ubuntu 12.04 (precise 64 bit)
First let’s change our operating system for the environment by changing the value of the config.vm.box
configuration value from it’s default (base
) to precise64
as shown below:
config.vm.box = "precise64"
For good measure, we probably also want to set the config.vm.box_url
which is a URL to where this box can be downloaded in case it doesn’t already exist on your machine as follows (you can find a nice collection of pre-built boxes from both the Vagrant project and others Here):
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
Next, let’s set up some networking configuration for our virtual environment by starting off setting a static IP in a private network for our VM. This is done by using the config.vm.network
configuration key with :private_network
as shown below:
config.vm.network :private_network, ip:"192.168.42.42"
This will create a private network between the virtual environment and the host machine accessible on the host machine using the 192.168.42.42
IP address.
Pro Tip: By modifying your host environment’s local DNS
hosts
file (i.e./etc/hosts
in unix-like environments) you can map this IP address to something easier to remember such asdev.example.com
Now that we have a static IP for our virtual environment let’s set up some port forwarding. Again we will be configuring things under the config.vm.network
configuration key except this time we will create a few :forwarded_port
values for services we might need to get at from our host environment as shown:
# HTTP
config.vm.network :forwarded_port, guest: 80, host:80
# MySQL
config.vm.network :forwarded_port, guest: 3306, host:3306
# MongoDb
config.vm.network :forwarded_port, guest: 27017, host: 27017
Once you have added / modified all of the configurations as described below save your Vagrantfile
. We will be returning to this file in the next article(s), but for now we are done here.
Running your Virtual Machine
Now that you have configured Vagrant through your Vagrantfile
we can now bring your new virtual environment online with one single command:
$ vagrant up
When you execute this command, if necessary it will download the necessary Vagrant box (from the config.vm.box_url
location), configure our port forwards, and dump you back at the command line. By default, Vagrant allows you to SSH into your environment quickly and easily as follows:
$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)
* Documentation: https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2
vagrant@precise64:~$
Useful Tips and Commands
Now that you have your running virtual environment there are a few things you should check out that will give you an idea of what you have. Firstly, the /vagrant
directory in your VM will be a shared folder pointing to the root directory of your project (where Vagrantfile
was located):
vagrant@precise64:~$ cd /vagrant
vagrant@precise64:/vagrant$ ls
Vagrantfile
vagrant@precise64:/vagrant$
Back in your host environment (just type exit
to close your SSH to the virtual environment like any other SSH connection), there are a few useful commands you should be aware of off-the-bat for managing your virtual environment:
vagrant up
– Bring up the virtual environmentvagrant halt
– Power down the virtual environment (i.e. same as shutting the VM down)vagrant destroy
– Completely destroy the virtual environment
Note: Running
vagrant
without parameters will give you a full list of available commands.
That’s it for now!
With that, you now have a fully functional virtual environment running Ubuntu 12.04 with ports 80, 3306, and 27017 forwarded and available to be accessed from the host machine. From here you can SSH, play around, and have no fear of breaking something – after all, no matter how badly you break your VM you can always just destroy it and re-create it in a matter of minutes! Even better than that, any changes you make to your virtual environment configuration (the Vagrantfile
) can be tracked by committing to git.
In the second part of this series, we will introduce the next phase of our virtual environment by introducing puppet into the mix as a provisioner for the virtual machine which will allow us to configure the details of our virtual machine in a managed way.
Back in your host environment (just type exit to close your SSH to the virtual environment like any other SSH connection), there are a few useful commands you should be aware of off-the-bat for managing your virtual environment:
vagrant up – Bring up the virtual environment vagrant halt – Power down the virtual environment (i.e. same as shutting the VM down) vagrant destroy – Completely destroy the virtual environment Note: Running vagrant without parameters will give you a full list of available commands. THAT’S IT FOR NOW!
With that, you now have a fully functional virtual environment running Ubuntu 12.04 with ports 80, 3306, and 27017 forwarded and available to be accessed from the host machine. From here you can SSH, play around, and have no fear of breaking something – after all, no matter how badly you break your VM you can always just destroy it and re-create it in a matter of minutes! Even better than that, any changes you make to your virtual environment configuration (the Vagrantfile) can be tracked by committing to git.
In the second part of this series, we will introduce the next phase of our virtual environment by introducing puppet into the mix as a provisioner for the virtual machine which will allow us to configure the details of our virtual machine in a managed way.
Share your thoughts with @engineyard on Twitter