Sep 06

Using Chef to install mongoDB in a Vagrant box

Lately I have had the pleasure of playing around with a couple of new tools. New for me at least. I like to have a system as clean as possible. At work I have the pleasure of working with different technologies and different development environments and different databases. Installing all major databases and several different versions of it can mess up your system. So I started playing around with Vagrant for the different scenarios. This blog post will get you started with Vagrant as a tool and show what needs to be done (at least what I did) to get up an running with mongoDB in a Vagrant box.

mongoDB

mongoDB is and open-source document database. It is written in C/C++ and stores information in the form of JSON-style documents for you. There are several more features that would go into too much detail and that I (currently) do not require, they are pretty interesting nonetheless: Auto-Sharding, Replication, Map/Reduce etc. We went with mongoDB to give it a spin and get a feeling for its handling of data and getting to use NoSQL features and to get familiar with NoSQL in general.

Vagrant

“Vagrant is a tool for building complete development environments” on your computer, inside a virtual machine. One of the upsides is that you could for example use it to handle a linux virtual machine on your Windows or Mac OS computer. For this Vagrant builds upon VmWare and VirtualBox for the virtualization of the VMs. It has been around for a couple of years now and felt stable in my usage. One could also describe it as a wrapper around VmWare or VirtualBox machine handling.

All there is needed to get started with Vagrant – after installing it on your system (Note: you also need either VmWare or VirtualBox installed) – are just 3 lines on your command line:

$ vagrant box add base http://files.vagrantup.com/lucid32.box
$ vagrant init 
$ vagrant up

This will download and add a base box to your system, the box is supplied by Vagrant. There are several other providers of boxes available. The box I am currently using I got from Opscode: bento on github – which comes with a newer version of Chef pre-installed (at least the older baseboxes). To connect to your new virtual machine just go to the directory of your Vagrantfile and run:

vagrant ssh

Chef

Chef is built to address the hardest infrastructure challenges on the planet. It transforms infrastructure into code. Into Ruby code. In a nutshell: There are recipes that you can use with chef that have the basic instructions ready on how a software (e.g. vim or mongoDB) should be installed. No more manual apt-get install xyz, though you can still do that. You can read here why you should use Chef. Chef is not a must use for Vagrant, you could use Puppet or not use any provisioning tool at all and just do the installation manually.

I use chef in a way that I download the required recipes from opscode and supply them in a directory that I later reference in my Vagrantfile.

My Vagrantfile

The Vagrantfile is the main configuration point for your virtual machine in Vagrant. This is where you tell Vagrant what software to install, which ports to forward, which folders to hook up on the host and guest system. The most important part of my Vagrantfile will be explained here.

  config.vm.box = "opscode-ubuntu-12.04"
  config.vm.box_url = "https://opscode-vm/.../opscode_ubuntu-12.04_chef-11.4.4.box"

This tells Vagrant to use the opscode supplied box as a base vm. To be able to download it on the first vagrant up I need to supply the URL to it, too.

config.vm.network :forwarded_port, guest: 27017, host: 27018

mongoDB by default runs on port 27017. To distinguish from the default port I use port 27018 on my local system, so to connect to it I can use localhost:27018.

  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path ="../recipes"
    chef.add_recipe "git"
    chef.add_recipe "mongodb::10gen_repo"
    chef.add_recipe "vim"
    chef.add_recipe "curl"
    chef.add_recipe "apt"
    chef.add_recipe "build-essential"
  end

This is the Chef setup. All referenced recipes are located in the folder of the cookbooks_path. Note the mongodb::10gen_repo snippet, this is required to get the latest stable version and not the version that is supplied by the guest system you are using. Depending on the feature of mongoDB you wanna use this can make a big difference. This is all there is to do to get a working virtual machine with mongoDB installed and ready to use. Adjust for your system in case you have to use different ports and or other recipes. After that just run

vagrant up

and get started using mongoDB on a vm of your choice.

Conclusion

As a result of this setup I can stop the Mongo DB box and my system is clean of any references to it. Whenever I want to use a different database I just boot up that VM if required. Another advantage: Once the setup is done and everything is working I could just go ahead and copy my Vagrantfile and the folders in my vagrant directory for a co worker and he could get started right away, or I just copy my box and give it to her.

Tags: chef, mongodb, vagrant