首页 > 解决方案 > 如何更改 Kubernetes 中的文件系统观察程序限制 (fs.inotify.max_user_watches)

问题描述

我正在使用pm2来查看包含我的应用服务器的 NodeJS 程序的源代码的目录,该程序在 Kubernetes 集群中运行。

但是,我收到此错误:

ENOSPC: System limit for number of file watchers reached

我搜索了那个错误,找到了这个答案:https ://stackoverflow.com/a/55763478

# insert the new value into the system config
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

但是,我尝试在目标 k8s 节点上的 pod 中运行它,它说sudo找不到该命令。如果我删除sudo,我会收到此错误:

sysctl: setting key "fs.inotify.max_user_watches": Read-only file system

如何将文件系统观察程序限制从 Kubernetes 节点上的 8192 修改为更高的值,例如 524288?

标签: linuxkubernetesinotify

解决方案


我找到了一个解决方案:使用在集群中每个节点上运行的特权守护程序集,它具有修改fs.inotify.max_user_watches变量的能力。

将以下内容添加到node-setup-daemon-set.yaml包含在 Kubernetes 集群中的文件中:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-setup
  namespace: kube-system
  labels:
    k8s-app: node-setup
spec:
  selector:
    matchLabels:
      name: node-setup
  template:
    metadata:
      labels:
        name: node-setup
    spec:
      containers:
      - name: node-setup
        image: ubuntu
        command: ["/bin/sh","-c"]
        args: ["/script/node-setup.sh; while true; do echo Sleeping && sleep 3600; done"]
        env:
          - name: PARTITION_NUMBER
            valueFrom:
              configMapKeyRef:
                name: node-setup-config
                key: partition_number
        volumeMounts:
          - name: node-setup-script
            mountPath: /script
          - name: dev
            mountPath: /dev
          - name: etc-lvm
            mountPath: /etc/lvm
        securityContext:
          allowPrivilegeEscalation: true
          privileged: true
      volumes:
        - name: node-setup-script
          configMap:
            name: node-setup-script
            defaultMode: 0755
        - name: dev
          hostPath:
            path: /dev
        - name: etc-lvm
          hostPath:
            path: /etc/lvm
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: node-setup-config
  namespace: kube-system
data:
  partition_number: "3"
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: node-setup-script
  namespace: kube-system
data:
  node-setup.sh: |
    #!/bin/bash
    set -e

    # change the file-watcher max-count on each node to 524288

    # insert the new value into the system config
    sysctl -w fs.inotify.max_user_watches=524288

    # check that the new value was applied
    cat /proc/sys/fs/inotify/max_user_watches

注意:上面的文件可能会简化很多。(我以本指南为基础,并留下了很多简单运行命令可能不需要的东西sysctl。)如果其他人成功地进一步修剪它,同时确认它仍然有效,请随时进行/建议这些编辑对我的回答。


推荐阅读