Containerising your environment with LXC ...

Containerising your environment with LXC/LXD

Mar 03, 2023

Back in the day, if you've ever needed to segregate your server and application environment, you'd have the choice of using multiple physical servers which technically worked, but it was quite a pain to set up all those servers what with the operating systems and networking configuration.

Then came along the virtual machine era which revolutionised our work - we could now have multiple machines all running on a single server, also known as a node in the virtual world.

The new problem was that you still needed more than one server if you wanted to run the same setup or software with slightly different parameters (multiple database instances, for example).

Today, we've got kubernetes and docker amongst others. Both great choices for compartmentalising your setups. However, I still needed a system which allowed me to host completely isolated operating systems with their respective software and services. Enter LXC/LXD. 

LXC is similar to a virtual machine in the sense of its isolation technique, however, it goes about segregating the environment a little differently. For one, it has a lighter footprint compared to its VM brother. Secondly, all lxc containers utilise their parent host's resources - they do not have their own virtual hardware as in the case of a VM. In other words, if you've got 10 containers running on a VM or physical machine, all the containers will have the same memory and CPUs. Even the disk space is shared. Not to be confused with file system in use. The host will place each container's root filesystem in either a single, large image containing all grouped disks or inside individual directories for each container. We shall see how to define storage for lxc in this article.

Installing lxc on your distro is quite straightforward - usually run your package manager's install option, e.g apt install lxc, zypper in lxc etc. I won't cover the installation process in this article.

Once installed, make sure the daemon is running with systemctl status lxd and enable it.

You now should be able to list your containers with 

lxc ls

You most probably won't have any at this point.

There are a couple of options available to start a container. I usually just import an existing distro's image from the image repository.

Or via cli:

$ lxc image list images: | less

To launch and install openSUSE, for examply, simply run

$ lxc launch images:opensuse/15.4/amd64 opensuse-15-4

Follow the pattern if you need to launch another distro. Once you need to get into the container, run

$ lxc exec image bash

Now, onto storage. To create a storage pool with lxc, run the following

$ lxc storage create pool1 dir

This is the directory-based storage we spoke about earlier. This means that all your containers' disks will live within the dir as specified. To list your storages, run

$ lxc storage ls

And for a more detailed description of your storage, including which containers are apart of that storage group, run

$ lxc storage info storage-name

If you need to move a container to a new storage pool, as I have many times, run the following:

$ lxc stop container_name
$ lxc move container_name temp_container_name -s new_storage_pool
$ lxc move temp_container_name container_name
$ lxc start container_name

Onto some networking. LXC utilises, by default, both IPv4 and IPv6 on a DHCP network. I find the IPv6 config a little annoying as it tends to allocate multiple v6 addresses to each container making the list look very messy. You can disable v6 completely with

$ lxc network set lxdbr0 ipv6.address none

In order to set static addresses on your containers, you can usually set their respective address within the container itself as with any other physical or virtual machine.

That's all for today. I hope you learnt and enjoyed this article. I will be ammending in the near future as I know I've missed many features of lxc.

Until next time.

Enjoy this post?

Buy Alexis Panopoulos a coffee

More from Alexis Panopoulos