Too Many Open files
The error
Section titled “The error”failed to create fsnotify watcher: too many open filesOne of the inotify limit has been reached
by the k3s user (ie root)
Concept
Section titled “Concept”Watch descriptors (WD)
Section titled “Watch descriptors (WD)”A watch descriptor (WD) is an unique integer handle returned by the inotify
when a process asks the kernel to watch a file or directory for changes.
This is required for each directory:
- 1 watched directory = 1 watch descriptor
- 10,000 watched directories = 10,000 watch descriptors
Limits
Section titled “Limits”inotify.max_user_watches
Section titled “inotify.max_user_watches”fs.inotify.max_user_watches: the maximum number of watch descriptors a user can create (not
per process)
Default value (1% memory): inotify.max_user_watches is calculated to make use no more
than 1% of addressable memory within the range 8192, 1048576.
https://github.com/torvalds/linux/commit/92890123749bafc317bbfacbe0a62ce08d78efb7
inotify.max_user_instances
Section titled “inotify.max_user_instances”inotify.max_user_instances Maximum number of inotify instances a user can create.
Each program that wants to watch files creates one instance first, then adds watches to it.
Check the log
Section titled “Check the log”If you can’t watch, you can’t follow a file, so you cannot also follow the journal
of k3s
Executing the below command should output as first line: Insufficient watch descriptors available. Reverting to -n.
journalctl --unit=k3s --followIf this is the case, we need to check the limits
If not, check the fsnotify message
journalctl --unit=k3s --since "10 minutes ago" | grep -A5 -B5 fsnotifyCheck the Limits
Section titled “Check the Limits”- Check the max_user_watches current limit:
cat /proc/sys/fs/inotify/max_user_watches# 60123sudo sysctl fs.inotify.max_user_watches#fs.inotify.max_user_watches = 60123Check the max_user_instances current limit
cat /proc/sys/fs/inotify/max_user_instances# 128sudo sysctl fs.inotify.max_user_instances# fs.inotify.max_user_instances = 128Check the actual value
Section titled “Check the actual value”https://github.com/fatso83/dotfiles/blob/master/utils/scripts/inotify-consumers
curl -s https://raw.githubusercontent.com/fatso83/dotfiles/713ea5547903ee49428f8130af5ab2c956471914/utils/scripts/inotify-consumers | bashExample of output, showing a max_user_instances reached:
INotify instances per user (e.g. limits specified by fs.inotify.max_user_instances):INSTANCES USER----------- ------------------126 rootIncrease the limit and test
Section titled “Increase the limit and test”If max_user_instances was reached, double it
# temporallysysctl -w fs.inotify.max_user_instances=256Check that you don’t get any error with a journal follow:
journalctl --unit=k3s --follow# no more: Insufficient watch descriptors available. Reverting to -n.cat >> /etc/sysctl.d/99-k3s-inotify.conf << 'EOF'fs.inotify.max_user_instances = 256EOFSave them permanentyly, if successfully
Section titled “Save them permanentyly, if successfully”If successful, you can set them in your cluster values file.
They will be set when running the kubee cluster play command
Example:
k3s_ansible: hosts: servers: - fqdn: 'sub.domain.com' ip: 'x.x.x.x' inotify_max_user_instances: '256' inotify_max_user_watches: '120246'