LXC/LXD

June 21, 2020 - Reading time: 4 minutes

Released by IBM in 2008, LXC (lexy) combines chroot, kernel namespaces, SELinux policies and apparmor to provide a containerized environment.

From the official website: "Our main focus is system containers. That is, containers which offer an environment as close as possible as the one you'd get from a VM but without the overhead that comes with running a separate kernel and simulating all the hardware."

The main components are lxd and lxd-client.

  • lxd init #sets a storage pool, gives the option to configure a cluster, set a bridge.
  • lxc list # shows status of lxd
  • lxc launch [name] # images are pulled from http://images.linuxcontainers.org/
  • lxc exec -- [command] # runs a command inside a container
  • lxc remote lists # lists all the source list.
  • lxc image list # list the downloaded images

Examples

# Installation 
cloud_user@ip-10-0-1-50:~$  sudo apt-get install lxd lxd-client

# Initialization 
cloud_user@ip-10-0-1-50:~$ sudo lxd init
Do you want to configure a new storage pool (yes/no) [default=yes]?
Name of the storage backend to use (dir or zfs) [default=dir]:
Would you like LXD to be available over the network (yes/no) [default=no]?
Do you want to configure the LXD bridge (yes/no) [default=yes]?
Warning: Stopping lxd.service, but it can still be activated by:
  lxd.socket
LXD has been successfully configured.
cloud_user@ip-10-0-1-50:~$

# New lxdbr0 bridge with random subnet was created 
cloud_user@ip-10-0-1-50:~$ ip -4 addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc pfifo_fast state UP group default qlen 1000
    inet 10.0.1.50/24 brd 10.0.1.255 scope global eth0
       valid_lft forever preferred_lft forever
5: lxdbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 10.218.29.1/24 scope global lxdbr0
       valid_lft forever preferred_lft forever

# Sources for images
cloud_user@ip-10-0-1-50:~$ sudo lxc remote list
+-----------------+------------------------------------------+---------------+--------+--------+
|      NAME       |                   URL                    |   PROTOCOL    | PUBLIC | STATIC |
+-----------------+------------------------------------------+---------------+--------+--------+
| images          | https://images.linuxcontainers.org       | simplestreams | YES    | NO     |
+-----------------+------------------------------------------+---------------+--------+--------+
| local (default) | unix://                                  | lxd           | NO     | YES    |
+-----------------+------------------------------------------+---------------+--------+--------+
| ubuntu          | https://cloud-images.ubuntu.com/releases | simplestreams | YES    | YES    |
+-----------------+------------------------------------------+---------------+--------+--------+
| ubuntu-daily    | https://cloud-images.ubuntu.com/daily    | simplestreams | YES    | YES    |
+-----------------+------------------------------------------+---------------+--------+--------+

# List of available images on one remote 
cloud_user@ip-10-0-1-50:~$ sudo lxc image list ubuntu-daily: | head
+--------------------+--------------+--------+-----------------------------------------------+---------+----------+-------------------------------+
|       ALIAS        | FINGERPRINT  | PUBLIC |                  DESCRIPTION                  |  ARCH   |   SIZE   |          UPLOAD DATE          |
+--------------------+--------------+--------+-----------------------------------------------+---------+----------+-------------------------------+
| arm64 (5 more)     | 65ac61467b6d | yes    | ubuntu 18.04 LTS arm64 (daily) (20200618)     | aarch64 | 166.42MB | Jun 18, 2020 at 12:00am (UTC) |
+--------------------+--------------+--------+-----------------------------------------------+---------+----------+-------------------------------+
| armhf (5 more)     | ca3e71f57c99 | yes    | ubuntu 18.04 LTS armhf (daily) (20200618)     | armv7l  | 164.92MB | Jun 18, 2020 at 12:00am (UTC) |
+--------------------+--------------+--------+-----------------------------------------------+---------+----------+-------------------------------+
| b (11 more)        | 1be5b44a55b7 | yes    | ubuntu 18.04 LTS amd64 (daily) (20200618)     | x86_64  | 179.28MB | Jun 18, 2020 at 12:00am (UTC) |
+--------------------+--------------+--------+-----------------------------------------------+---------+----------+-------------------------------+
| e (5 more)         | 648aef59e416 | yes    | ubuntu 19.10 amd64 (daily) (20200611)         | x86_64  | 341.62MB | Jun 11, 2020 at 12:00am (UTC) |

# Launch a new image
cloud_user@ip-10-0-1-50:~$ sudo lxc launch images:60ee6bef5d7d my-alpine
Creating my-alpine
Starting my-alpine

# List status 
cloud_user@ip-10-0-1-50:~$ sudo lxc image list
+-------+--------------+--------+------------------------------------+--------+--------+------------------------------+
| ALIAS | FINGERPRINT  | PUBLIC |            DESCRIPTION             |  ARCH  |  SIZE  |         UPLOAD DATE          |
+-------+--------------+--------+------------------------------------+--------+--------+------------------------------+
|       | 60ee6bef5d7d | no     | Alpine 3.10 amd64 (20200621_13:00) | x86_64 | 2.40MB | Jun 21, 2020 at 6:03pm (UTC) |
+-------+--------------+--------+------------------------------------+--------+--------+------------------------------+
cloud_user@ip-10-0-1-50:~$ sudo lxc list
+-----------+---------+----------------------+------+------------+-----------+
|   NAME    |  STATE  |         IPV4         | IPV6 |    TYPE    | SNAPSHOTS |
+-----------+---------+----------------------+------+------------+-----------+
| my-alpine | RUNNING | 10.218.29.137 (eth0) |      | PERSISTENT | 0         |
+-----------+---------+----------------------+------+------------+-----------+

# Connect to the alpine image, and list interfaces
cloud_user@ip-10-0-1-50:~$ sudo lxc exec my-alpine -- /bin/ash
~ # ip -4 addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP qlen 1000
    inet 10.218.29.137/24 brd 10.218.29.255 scope global eth0
       valid_lft forever preferred_lft forever
~ #