Skip to content

Installing and configuring LXD

Syed Sayem edited this page May 1, 2019 · 2 revisions

Table of contents

 

Installation

By default, LXD is already installed on Ubuntu 18.04. If not, you can install it by running the following command:

sudo snap install lxd

 

Add your user to the LXD group

To allow your user to access the LXD daemon locally, it must be part of the lxd group. Run the following command to add your username to lxd group:

sudo adduser <USER> lxd

where is your username, mine is sayem

The new group will be effective at the next login session, to apply it to the current shell, run the following command:

newgrp lxd

 

Install ZFS tools

For this tutorial, we'll set up LXD using the ZFS storage backend.

The ZFS filesystem provides copy-on-write functionality and allows using advanced LXD features, like per-container disk quotas, immediate snapshot/restore, optimized migration (send/receive) and instant container creation from an image.

To install ZFS tools, just run the following command:

sudo apt install zfsutils-linux

 

top

 

Configuration

Start the LXD initialization process with the lxd init command:

sudo lxd init

You'll be asked a series of questions on how to configure the daemon. Accept the default value (by pressing Enter).

Would you like to use LXD clustering? (yes/no) [default=no]: 
Do you want to configure a new storage pool? (yes/no) [default=yes]: 
Name of the new storage pool [default=default]: 
Name of the storage backend to use (dir, zfs) [default=zfs]: dir
Create a new ZFS pool? (yes/no) [default=yes]: 
Would you like to use an existing block device? (yes/no) [default=no]: 
Size in GB of the new loop device (1GB minimum) [default=27GB]: 
Would you like to connect to a MAAS server? (yes/no) [default=no]: 
Would you like to create a new local network bridge? (yes/no) [default=yes]: 
What should the new bridge be called? [default=lxdbr0]: 
What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: 
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: 
Would you like LXD to be available over the network? (yes/no) [default=no]: 
Would you like stale cached images to be updated automatically? (yes/no) [default=yes] 
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]: yes

You have successfully configured LXD, you can verify client can connect to the daemon, by running the following command:

lxc list
lxc info | more

The output should be similar to

+------+-------+------+------+------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+------+-------+------+------+------+-----------+

which shows there are no running containers.

 

It's now time to configure profile.

 

top

 

Profile

To get a list of profiles, run the following command:

lxc profile list

+---------+---------+
|  NAME   | USED BY |
+---------+---------+
| default | 0       |
+---------+---------+

The default configuration can be edited with lxc profile edit default profile.

lxc profile edit default profile
config: {}
description: Default LXD profile
devices:
  eth0:
    name: eth0
    nictype: bridged
    parent: lxdbr0
    type: nic
  root:
    path: /
    pool: default
    type: disk
name: default
used_by: []

Replace with this configuration

config: 
  limits.cpu: "2"
  limits.memory: 4GB
  limits.memory.swap: "false"
  linux.kernel_modules: ip_tables,ip6_tables,netlink_diag,nf_nat,overlay
  raw.lxc: "lxc.apparmor.profile=unconfined\nlxc.cap.drop= \nlxc.cgroup.devices.allow=a\nlxc.mount.auto=proc:rw
    sys:rw"
  security.privileged: "true"
  security.nesting: "true"
description: Default LXD profile
devices:
  eth0:
    name: eth0
    nictype: bridged
    parent: lxdbr0
    type: nic
  root:
    path: /
    pool: default
    type: disk
name: default
used_by: []

 

top

 

Getting started

To list all available LXD images, run the following command:

lxc image list images:
lxc image list images: | grep -i centos
lxc image list images: | grep -i ubuntu
lxc image list images: | grep -i debian

To create and start containers, run the following command:

lxc launch ubuntu:18.04 ubuntu

The output will be something like this:

Creating ubuntu
Retrieving image: rootfs: 19% (5.18MB/s) 

It's downloading the official Ubuntu 18.04 LTS image...

 

You can use the following commands to list of all the existing containers:

$ lxc list --fast
$ lxc list | grep RUNNING
$ lxc list | grep STOPPED
$ lxc list | grep
$ lxc list "*c1*"
$ lxc list "*c2*"
$ lxc list

The output will be something like this:

+--------+---------+-----------------------+----------------------------------------------+------------+-----------+
|  NAME  |  STATE  |         IPV4          |                     IPV6                     |    TYPE    | SNAPSHOTS |
+--------+---------+-----------------------+----------------------------------------------+------------+-----------+
| ubuntu | RUNNING | 10.115.197.167 (eth0) | fd42:bd6:68ea:3bd1:216:3eff:fe06:4a83 (eth0) | PERSISTENT | 0         |
+--------+---------+-----------------------+----------------------------------------------+------------+-----------+

 

Execute command in a container

To run or execute commands in containers use exec command:

lxc exec containerName -- command
lxc exec containerName -- /path/to/script
lxc exec containerName --env EDITOR=/usr/bin/vim -- command
### run date, ip a, ip rm and other commands on various containers ###
$ lxc exec cenots-c2 -- date
$ lxc exec cenots-c2 -- ip a
$ lxc exec ubuntu-nginx-c3 -- ip r
$ lxc exec fedora28-c7-- dnf update
$ lxc exec debian9-c4 -- cat /etc/debian_version

 

Access to container

To gain login and gain shell access in a container named file-server , enter:

lxc exec ubuntu bash

 

Stop containers

lxc stop ubuntu

 

Delete containers

The command is (be careful as the LXD containers are deleted immediately without any confirmation prompt i.e. keep backups):

lxc delete ubuntu

You may get the following error while deleting the container:

The container is currently running, stop it first or pass –force.

To fix this:

 lxc stop ubuntu && lxc delete

 

Information

lxc info
lxc info <containerName>
lxc info ubuntu

 

All containers (or their rootfs) are stored under the /var/lib/lxd/containers directory, and images are stored under the /var/lib/lxd/images directory.

 

top