Vagrant And DigitalOcean On Ubuntu

Overview

Vagrant is a piece of software which allows (re)creation of virtual environments. By default it supports VirtualBox but other “providers” can also be used such as Amazon Web Services, VMware, etc. Vagrant creates virtual machines with specific resources and can also run post creation setup also known as provisionig. Provisioning comes as basic SSH shell commands but this can be extended with platforms such as chef, puppet, etc.

Setup

Go here to download the latest version of Vagrant.

The downloaded file needs to be installed:
sudo dpkg -i vagrant_1.3.5_x86_64.deb

Configure

Create a directory where the vagrant project will be set up:
mkdir vagrant

go into the project directory and initialise vagrant:
cd vagrant
vagrant init

A Vagrant file will be created called Vagrantfile. This file contains the configuration required to provision a new environment. An environment in Vagrant is called a Box. The Vagrantfile should be added to a source control of your choice.

Modify the Vagrantfile to what you want it to create.

Box

Vagrant requires boxes which are prepackaged environments it can create from. Vagrant Cloud is the community site where people can submit boxes for others to use. These boxes are not vetted so take precausitions when choosing a box from the cloud.

One problem I had encountered was the lack of boxes when a new version of a distro comes out. Creating boxes are out of scope of this article.

Omnibus

The Vagrant-Omnibus plugin is required so that it can install vagrant on the new VM. Without it Vagrant won’t be able to provision anything because it doesn’t install Vagrant on the target machine. Run the following command to install the plugin:
vagrant plugin install vagrant-omnibus

The following line needs to be added to the Vagrantfile:
config.omnibus.chef_version = :latest
where :latest is the version of chef it will install. A specific version can be install by changing the above line to something like this:
config.omnibus.chef_version = "11.4.0"

Additional Providers

Providers are software or service which provide the boxes. This can range from Virtualbox to AWS. Vagrant by default comes with Virtualbox provider. To install Digital Ocean provider:
vagrant plugin install vagrant-digitalocean

If not already done so, SSH keys need to be generated and added to Digital Ocean:
ssh-keygen -t rsa -C "me@gmail.com" -b 2048
Make sure you replace the email address above. The defaults should be ok but set a passphrase if you want to password protect the key. The -b 2048 is the key length in bits. I personally wouldn’t go lower than 2048 but it’s personal preference. Longer the key, the more of a performance degradation will appear.

Copy and paste the public key and add it to Digital Ocean:
cat ~/.ssh/id_rsa.pub
Digital Ocean key

Example Vagrantfile

Usage

To provision a new box use the following command:
vagrant up
This will create a new droplet of a set size and name.

To specify the provider to use e.g Digital Ocean:
vagrant up --provider=digital_ocean

To delete the VM:
vagrant destroy

Summary

Vagrant is very quick to setup and has a lot of re-usability. Once the Vagrantfile is built up, it can be sent to other people to create the exact same environment which is one of the big appeals. It uses Ruby so some knowledge of Ruby is required as well but the Debian package doesn’t need Ruby or Rails installed to run.

Example code
AndrewDryga / digital-ocean
Devops: Vagrant with AWS EC2 & Digital Ocean
Setup Ruby On Rails on Ubuntu 14.04 Trusty Tahr
Set the default Vagrant provider from your Vagrantfile

About Danny

I.T software professional always studying and applying the knowledge gained and one way of doing this is to blog. Danny also has participates in a part time project called Energy@Home [http://code.google.com/p/energyathome/] for monitoring energy usage on a premise. Dedicated to I.T since studying pure Information Technology since the age of 16, Danny Tsang working in the field that he has aimed for since leaving school. View all posts by Danny → This entry was posted in Infrastructure and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply