LXD/LXC on LVM Thin Provisioned (thinpool)

Install APT Packages

We'll need to install some packages if they aren't already, including bridge utils and xfs for filesystem.

Ubuntu 16.04 comes with LXD 2.0, with 2.14 being current stable (as of 2017-06-04). We'll add the ppm to pull in this latest version.

add-apt-repository ppa:ubuntu-lxc/lxd-stable  
 This PPA contains the latest stable release of LXD as well as the latest stable version of any of its dependencies.
 More info: https://launchpad.net/~ubuntu-lxc/+archive/ubuntu/lxd-stable
Press [ENTER] to continue or ctrl-c to cancel adding it

gpg: keyring `/tmp/tmpdgtzyk2z/secring.gpg' created  
gpg: keyring `/tmp/tmpdgtzyk2z/pubring.gpg' created  
gpg: requesting key 7635B973 from hkp server keyserver.ubuntu.com  
gpg: /tmp/tmpdgtzyk2z/trustdb.gpg: trustdb created  
gpg: key 7635B973: public key "Launchpad PPA for Ubuntu LXC team" imported  
gpg: Total number processed: 1  
gpg:               imported: 1  (RSA: 1)  
OK  

Next, do the standard apt-get update and dist-upgrade.

apt-get update && apt-get dist-upgrade -y  
apt-get install -y ifenslave inetutils-traceroute lxd lxd-tools lxd-client bridge-utils criu xfsprogs xfsdump  

Configure LXC Storage for LVM

In another blog post, I walk through configuring LVM with a thinpool. The commands below are configuring LXC to use this thinpool.

lxc storage set vg1 lvm.vg_name  
lxc storage set vg1 lvm.thinpool_name tp1  
lxc storage set vg1 volume.block.filesystem xfs  
lxc storage set vg1 volume.size 5GB  

Now we can view the storage configuration for vg1.

lxc storage show vg1  
description: ""  
config:  
  lvm.thinpool_name: tp1
  lvm.vg_name: vg1
  source: vg1
  volume.block.filesystem: xfs
  volume.size: 15GB
name: vg1  
driver: lvm  
used_by:  
- /1.0/containers/test1
- /1.0/images/2373b6551aca33a1dca54b92b99083064c95dee9c5a845ea02b9be5e62a271a7
- /1.0/profiles/default

Configure Bridge

Instead of isolated networks (type 2, default), I use host bridged.

Edit /etc/network/interfaces to look like below, replace eno1 with the name of your ethernet device name.

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo  
iface lo inet loopback

# The primary network interface
auto eno1  
iface eno1 inet manual

auto br0  
iface br0 inet dhcp  
    bridge-ifaces eno1
    bridge-ports eno1
    bridge_stp off
    bridge_fd 0
    bridge_maxwait 0

Configure LXC Networking

After updating the network configuration and rebooting, the server should be configured with a bridge (which gets its IP via DHCP). Running the commands below configure LXC to use this bridge as the default.

lxc profile device set default eth0 nictype bridged  
lxc profile device set default eth0 parent br0  

Configure sysctl settings

Create a new file /etc/sysctl.d/98-lxd.conf with the following contents:

fs.inotify.max_queued_events = 1048576  
fs.inotify.max_user_instances = 1048576  
fs.inotify.max_user_watches = 1048576  
vm.max_map_count = 262144  
kernel.dmesg_restrict = 1  
vm.nr_hugepages = 256  
net.core.netdev_max_backlog = 182757  
net.ipv4.tcp_mem = 182757 243679 365514  

Create hugepages LXC profile

lxc profile create hugepages  
lxc profile edit hugepages <<__END__  
name: hugepages  
config:  
  raw.lxc: |
    lxc.mount.entry = hugetlbfs dev/hugepages hugetlbfs rw,relatime,create=dir 0 0
  security.privileged: "true"
__END__  

Configure limits

Create a new file /etc/security/limits.d/lxd.conf with the following contents:

* soft nofile 1048576
* hard nofile 1048576
root soft nofile 1048576  
root hard nofile 1048576  
* soft memlock unlimited
* hard memlock unlimited

Download Ubuntu 16.04

lxc image copy images:ubuntu/xenial local: --alias=ubuntu-16.04  

Test out creation of a new container by launching a test container

lxc launch ubuntu-16.04 test1  

Configure Ubuntu fan networking

apt-get install ubuntu-fan