如何修改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?

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

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

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命令来说可能不需要的东西。)如果其他人成功地进一步修改了它,同时确认它仍然有效,请随意对我的答案进行编辑/建议。

如果可以的话,您不希望您的容器作为特权容器运行。

这里的解决方案是设置以下内核参数,然后重新启动容器。容器将使用容器所在内核中的变量。这是因为容器不会在Linux主机上运行单独的内核(容器使用相同的内核)。
fs.inotify.max_user_watches=10485760
fs.aio-max-nr=10485760
fs.file-max=10485760
kernel.pid_max=10485760
kernel.threads-max=10485760

你应该把上面的内容粘贴到:/etc/sysctl.conf.

最新更新