I use LXC on my ubuntu workstation quite often. LXD has been out for a while, and I tested it to see if I could use it as a direct replacement for LXC. And the answer is yes! LXD provides nice management tools that didn't exist in LXC, but the mechanics are the same.
This blog is a recap of what I did to setup a local installation. It assumes you already know what is LXC and how to use it.
Some differences with LXC
- No more template scripts, LXD uses pre-built images. This has become quite common (think Docker/EC2/OpenStack Glance).
- LXD runs as a daemon and can be managed remotely. If run locally any user in the lxd group can talk to the daemon. APIs are great.
- Network management is way simpler, and doesn't require tweaking configuration files.
Install and configure LXD
Ubuntu 16.04 seems to come with LXD installed, but in case it isn't there:
sudo apt install lxd
You can then use the lxd init tool to setup the initial configuration:
sudo lxd init
You will have to answer questions about:
- The storage back-end, directory or zfs. The zfs back-end is nice. It uses clones and snapshots to optimize performance when creating containers, and consumes less disk space.
- The initial network.
- The LXD API access: local only or exposed on a network.
The lxd command manages the daemon, use the lxc command to manage your containers.
Create and access containers
The containers creation is straightforward:
lxc launch ubuntu:16.04 c1
ubuntu:16.04 is the reference to an existing container image. If LXD cannot find it locally, it will download it from a repository (canonical's by default). The image will then be stored locally.
The container will be started after creation. Use the list or info subcommands to get information about the new container.
You will not be able to access the container using SSH by default:
$ ssh email@example.com Permission denied (publickey).
Just like for ubuntu cloud instances the default user doesn't have a password set, and you need to use an SSH key to authenticate. An initial setup needs to be done. Not handy but should only be done once.
To configure your SSH key inside the container use the exec subcommand:
$ lxc exec c1 /bin/bash root@c1:~# echo "YOU PUBLIC KEY" > /home/ubuntu/.ssh/authorized_keys root@c1:~# exit exit
Validate that you can access the container:
$ ssh firstname.lastname@example.org ... ubuntu@c1:~$
Now you can build a new image that contains you SSH key:
$ lxc stop c1 $ lxc publish c1 --alias ubuntu-ssh $ lxc image list | grep ubuntu-ssh $ lxc launch ubuntu-ssh c2