Advanced Devops With Vagrant and LXC

This post was originally published on the SHIFT developer blog.

Creating and testing new databases that require clustering can be a pain point when trying to do everything on a local machine. Simulating failures or network failures can be difficult or impossible if everything you’re testing is running on the same machine. To better simulate your production environment you can try using LXC (Linux Containers). A linux container is a lot like a Virtual Machine, but shares the host’s Kernel and as a result has very little overhead. A limitation of this is that you can’t mix different environments - for example you can’t run Windows in a container on a Linux host. Theoretically it’s possible to run different Linux distros but so far it seems like there’s a few hiccups doing this.

Like a full Virtual Machine, a container can be suspended or shutdown easily. It has it’s own IP address and isolated disk space. Unlike a VM you have direct access to the file system from your host, so you can share directories easily by mounting them in the container.

If you’re looking to set up an environment where this is repeatable and automated, VirtualBox + Vagrant is a winner. Vagrant combines provisioining with Puppet or Chef with VirtualBox to be able to easily launch Virtual Machines tailored to a specific purpose. For this article I’ll assume you’ve already created a VM and are using it.

Let’s get started. First, get the base LXC packages:

apt-get install lxc

In order to SSH into your VM, you’ll need a local DNS entry and a network bridge. Fortunately, dnsmasq gets installed when you install lxc through apt, as well as a bridge set up, so you should already have it running. You’ll need to tell your system to send requests to it - just add the IP of the bridge to your /etc/resolvconf/resolv.conf.d/head (you can see the IP of your bridge by looking at ifconfig for lxcbr0)

nameserver 10.0.3.1

Then reload your resolve.conf

resolvconf -u

Lets create a new container creatively called jonhaddad

lxc-create -t ubuntu -n jonhaddad

Start it up in daemonized mode:

lxc-start -d -n jonhaddad

And connect:

ssh ubuntu@jonhaddad

The password is ubuntu.

You should now be able to connect to your container’s environment. You can install anything you want in here and it’ll be isolated to the container. You can create dozens of containers to simulate dozens of machines, deployments, failures, and behaviors that normally require an entire cluster.

When you’re finished, you can shut it down like so:

lxc-stop -n jonhaddad

In the next blog post we’ll be covering customizing your containers for various purposes.

If you found this post helpful, please consider sharing to your network. I'm also available to help you be successful with your distributed systems! Please reach out if you're interested in working with me, and I'll be happy to schedule a free one-hour consultation.