1. Home
  2. Linux
  3. Ubuntu
  4. Setting up LXD on Ubuntu 16.04

Setting up LXD on Ubuntu 16.04

Setting up LXD on Ubuntu 16.04
Setting up LXD on Ubuntu 16.04

Introduction

LXD is a container hypervisor which allows a ReST API to manage LXC containers. It also grants the ability to run containers and manage related resources, such as storage volumes and networks.
In this tutorial we will demonstrate how to install and setup LXD on Ubuntu 16.04 (Xenial Xerus) as well as how to launch a container and access it.

Requirements

For this tutorial you will need a Ubuntu 16.04 installation (Desktop or Server), with internet access. This could either be physical or virtual.

Installing LXD

If you’re running an Ubuntu 16.04 server installation, LXD should be preinstalled on your system.
On Desktop installs, it will have to be installed manually; Open a terminal and execute the following commands to install LXD.

sudo apt update

Then use the following.,

sudo apt install lxd

Adding your user to the LXD group
In order to allow your user to access the LXD daemon locally, it will need to be a part of the lxd group.
To do that, execute the following below.

sudo adduser <USER> lxd

Instead of ‘<USER>’, put your own username.
The new group should be in effect on the next time you log in. For it to apply to the current shell, execute the following command.

newgrp lxd

Install ZFS tools

In this tutorial, we are going to setup LXD with the ZFS storage backend.
The ZFS filesystem will provide a copy-on-write function which also allows advanced LXD features such as per-container disk quotas, instant snapshot/restore, optimized migration (send/receive), along with immediate container creation from an image.
Execute the following command to install ZFS tools.

sudo apt install zfsutils-linux

Setting up LXD

You may now continue to setup the LXD daemon.
To do this, run the below.

sudo lxd init

This will prompt a series of questions on how you would like your daemon configured.
You can use the default answer by pressing ‘Enter’ for a lot of them. You can pick the size of the loop device for the ZFS pool depending on your disk space available.
As a part of the LXD initial setup, networking can also be setup; this means that any container created can be accessed from the host.
Choose ‘Yes’ at the first question, to make a network bridge.
Next, accept the default name for the bridge ‘lxdbr0’.
Then, setup IPv4 networking for the bridge by choosing ‘Yes’.
Afterwards, it will prompt you to setup an iPv4 subnet, choose ‘Yes’ again.
It will now ask for a couple of details on how you would like the iPv4 network configured, specifically the following ones:

  • address
  • CIDR
  • start/end address for DHCP
  • maximum number of DHCP clients

You can use the defaults for these questions as well, if you wish.
Lastly, answer ‘Yes’ to the question about NAT on the network
There’s a similar configuration available for iPv6 networks, however you can choose to decide not to set up iPv6
Then LXD will finally be setup and ready to be used.

Launching a Container

We may now start using LXD.
To begin, ensure the client is able to connect to the daemon by executing the following.

lxc list

The output you should receive is going to look similarly to the one below.
‘Generating a client certificate. This may take a minute…’
If this is your first time using LXD, you should also run the following.

sudo lxd init

To start your first container, try the below.

lxc launch ubuntu:16.04
+------+-------+------+------+------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+------+-------+------+------+------+-----------+

This is going to show that there are no containers running, so let’s launch your first container.

lxc launch ubuntu:16.04

The output you should receive will look like this one below.

Creating stirring-beagle
Starting stirring-beagle

This should now download the official Ubuntu 16.04 LTS (Xenial Xerus) image and launch a container with it.
In case a container name has not been given (such as in this case), a random one is going to be generated.
Confirm that your container is actually running, using ‘lxc list’.

+-----------------+---------+-----------------------+------+------------+-----------+
|      NAME       |  STATE  |         IPV4          | IPV6 |    TYPE    | SNAPSHOTS |
+-----------------+---------+-----------------------+------+------------+-----------+
| stirring-beagle | RUNNING | 10.147.177.171 (eth0) |      | PERSISTENT | 0         |
+-----------------+---------+-----------------------+------+------------+-----------+

You can run commands in the container with ‘lxc exec’.

lxc exec stirring-beagle -- ls -la

This should run the provided command as root on the target container.

total 7
drwx------  3 root root    5 Jun 29 11:29 .
drwxr-xr-x 22 root root   22 Jun 19 23:52 ..
-rw-r--r--  1 root root 3106 Oct 22  2015 .bashrc
-rw-r--r--  1 root root  148 Aug 17  2015 .profile
drwx------  2 root root    3 Jun 29 11:29 .ssh

Shell access to the container may be obtained using the below.

lxc exec stirring-beagle /bin/bash

Note that because you have setup networking, the container will have an iPv4 address (like the one we’ve shown with the lxc list command) and it could also be reached using SSH from the host. In this case though, you will need to import an ssh key in the container first.
After the container is no longer required, you can stop it using the following.

lxc stop stirring-beagle

Destroyed with.

lxc delete stirring-beagle

Conclusion

Your machine will now be set up to run LXD containers.
LXD allows a lot of features and provides great flexibility when configuring containers as well as limiting container resources and access.

Updated on December 23, 2018

Was this article helpful?

Related Articles

Leave a Comment

[apsl-login-lite login_text='Please login with a social account']