需要了解主机和容器中 "ulimit" 的 nofile 设置
Need understand "ulimit"'s nofile setting in host and container
据我所知,如果我们需要在linux系统中调整"open files" nofile
(软硬),我们需要运行命令ulimit
或设置在相关配置文件中永久获取设置。但是我对主机中容器 运行ning 的设置有点困惑
例如,如果 Linux OS 的 ulimit
nofile 设置为 1024(软)和硬 (4096),并且我 运行 docker 与 --ulimit nofile=10240:40960
相比,容器可以使用比其主机更多的 nofiles 吗?
更新
在我的环境中,当前设置为 dockers 运行ning,
- 在主机 (Debian) 上 - 65535(软)65535(硬)
- Docker 守护程序设置最大值 - 1048576(软)1048576(硬)
- 默认 docker 运行 - 1024(软)4096(硬)
- 定制 docker 运行 - 10240(软)40960(硬)
我发现应用程序可以 运行 打开大约 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?
- Docker 在其权限上设置了
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 守护进程设置的。
您可以使用此命令检查 docker 守护程序限制:
$ cat /proc/$(ps -A | grep dockerd | awk '{print }')/limits | grep "files"
Max open files 1048576 1048576 files
如您所见,docker 19 有一个相当高的 1048576
限制,因此您的 40960 将像 魅力.
如果你 运行 一个 docker 容器 --ulimit
设置为高于节点但低于守护进程本身,你将找不到任何问题,并且不需要像下面的示例那样提供额外的权限:
$ cat /proc/$(ps -A | grep dockerd | awk '{print }')/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 }')/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。
- 我看到你已经标记了 Kubernetes 标签,但你的问题中没有提到它,但是为了让它在 Kubernetes 上工作,容器将需要
securityContext.priviledged: true
,这样你可以 运行命令ulimit
作为容器内的root,这里是一个例子:
image: image-name
command: ["sh", "-c", "ulimit -n 65536"]
securityContext:
privileged: true
据我所知,如果我们需要在linux系统中调整"open files" nofile
(软硬),我们需要运行命令ulimit
或设置在相关配置文件中永久获取设置。但是我对主机中容器 运行ning 的设置有点困惑
例如,如果 Linux OS 的 ulimit
nofile 设置为 1024(软)和硬 (4096),并且我 运行 docker 与 --ulimit nofile=10240:40960
相比,容器可以使用比其主机更多的 nofiles 吗?
更新
在我的环境中,当前设置为 dockers 运行ning,
- 在主机 (Debian) 上 - 65535(软)65535(硬)
- Docker 守护程序设置最大值 - 1048576(软)1048576(硬)
- 默认 docker 运行 - 1024(软)4096(硬)
- 定制 docker 运行 - 10240(软)40960(硬)
我发现应用程序可以 运行 打开大约 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?
- Docker 在其权限上设置了
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 守护进程设置的。 您可以使用此命令检查 docker 守护程序限制:
$ cat /proc/$(ps -A | grep dockerd | awk '{print }')/limits | grep "files"
Max open files 1048576 1048576 files
如您所见,docker 19 有一个相当高的
1048576
限制,因此您的 40960 将像 魅力.如果你 运行 一个 docker 容器
--ulimit
设置为高于节点但低于守护进程本身,你将找不到任何问题,并且不需要像下面的示例那样提供额外的权限:
$ cat /proc/$(ps -A | grep dockerd | awk '{print }')/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 }')/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。 - 我看到你已经标记了 Kubernetes 标签,但你的问题中没有提到它,但是为了让它在 Kubernetes 上工作,容器将需要
securityContext.priviledged: true
,这样你可以 运行命令ulimit
作为容器内的root,这里是一个例子:
image: image-name
command: ["sh", "-c", "ulimit -n 65536"]
securityContext:
privileged: true