Using LXD on Ubuntu 16.04
ubuntu lxc lxd- install lxd
$ sudo apt install lxd
- Enable swap accounting
$ sudo vi /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="swapaccount=1"
$ sudo update-grub
$ sudo shutdown -r now
- create lxd user
$ sudo useradd -s /bin/bash -m lxdadm
$ sudo passwd lxdadm
$ sudo adduser lxdadm lxd
- init lxd
$ sudo lxd init
- 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
- Create container without starting it
$ lxc init ubuntu:xenial ubuntu16
- List images
$ lxc image list
$ lxc image list images:
$ lxc image list ubuntu:
- List containers
$ lxc list
$ lxc list --fast
$ lxc info <container>
- Start/stop a container
$ lxc start
$ lxc stop <container>
$ lxc stop <container> --force
$ lxc restart <containter>
$ lxc restart <container> --force
$ lxc pause <container>
- Profiles
$ lxc profile list
$ lxc profile show <profile>
$ lxc profile edit <profile>
$ lxc profile apply <container> <profile1>,<profile2>,...
- Shell
$ lxc exec <container> bash
$ lxc exec <container> -- ls -lh /
$ lxc exec <container> --env mykey=myvalue
- 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>
- 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>
- Cloning/renaming/delting
$ lxc copy <source container> <destination container>
$ lxc move <old name> <new name>
$ lxc delete <container>
- 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
- Memory limit
$ lxc config set my-container limits.memory 256MB
- Disk limit (requires btrfs or ZFS)
$ lxc config device set my-container root size 20GB
- IO reading/writing limits
$ lxc config device set my-container root limits.read 20Iops
$ lxc config device set my-container root limits.write 10Iops
- Autostart container
$ lxc config set container_name boot.autostart 1
- Mount host directory
<?prettify linenums=true?>
$ lxc config device add container_name device_name disk source=host_directory path=guest_directory
- 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
- 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