5. LXC
• LXC (LinuX Containers)
– Run a Linux system within another Linux system
• Container
– a group of processes on a Linux box, put together
in an isolated environment
• Inside the box, it looks like a VM
• Outside the box, it looks like normal processes
5
6. Benefit
• Speed - fast
– Boots, create VM, deploy tasks
• Footprint - small
– aufs or overlayfs
• Virtualization
– Own network interface
– Own filesystem
– Isolation and security
– Isolation and resource usage
6
7. Use Cases
• Continuous Integration
– Run 100 tests in 100 VMs
• Escape dependency hell
• Do whatever you did in VMs
– But faster
7
10. Cheat Sheet
• lxc-create - create system container
• lxc-destroy - destroy container
• lxc-start - start sys container
• lxc-stop - stop sys container
• lxc-shutdown - safely shut down a container
• lxc-execute - Run command in a app
container
• lxc-start-ephemeral - start an one-time
container
• lxc-ls - shorter output than lxc-list
• lxc-list - List all containers
• lxc-info - Print info on the state of a
container
• lxc-monitor - Monitor state
• lxc-wait - Wait for a state change
• lxc-restore - restore containers from
backups made by lxc-backup
• lxc-backup - back up the root filesystems
• lxc-freeze - freeze a running container
• lxc-unfreeze - unfreeze a frozen container
• lxc-cgroup - View and set container control
group settings
• lxc-netstat - Execute netstat in a running
container
• lxc-ps - View process info in a running
container
10
11. create x destroy
• lxc-create -n {ctx-name} -t {template name}
– $ sudo lxc-create -n u1 -t ubuntu
– $ sudo lxc-create -n u2 -t ubuntu -- -r raring
– Templates are in /usr/lib/lxc/lxc-*
– When first created, a base filesystem will put in
/var/cache/lxc/
– Then copy a instance to /var/lib/lxc/{name}/
• config
• fstab
• rootfs/
• lxc-destroy -n {name}
11
12. start x stop x shutdown
• lxc-start -n {name} [-d] [-o logfile] [--logpriority=LEVEL]
– Start a system-level container (/sbin/init)
• lxc-shutdown -n name [-w] [-r] [-t timeout]
– Cleanly shut down a container.
• Send SIGPWR
• If not stopped, call lxc-stop which sends SIGKILL
– -w: wait for shutdown to complete.
– -r: reboot (ignore -w).
– -t timeout: wait at most timeout seconds (implies -w), then
kill the container.
• lxc-stop -n {name}
12
13. execute x start-ephermal
• lxc-execute -n {NAME} -- {COMMAND}
– Run a command in application-level container
• lxc-start-ephemeral [-d] [-u user] [-S key] -o {orig} --
[COMMAND]
– Runs an ephemeral (one-off) container
– $ sudo lxc-start-ephemeral -u ubuntu -o u1 -- uname -a
– Options:
• orig - name of the original container
• user - the user to connect to the container as
• key - the path to the SSH key to use to connect
• -d - run in the background
13
14. cgroup
• lxc-cgroup -n {name} {subsystem} {value}
– View and set container control group settings
– $ sudo lxc-cgroup -n u1 memroy.limit_in_bytes
256M
– $ lxc-cgroup -n u1 cpu.shares 512
• maximum is 1024
– $ lxc-cgroup -n u1 cpuset.cpus 0,3
– Configure - /var/lib/lxc/{name}/config, such as
• lxc.cgroup.memory.limit_in_bytes = 256M
14
19. 19
Dockerfile allow you to automate the steps you
would normally manually take to create an image.
20. Juju
Automate your cloud infrastructure
Configure, manage, maintain, deploy and scale efficiently with
best-practice Charms on any public, private or hybrid cloud from
a powerful GUI or the command-line.
20
32. Network
• Each container has its own interface, routing table, iptables
rules…
• Communication between containers via pairs of veth
interface
• /etc/init/lxc-net.conf: iptables, dnsmasq…
32
[1] $ sudo unshare --net bash
[2] $ echo $$
[1] $ sudo ip link add name lxcbr0 type veth peer name vethdoro
[1] $ ip link set vethdoro netns <PID>
[2] $ ip link set vethdoro name eth0
[2] $ ifconfig eth0 192.168.1.2
[2] $ ifconfig lo 127.0.0.1
[1] $ ifconfig addif vethdoro
33. Mount
• Deluxe chroot() pivot_root()
• Filesystems mounted in a mnt namespace are
visible only in this namespace
• You need to remount special filesystem
– procfs
– devpts
• Commands
– unshare --mount <program>
– mount {--make-[r]shared | --make-[r]slave | -- make --
[r]private | --make-unbindable} <mount-object>
33
http://www.ibm.com/developerworks/linux/library/l-mount-namespaces/index.html
34. cgroup
• Everything exposed through filesystem
– cgroup on /sys/fs/cgroup type tmpfs (rw,relatime,mode=755)
– cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,relatime,cpuset)
– cgroup on /sys/fs/cgroup/cpu type cgroup (rw,relatime,cpu)
– cgroup on /sys/fs/cgroup/cpuacct type cgroup (rw,relatime,cpuacct)
• Create a cgroup
– mkdir -p /sys/fs/cgroup/cpu/lxc/u1
– Add PID to cgroup: echo $PID > /sys/fs/cgroup/cpu/lxc/u1/tasks
– Limit: echo 512 > /sys/fs/cgroup/cpu/lxc/u1/cpu.shares
34