使用lxcfs进行容器资源隔离
简介
通常在容器内执行一些系统监控命令(如top,free等),看到的并不是容器本身所分配的资源情况,而是容器所在节点,整体的资源情况,这是由于,容器并没有做对资源是图的隔离。这会让我们在容器内查询当前容器的真实用量带来困难,只能借助于监控来实现。
而LXCFS的出现,就是为了解决这个问题,通过proc的屏蔽,使容器看起来更像是个真正的系统。
以下是摘录自官方的简介
LXCFS is a small FUSE filesystem written with the intention of making Linux containers feel more like a virtual machine. It started as a side-project of
LXC
but is useable by any runtime.参考链接:https://github.com/lxc/lxcfs
LXCFS is a simple userspace filesystem designed to work around some current limitations of the Linux kernel.
Specifically, it’s providing two main things
- A set of files which can be bind-mounted over their /proc originals to provide CGroup-aware values.
- A cgroupfs-like tree which is container aware.
参考链接:https://linuxcontainers.org/lxcfs/introduction/
部署
lxcfs的部署方式有很多,可以直接添加源,并使用如apt或yum等包管理器安装。也可以使用源码编译安装。
本次我们测试,参考官方文档,使用源码包编译安装。
官方完整步骤如下:
yum install fuse fuse-lib fuse-devel (官方文档这里写的应该有问题,应该是fuse-libs) git clone git://github.com/lxc/lxcfs cd lxcfs ./bootstrap.sh ./configure make make install
以下所有安装步骤均来自官方文档:https://github.com/lxc/lxcfs
软件安装
-
安装编译环境
我使用的centos8系统,这里使用的都是fuse3的包,并不是fuse,不然在configure步骤会报错缺少此包,你也可以不加,等到报错后,在根据报错来确定问题
yum install fuse3 fuse3-libs fuse3-devel
-
下载所需源码包
下载地址:https://github.com/lxc/lxcfs/releases
这里官方给的做法是直接git克隆,但是克隆后,并没有安装包所说的文件
-
解压并进入lxcfs目录
-
执行
./bootstrap.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
[root@VM-0-2-centos lxcfs-lxcfs-4.0.9]# ./bootstrap.sh ++ set -e ++ test -d autom4te.cache ++ libtoolize libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, 'config'. libtoolize: linking file 'config/ltmain.sh' libtoolize: putting macros in AC_CONFIG_MACRO_DIRS, 'm4'. libtoolize: linking file 'm4/libtool.m4' libtoolize: linking file 'm4/ltoptions.m4' libtoolize: linking file 'm4/ltsugar.m4' libtoolize: linking file 'm4/ltversion.m4' libtoolize: linking file 'm4/lt~obsolete.m4' ++ aclocal -I m4 ++ autoheader ++ autoconf ++ automake --add-missing --copy configure.ac:16: installing 'config/compile' configure.ac:15: installing 'config/config.guess' configure.ac:15: installing 'config/config.sub' configure.ac:14: installing 'config/install-sh' configure.ac:14: installing 'config/missing' Makefile.am: installing './INSTALL' src/Makefile.am: installing 'config/depcomp'
-
执行
./configure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[root@VM-0-2-centos lxcfs-lxcfs-4.0.9]# ./configure checking for pkg-config... /usr/bin/pkg-config checking pkg-config is at least version 0.9.0... yes checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for a thread-safe mkdir -p... /usr/bin/mkdir -p checking for gawk... gawk checking whether make sets $(MAKE)... yes checking whether make supports nested variables... yes checking build system type... x86_64-pc-linux-gnu checking host system type... x86_64-pc-linux-gnu checking whether make supports the include directive... yes (GNU style) checking for gcc... gcc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether gcc accepts -g... yes checking for gcc option to accept ISO C89... none needed ... ... ... config.status: creating config.h config.status: executing depfiles commands config.status: executing libtool commands ---------------------------- Environment: - compiler: gcc Debugging: - tests: - ASAN: no - mutex debugging:
-
执行
make && make install
这里的输出太长了,就不贴了,自行观察无报错即可,有报错再具体看,不同系统可能结果会不同
软件启动
官方给出的是前台运行,但这显然不符合使用喜欢,可以用nohup将其放到后台,并输出相关日志即可
为了开启实现自启动,需要添加相应的开机启动,如是通过包管理工具安装的,也要将此设置为开机自启
|
|
观察日志如下
|
|
隔离验证
启动工作负载
启动两个完全一样的workload,两者唯一的区别是一个未挂载任何卷,一个挂载有宿主机/var/lib/lxcfs下的内容
两者yaml如下,已省略非必要内容,容器的内存limit为1G,CPU的limit为0.5,这个在yaml中就不去体现了。
挂载hostpath,意味着使用lxcfs进行隔离:
|
|
未挂载hostpath,即不使用lxcfs隔离:
|
|
验证
挂载hostpath的容器:
|
|
未挂载hostpath的容器:
|
|
通过上述可以看到,未挂载hostpath的容器,所看到的信息,均为节点的信息,而并非容器真实的,但是挂载了hostpath的,看到的是容器的信息。
总结
目前lxcfs已经支持如下的试图隔离,没有一一测试,具体支持情况,还是建议在使用时参考官网,避免出现规则变化,导致无效。
/proc/cpuinfo /proc/diskstats /proc/meminfo /proc/stat /proc/swaps /proc/uptime /proc/slabinfo /sys/devices/system/cpu /sys/devices/system/cpu/online
优点:
很明显,隔离了,观察更加方便了。
缺点:
也很明显,要配置很多hostpath,如果是docker run方式启动,也要去挂载很多卷,这个是很不方便的,不知道后续,此特性是否会集成到linux内核中,默认即隔离。
遇到的问题
当lxcfs崩掉后,日志会打印Transport endpoint is not connected,并且/var/lib/lxcfs目录状态如下
|
|
可通过如下方式解决,然后重启lxcfs即可。
fusermount -u /var/lib/lxcfs
参考:https://github.com/lxc/lxcfs/issues/73
后来发现,如果使用bin包直接安装,使用systemd管理时,生成的lxcfs.service
配置文件,有对这个问题的处理
那就使用systemd管理吧:
|
|
别忘了systemctl enable lxcfs