在应用没有用容器的方式运行时,可以用nsenter和cgroup相关命令来进行控制,对应用进行资源隔离和namespace限制。本文先给出命令工具的入口,后续单独出一篇讲其应用场景及使用样例。
linux namespace 控制工具,用于进入和设置namespace
[root@gentlewok ~]# nsenter --help
Usage:
nsenter [options]
<program>
[<argument>...]
Run a program with namespaces of other processes.
Options:
-t,
--target <pid> target process to get namespaces from
-m,
--mount[=<file>] enter mount namespace
-u,
--uts[=<file>] enter UTS namespace
(hostname etc)
-i,
--ipc[=<file>] enter System V IPC namespace
-n,
--net[=<file>] enter network namespace
-p,
--pid[=<file>] enter pid namespace
-U,
--user[=<file>] enter user namespace
-S,
--setuid <uid>
set uid in entered namespace
-G,
--setgid <gid>
set gid in entered namespace
--preserve-credentials do
not touch uids or gids
-r,
--root[=<dir>]
set the root directory
-w,
--wd[=<dir>]
set the working directory
-F,
--no-fork do
not fork before exec'ing <program>
-Z, --follow-context set SELinux context according to --target PID
-h, --help display this help and exit
-V, --version output version information and exit
For more details see nsenter(1).
[root@gentlewok ~]# nsenter -V
nsenter from util-linux 2.23.2
用于控制进程资源相关操作 ,在linux机器上执行 man + 红框中的命令(去掉末尾数字)即可获得对应用法
https://github.com/libcgroup/libcgroup/tree/main/doc/man
运行时是runc
runc的源码里也写了一个nsenter,用于操作linux namespaces
扩展阅读:
在网上找到两篇对容器的创建流程讲解得比较详细的文章,供各位参考,向大佬致敬!
1、create and run a container
https://terenceli.github.io/%E6%8A%80%E6%9C%AF/2021/12/23/runc-internals-2
https://terenceli.github.io/%E6%8A%80%E6%9C%AF/2021/12/28/runc-internals-3