如何更改 Kubernetes 中的文件系统观察程序限制 (fs.inotify.max_user_watches)

Ven*_*ryx 6 linux inotify kubernetes

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

但是,我收到此错误:

ENOSPC: System limit for number of file watchers reached
Run Code Online (Sandbox Code Playgroud)

我搜索了该错误,找到了这个答案:/sf/answers/3903443491/

# insert the new value into the system config
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
Run Code Online (Sandbox Code Playgroud)

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

sysctl: setting key "fs.inotify.max_user_watches": Read-only file system
Run Code Online (Sandbox Code Playgroud)

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

Ven*_*ryx 3

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

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

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
Run Code Online (Sandbox Code Playgroud)

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