使用Lxcfs对docker容器隔离
What’s LXCFS?
LXCFS is a side project of LXC and LXD. It’s basically a tiny FUSE filesystem which gets mounted in your containers and mask a number of proc files.
At present, it supports the following files:
- /proc/cpuinfo
Only returns the CPUs listed in your cpuset - /proc/diskstats
Returns I/O usage from the container - /proc/meminfo
Only shows the amount of memory and SWAP the container can use - /proc/stat
Related to cpuinfo, only lists the right CPUs - /proc/swaps
Related to meminfo, only shows your container’s swap consumption - /proc/uptime
Shows the container uptime instead of the host’s
It’s basically a userspace workaround to changes which were deemed unreasonable to do in the kernel. It makes containers feel much more like separate systems than they would without it.
On top of the proc virtualization feature, lxcfs also supports rendering a partial cgroupfs view which can then be mounted into a container on top of /sys/fs/cgroup, allowing processes in the container to interact with the cgroups in a safe way.
This part is only enabled on kernels that do not support the cgroup namespace, as newer kernels (4.6 upstream, 4.4 Ubuntu) no longer need this.
Why do I need it?
lxcfs isn’t absolutely needed to run LXC or LXD containers.
That being said, you will want it if:
- You want proper resource consumption reporting inside your container
- You need to start a systemd based container on a system running a kernel older than 4.6 upstream (or 4.4 Ubuntu)
LXD in Ubuntu actually depends on LXCFS as we think it’s a critical part of offering a good container experience on Ubuntu.
How to get it?
LXCFS is available in quite a few distributions, so chances are you can just grab it with your package manager. It may take a few days/weeks for 2.0 to be available though.
Ubuntu users have had lxcfs available for a few years now and the 2.0 release is now in the Ubuntu development release. Up to date packages for all Ubuntu releases can also be found in our PPAs.
Install fuse
1 | yum install fuse fuse-devel |
Building and running lxcfs from git
1 | git clone git://github.com/lxc/lxcfs |
note:
After the last step is started, if the report can not find the liblxcfs.so file.
1 | # ./lxcfs -s -f -o allow_other /var/lib/lxcfs/ |
execute:
1 | # cp .libs/liblxcfs.so /usr/lib/lxcfs/liblxcfs.so |
Start LXCFS in the background and detect the process state automatically pull up, the content is as follows:
1 | #!/bin/bash |
Test
create and run docker container:
1 | docker run -it --rm --privileged=true --cpuset-cpus=2-9 -m 1024m -v /var/lib/lxcfs/proc/uptime:/proc/uptime:rw -v /var/lib/lxcfs/proc/cpuinfo:/proc/cpuinfo:rw -v /var/lib/lxcfs/proc/stat:/proc/stat -v /var/lib/lxcfs/cgroup/:/cgroup/:rw -v /var/lib/lxcfs/proc/meminfo:/proc/meminfo -v /var/lib/lxcfs/proc/swaps:/proc/swaps -v /var/lib/lxcfs/proc/diskstats:/proc/diskstats -v /home/wangxigang/go/src/runtime:/root/ ubuntu:14.04 /bin/bash |
Project information
- Upstream website: https://linuxcontainers.org/lxcfs/
- Code: https://github.com/lxc/lxcfs