CONTAINERISATION: EXPLORING LXC/LXD CONTAINERS
Today, whenever we think of containerisation, we almost always think first about Docker. LXC/LXD often times, comes second. Perhaps because it is Linux-based while docker on the other hand is cross-platform and distro-agnostic. LXC/LXD containers does beat docker in certain areas though. In terms of efficiency and performance, it tends to have a lesser overhead and provides an almost native performance at the operating system level, sharing the host’s kernel. In addition, Docker adds an extra layer which can introduce some performance overhead.
LXC container is an operating system level virtualisation solution. It allows you run multiple isolated Linux systems on a single Linux host. It allows one to use the Linux kernel underneath the host operating system
LXD container is a higher-level container management system built on top of LXC. It is user-friendly and also has an interface that is feature-rich. All these make it easier to manage and orchestrate.
LXC/LXD containers can be used to host a website, create a web cluster and even implement a backup solution.
How to Serve a Website from a LXD Container
Serving a website from a LXD container involves a series of steps. In this example, the LXD container would be based off an ubuntu image. Nginx server would also be installed inside the LXD container in order to access it using a public i.p. address via a web browser. The steps below describe how to host and serve a website from a LXD container.
- Install LXD on the Ubuntu terminal using the command below
sudo snap install lxd –channel=3.0/stable
2. Check the LXD version : lxc -–version
3. Add the user called cloud_user to the lxd group using the command
sudo usermod cloud_user -aG lxd
4.Initialise LXD in order for the LXD container to be placed in a storage pool and for it to have network access. Initialise LXD container using the command below:
lxd init
5.Launch a LXC/LXD container off an ubuntu-based image using the lxc launch command below:
lxc launch images:ubuntu/20.04 myWeb-container
6. Pull down the website folder from GitHub into the Ubuntu terminal
7.Access the container using the lxc exec command to update the container and also install nginx using the command below
lxc exec myWeb-container — /bin/bash
8.Update and upgrade the ubuntu container using the following commands respectively
apt update
apt upgrade
9.Push the myWeb folder on the Ubuntu terminal home directory to www folder which is located in the ubuntu-based container called myWeb-container using the lxc file push command
lxc file push myWeb/ myWeb-container/var/www/ — create-dirs -r
10.Access the container and install nginx while inside the container using the apt install nginx command
Get Lukmon Adeokun’s stories in your inbox
Join Medium for free to get updates from this writer.
apt install nginx
11. 1. Nginx has a config file for a default server block. You need to edit this file to determine what would be served by nginx. This file is located in /etc/nginx/sites-available. Edit this file by using the command
vi /etc/nginx/sites-available/default
12.Add the following commands inside the ‘default’ file
root /var/www/myWeb/;
index index.html;
13. Install Uncomplicated firewall(ufw) and configure it. Ufw is used to configure firewall rules on the lxd container which in turn helps to control the incoming and outgoing traffic to and from the container. Install ufw using the commands below
sudo apt install ufw
sudo systemctl start ufw
sudo systemctl enable ufw
sudo ufw allow 80/tcp
sudo ufw enable
sudo ufw status
systemctl status nginx
14.Find out the private ip address of the container using the ‘lxc list’ command
lxc list
15. 1. Expose the web service running inside the container to the outside world. This allows incoming HTTP requests to be directed to the ubuntu container(myWeb-container), where Nginx web server can handle the requests. Run the command below to do this
lxc config device add myWeb-container myDevice proxy listen=tcp:0.0.0.0:80 connect=tcp:10.129.147.192:80
You need to add a proxy device to an LXC container in order to route traffic from a specific port (in this case, port 80) on the host to a port on the container. Here’s an explanation of the command:
· `lxc config device add myWeb-container myDevice proxy`: This part of the command instructs LXC to add a proxy device named “myDevice” to the container named “myWeb-container.”
· `listen=tcp:0.0.0.0:80`: This specifies that the proxy device should listen on all available network interfaces (`0.0.0.0`) on port 80 of the host. It means that incoming traffic on port 80 of the host will be redirected to the container.
· `connect=tcp:10.129.147.192:80`: This part indicates that the traffic received on the host’s port 80 should be connected (forwarded) to the container’s IP address 10.129.147.192 on port 80. In other words, it sets up port forwarding from the host to the ubuntu container.
· Make sure that the IP address and port numbers in the `connect` option correspond to the actual IP address and port where your web server or application is listening inside the container. Additionally, ensure that the firewall on the host is properly configured to allow incoming traffic on port 80.
CONCLUSION
LXC/LXD containerisation is a technology that finds great use in today’s computing landscape. Asides web hosting, it also finds great use in microservices architecture,Database management, IoT and edge computing









