Making LXD fly on Ubuntu!

Since my last article, lots of things happened in the container world! Instead of using LXC, I find myself using the next great thing much much more now, namely LXC's big brother, LXD.

As some people asked me, here's my trick to make containers use my host as an apt proxy, significantly speeding up deployment times for both manual and juju-based workloads.

Metal As An Attitude

Setting up a cache on the host

First off, we'll want to setup an apt cache on the host. As is usually the case in the Ubuntu world, it all starts with an apt-get:

sudo apt-get install squid-deb-proxy

This will setup a squid caching proxy on your host, with a specific apt configuration listening on port 8000.

Since it is tuned for larger machines by default, I find myself wanting to make it use a slightly smaller disk cache, using 2Gb instead of the default 40Gb is way more reasonable on my laptop.

Simply editing the config file takes care of that:

$EDITOR /etc/squid-deb-proxy/squid-deb-proxy.conf
# Look for the "cache_dir aufs" line and replace with:
cache_dir aufs /var/cache/squid-deb-proxy 2000 16 256 # 2 gb

Of course you'll need to restart the service after that:

sudo service squid-deb-proxy restart

Setting up LXD

Compared to the similar procedure on LXC, setting up LXD is a breeze! LXD comes with configuration templates, and so we can conveniently either create a new template if we want to use the proxy selectively, or simply add the configuration to the "default" template, and all our containers will use the proxy, always!

In the default template

Since I never turn the proxy off on my laptop I saw no reason to apply the proxy selectively, and simply added it to the default profile:

export LXD_ADDRESS=$(ifconfig lxdbr0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
echo -e "#cloud-config\napt:\n proxy: http://$LXD_ADDRESS:8000" | lxc profile set default user.user-data -

Of course the first part of the first command line automates the discovery of your IP address, conveniently, as long as your LXD bridge is called "lxdbr0".

Once set in the default template, all LXD containers you start now have an apt proxy pointing to your host set up!

In a new template

Should you not want to alter the default template, you can easily create a new one:

export PROFILE_NAME=proxy
lxc profile create $PROFILE_NAME

Then substitute the newly created profile in the previous command line. It becomes:

export LXD_ADDRESS=$(ifconfig lxdbr0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
echo -e "#cloud-config\napt:\n proxy: http://$LXD_ADDRESS:8000" | lxc profile set $PROFILE_NAME user.user-data -

Launching a new container needs to add this configuration template, so that the container benefits form the proxy configuration:

lxc launch ubuntu:xenial -p $PROFILE_NAME -p default

Reverting

If for some reason you don't want to use your host as a proxy anymore, it is quite easy to revert the changes to the template:

lxc profile set <template> user.user-data

That's it!

As you can see it is trivial to set an apt proxy on LXD, and using squid-deb-proxy on the host makes that configuration trivial.

Hope this helps!

Discussion and/or comments welcome on Reddit!