Using LXD on Ubuntu 16.04

  1. install lxd
$ sudo apt install lxd
  1. Enable swap accounting
$ sudo vi /etc/default/grub

GRUB_CMDLINE_LINUX_DEFAULT="swapaccount=1"

$ sudo update-grub

$ sudo shutdown -r now

  1. create lxd user
$ sudo useradd -s /bin/bash -m lxdadm

$ sudo passwd lxdadm

$ sudo adduser lxdadm lxd
  1. init lxd
$ sudo lxd init
  1. Creating the first container
$ sudo su - lxdadm

$ newgrp lxd

// create container ubuntu16

$ lxc launch ubuntu:xenial ubuntu16

// list lxc containers

$ lxc list

$ lxc info ubuntu16

// open a shell in ubuntu16

$ lxc exec ubuntu16 bash

  1. Create container without starting it
$ lxc init ubuntu:xenial ubuntu16
  1. List images
$ lxc image list

$ lxc image list images:

$ lxc image list ubuntu:

  1. List containers
$ lxc list

$ lxc list --fast

$ lxc info <container>

  1. Start/stop a container
$ lxc start 

$ lxc stop <container>

$ lxc stop  <container> --force

$ lxc restart <containter>

$ lxc restart <container> --force

$ lxc pause <container>

  1. Profiles
$ lxc profile list

$ lxc profile show <profile>

$ lxc profile edit <profile>

$ lxc profile apply <container> <profile1>,<profile2>,...

  1. Shell
$ lxc exec <container> bash

$ lxc exec <container> -- ls -lh /

$ lxc exec <container> --env mykey=myvalue

  1. Files
$ lxc file pull <container>/<path> <dest>

$ lxc file pull <container>/<path> - //read file to standard output

$ lxc file push <source> <container>/<path>

$ lxc file edit <container>/<path>

  1. Snapshot
  $ lxc snapshot <container>

  $ lxc snapshot <container> <snapshot name>

  $ lxc info <container> //see snapshot

  $ lxc restore <container> <snapshot name>

  $ lxc move <container>/<snapshot name> <container>/<new snapshot name>

  $ lxc delete <container>/<snapshot name>

  1. Cloning/renaming/delting
  $ lxc copy <source container> <destination container> 

  $ lxc move <old name> <new name>  

  $ lxc delete <container>

  1. CPU limit
  $ lxc config set my-container limits.cpu 2  //any 2 cpus

  $ lxc config set my-container limits.cpu 1,3 // cpu #2 #4

  $ lxc config set my-container limits.cpu 0-3,7-11

  $ lxc config set my-container limits.cpu.allowance 10% // limit time 10% of total

  $ lxc config set my-container limits.cpu.priority 0
  1. Memory limit
  $ lxc config set my-container limits.memory 256MB
  1. Disk limit (requires btrfs or ZFS)
  $ lxc config device set my-container root size 20GB
  1. IO reading/writing limits
  $ lxc config device set my-container root limits.read 20Iops

  $ lxc config device set my-container root limits.write 10Iops
  1. Autostart container
  $ lxc config set container_name boot.autostart 1
  1. Mount host directory

<?prettify linenums=true?>

  $ lxc config device add container_name device_name disk source=host_directory path=guest_directory
  1. Assign static IP (Lxd 3.0)
      $ lxc stop my-c1
      $ network attach lxdbr0 my-c1 eth0 eth0
      $ lxc config device set my-c1 ipv4.address 10.204.140.10
      $ lxc start my-c1
    
  2. IP Port forwarding (lxd 3.0)
      # Forward host port 8080 to container c1 port 80
      $ lxc config device add my-ssh-proxy ci proxy listen=tcp:0.0.0.0:8080 connect=tcp:127.0.0.1:80
    
    • connect to is container localhost ip.
Written on November 17, 2016