据我所知,如果我们需要调整“打开文件”nofile
(软硬)在linux系统中,我们需要运行命令ulimit
或在相关配置文件中设置以永久获取设置。但是我对在主机中运行的容器的设置有点困惑
例如,如果 Linux 操作系统有 ulimit
nofile 设置为 1024 (soft) 和 Hard (4096) ,我使用 --ulimit nofile=10240:40960
运行 docker ,容器可以使用比其主机更多的 nofiles 吗?
更新
在我的环境中,当前运行 docker 的设置,
我发现该应用程序可以在大约 100K 打开文件的情况下运行,然后崩溃。这个怎么理解?
真正的极限是什么?
最佳答案
For example, If a Linux OS has
ulimit
nofile set to 1024 (soft) and Hard (4096) , and I run docker with----ulimit nofile=10240:40960
, could the container use more nofiles than its host?
CAP_SYS_RESOURCE
能力设置在它的权限上。这意味着 Docker 能够设置
ulimit
与主人不同。根据 man 2 prlimit
: A privileged process (under Linux: one with the CAP_SYS_RESOURCE capability in the initial user namespace) may make arbitrary changes to either limit value.
您可以使用以下命令检查 docker 守护进程限制:
$ cat /proc/$(ps -A | grep dockerd | awk '{print $1}')/limits | grep "files"
Max open files 1048576 1048576 files
1048576
。所以你的 40960 将工作像一个魅力。 --ulimit
运行 docker 容器设置为高于节点但低于守护程序本身,您不会发现任何问题,并且不需要像下面的示例中那样提供额外的权限:$ cat /proc/$(ps -A | grep dockerd | awk '{print $1}')/limits | grep "files"
Max open files 1048576 1048576 files
$ docker run -d -it --rm --ulimit nofile=99999:99999 python python;
354de39a75533c7c6e31a1773a85a76e393ba328bfb623069d57c38b42937d03
$ cat /proc/$(ps -A | grep python | awk '{print $1}')/limits | grep "files"
Max open files 99999 99999 files
/etc/init.d/docker
上为 dockerd 设置新的限制。 : $ cat /etc/init.d/docker | grep ulimit
ulimit -n 1048576
ulimit
高于 docker 守护进程,它有点棘手,但可行,引用 here . securityContext.priviledged: true
, 这样就可以运行命令ulimit
作为容器内的根,这里是一个例子:image: image-name
command: ["sh", "-c", "ulimit -n 65536"]
securityContext:
privileged: true
关于linux - 需要了解主机和容器中 "ulimit"的 nofile 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62127643/