linux - 需要了解主机和容器中 "ulimit"的 nofile 设置

标签 linux docker ubuntu kubernetes debian

据我所知,如果我们需要调整“打开文件”nofile (软硬)在linux系统中,我们需要运行命令ulimit或在相关配置文件中设置以永久获取设置。但是我对在主机中运行的容器的设置有点困惑

例如,如果 Linux 操作系统有 ulimit nofile 设置为 1024 (soft) 和 Hard (4096) ,我使用 --ulimit nofile=10240:40960 运行 docker ,容器可以使用比其主机更多的 nofiles 吗?

更新

在我的环境中,当前运行 docker 的设置,

  • 在主机 (Debian) - 65535(软)65535(硬)
  • Docker 守护程序设置 Max - 1048576(软)1048576(硬)
  • 默认 docker run - 1024(软)4096(硬)
  • 自定义 docker run - 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 $1}')/limits | grep "files"
    Max open files            1048576              1048576              files 
    
  • 如您所见,docker 19 的上限非常高,为 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 .
  • 我看到你已经标记了 Kubernetes 标签,但在你的问题中没有提到它,但是为了让它在 Kubernetes 上工作,容器需要 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/

    相关文章:

    c++ - 许多 XSetInputFocus 和 XSync 导致错误

    c - 在 init_timer() 之前调用 timer_pending() 是否安全?

    c - 左值需要作为一元 '&"操作数

    python - Docker不复制 Assets 文件夹

    python - 如何让 virtualenv 在 Ubuntu 上看到默认的 python 版本

    linux - Linux 中的 std.net.curl 链接器错误

    c - 为什么Linux内核在preempt_schedule_irq中启用中断?

    c - Linux C-keyMapping keyCodes

    docker - 如何通过存储库删除本地 Docker 镜像?

    docker - 如何在后台捕获通过 `docker-compose exec -d` 运行的命令的日志?