如何监控容器中的进程?
How to monitor the process in a container?
我目前正在研究 LXC 容器 API。我想弄清楚如何让操作系统知道当前 运行 进程属于哪个容器。这样OS可以根据容器为进程分配资源
我假设您的查询是 - 给定 PID,如何找到此进程所在的容器 运行?
我会根据我最近阅读的 Linux 容器尝试回答这个问题。每个容器都可以配置为以其自己的用户和组 ID 映射开始。
来自https://linuxcontainers.org/lxc/manpages/man5/lxc.container.conf.5.html:
lxc.id_map
Four values must be provided. First a character, either 'u', or 'g', to specify whether user or group ids are being mapped. Next is
the first userid as seen in the user namespace of the container. Next
is the userid as seen on the host. Finally, a range indicating the
number of consecutive ids to map.
因此,您可以在配置文件中添加类似的内容(例如:~/.config/lxc/default.conf):
lxc.id_map = u 0 100000 65536
lxc.id_map = g 0 100000 65536
以上基本上意味着0到65536之间的uids/gids映射到100000到1655356之间的数字。因此,容器上的uid为0(根)将被视为主机上的100000
例如,在容器内部它看起来像这样:
root@unpriv_cont:/# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 02:18 ? 00:00:00 /sbin/init
root 157 1 0 02:18 ? 00:00:00 upstart-udev-bridge --daemon
但在主机上,相同的进程将如下所示:
ps -ef | grep 100000
100000 2204 2077 0 Dec12 ? 00:00:00 /sbin/init
100000 3170 2204 0 Dec12 ? 00:00:00 upstart-udev-bridge --daemon
100000 1762 2204 0 Dec12 ? 00:00:00 /lib/systemd/systemd-udevd --daemon
因此,您可以通过查找进程的 UID 并将其与容器配置中定义的映射相关联来找到进程的容器。
我目前正在研究 LXC 容器 API。我想弄清楚如何让操作系统知道当前 运行 进程属于哪个容器。这样OS可以根据容器为进程分配资源
我假设您的查询是 - 给定 PID,如何找到此进程所在的容器 运行?
我会根据我最近阅读的 Linux 容器尝试回答这个问题。每个容器都可以配置为以其自己的用户和组 ID 映射开始。
来自https://linuxcontainers.org/lxc/manpages/man5/lxc.container.conf.5.html:
lxc.id_map
Four values must be provided. First a character, either 'u', or 'g', to specify whether user or group ids are being mapped. Next is the first userid as seen in the user namespace of the container. Next is the userid as seen on the host. Finally, a range indicating the number of consecutive ids to map.
因此,您可以在配置文件中添加类似的内容(例如:~/.config/lxc/default.conf):
lxc.id_map = u 0 100000 65536
lxc.id_map = g 0 100000 65536
以上基本上意味着0到65536之间的uids/gids映射到100000到1655356之间的数字。因此,容器上的uid为0(根)将被视为主机上的100000
例如,在容器内部它看起来像这样:
root@unpriv_cont:/# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 02:18 ? 00:00:00 /sbin/init
root 157 1 0 02:18 ? 00:00:00 upstart-udev-bridge --daemon
但在主机上,相同的进程将如下所示:
ps -ef | grep 100000
100000 2204 2077 0 Dec12 ? 00:00:00 /sbin/init
100000 3170 2204 0 Dec12 ? 00:00:00 upstart-udev-bridge --daemon
100000 1762 2204 0 Dec12 ? 00:00:00 /lib/systemd/systemd-udevd --daemon
因此,您可以通过查找进程的 UID 并将其与容器配置中定义的映射相关联来找到进程的容器。