Skip to main content
Fixing "Too Many Open Files" in Kubernetes Containers

Fixing "Too Many Open Files" in Kubernetes Containers

KU
· Founder & Chief Editor
• 8 min read•Updated •56 views

A pod that logged fine for weeks starts throwing EMFILE at 3am. Here's how to tell a real file-descriptor leak from a limit that's just set too low.

Key takeaways

  • The error means a container process hit its RLIMIT_NOFILE soft limit.
  • Count its descriptors with ls /proc/1/fd and compare with /proc/1/limits.
  • A count that keeps climbing is a leak to fix in code; a flat, high count needs a higher nofile limit on the container runtime, or higher node inotify sysctls.

"Too many open files" (EMFILE) means a container process hit its RLIMIT_NOFILE soft limit. Run ls /proc/1/fd | wc -l inside the pod and compare it with cat /proc/1/limits. If the count keeps climbing, fix the leak in code. If it is flat and high, raise LimitNOFILE on containerd. If the node's inotify watches are exhausted, raise those sysctls.

Verified against

Kubernetes 1.37 docs, containerd 2.4.2 (latest release, October 2026), Linux man-pages 6.19 (getrlimit(2), inotify(7)), against kubernetes.io's sysctl task page and the containerd v2.0.0 release notes. Checked October 2026.

The typical version of this incident: a node cordons, pods reschedule, traffic doubles onto fewer replicas, and one of them starts spitting accept: too many open files into the logs. Requests hang. The pod stays Ready because its liveness probe is cheap enough to still answer.

The kernel refused a new file descriptor because that process already holds as many FDs as its RLIMIT_NOFILE soft limit allows. The word "files" is what throws people. On Linux almost everything is a file descriptor: open files, yes, but also every TCP socket, every pipe, every epoll instance, every inotify instance. A busy web service can sit at thousands of FDs without touching a single file on disk, because each keep-alive connection is one FD, each upstream connection is another.

So the error is real, but it does not tell you which of two very different things is wrong. Either you have a leak and the FD count climbs forever, or you have honest concurrency and the limit is simply set too low. Fixing the second is one line. "Fixing" the first with that same one line just moves the crash to next Tuesday.

Find the process and count its FDs

Get a shell in the container, or use kubectl debug if the image has no shell:

code
$ kubectl exec -it mypod -- sh
# or, for a distroless image:
$ kubectl debug -it mypod --image=busybox --target=app -- sh

Find the PID (usually 1 in a container) and count what it holds, then compare it to the ceiling:

code
$ ls /proc/1/fd | wc -l
$ grep 'open files' /proc/1/limits
Max open files            1024                 524288               files
$ ulimit -n

The first column is the soft limit, the one that actually bites; per getrlimit(2), calls like open and accept that would exceed it fail with EMFILE. The hard limit is only the ceiling an unprivileged process may raise its soft limit to. If the FD count is sitting near the soft limit, that is your wall. ulimit -n shows what the shell sees, which can differ from what the app inherited.

Now watch it over a minute or two:

code
$ while true; do echo "$(date +%T) $(ls /proc/1/fd | wc -l)"; sleep 5; done

Flat under load is honest concurrency. A line that only ever climbs, even when traffic dips, is a leak. That single observation decides everything that follows.

If you want to know what the FDs are, lsof -p 1 groups them, or just eyeball the symlinks:

code
$ ls -l /proc/1/fd | awk '{print $NF}' | sort | uniq -c | sort -rn | head

Ten thousand entries pointing at socket:[...] to the same upstream IP is a connection leak. A pile of anon_inode:inotify is a different animal, covered below. The same diagnostic loop works on a plain Linux host, which our too many open files guide for Linux covers in more depth.

When it's a real leak

This is the common one and the boring answer nobody wants: the fix is in the application, not the manifest. Somewhere a code path opens a socket or a file and never closes it on the error branch. HTTP clients that get created per-request instead of reused. Database connections checked out and never returned. A file handle opened in a loop without a defer close or a with block.

Reproduce it in a loop, watch /proc/1/fd climb, find the code path that correlates, and close the handle. Reuse your HTTP client and connection pool instead of minting one per call. If you genuinely cannot fix the upstream library today, raising the limit buys you time to schedule a restart before the crash, but be honest that it is a countdown, not a fix.

When the limit is honestly too low

If the count is flat and just high, raise the ceiling. Kubernetes has no pod-spec field for nofile, and a pod sysctls entry will not help either: fs.file-max is a node-wide kernel setting, not a namespaced sysctl, so the Kubernetes docs say it must be set on the node (or by a privileged DaemonSet), and the kubelet will not accept it in a pod's securityContext.

The per-process nofile limit comes from the container runtime. Containers inherit it from containerd unless the runtime spec says otherwise, and containerd 2.0 removed LimitNOFILE from its reference containerd.service (listed as a breaking change in the v2.0.0 release notes). On nodes running containerd 2.x, containers therefore fall back to systemd's default, typically a soft limit of 1024, which a modern service blows through instantly. Set it explicitly with a drop-in on the node:

code
# /etc/systemd/system/containerd.service.d/override.conf
[Service]
LimitNOFILE=1048576
code
$ sudo systemctl daemon-reload
$ sudo systemctl restart containerd
$ kubectl rollout restart deployment/myapp

Existing containers keep the limit they started with, so roll the pods, then confirm inside the container with cat /proc/1/limits. Restarting containerd touches every pod on the node, so do it during a drain. Pick a number with headroom over your steady-state count, not an arbitrary million because a blog said so. On managed node pools where you cannot edit units, the fallback is the application itself: raise its soft limit up to the hard limit at startup.

For the rest of the node-level knobs that bite containers, see our guide to Linux performance tuning for containers and Kubernetes nodes.

Inotify exhaustion, the sneaky one

Sometimes the app is fine and the error still fires, because inotify is governed by node-level kernel limits. Per inotify(7), max_user_instances and max_user_watches are counted per real user ID, so every container on the node running as the same UID (often root) draws from one pool. File-watching tools, config-reload libraries, and log shippers each grab instances and watches. Enough noisy neighbors and the node runs dry, and a totally innocent pod gets the error.

Check and raise the node sysctls:

code
$ sysctl fs.inotify.max_user_watches fs.inotify.max_user_instances
code
# /etc/sysctl.d/99-inotify.conf
fs.inotify.max_user_watches = 524288
fs.inotify.max_user_instances = 512

Apply with sysctl -p /etc/sysctl.d/99-inotify.conf. These are per-node, so a DaemonSet or node-init handles it cleanly. While you are on the node, glance at conntrack too, because a full connection-tracking table (nf_conntrack: table full) produces the same "connections mysteriously fail" symptom from a different ceiling: sysctl net.netfilter.nf_conntrack_count net.netfilter.nf_conntrack_max.

For the broader playbook on reading these signals, our Kubernetes troubleshooting guide walks the same diagnostic loop across other failure modes.

Questions people ask

How do I increase the open file limit for a Kubernetes pod?

There is no pod-spec field for it. Containers inherit RLIMIT_NOFILE from the container runtime, so set LimitNOFILE in a systemd drop-in for containerd.service on each node, restart containerd during a drain, and recreate the pods. On managed nodes you cannot edit, raise the soft limit inside the application at startup, up to the hard limit shown in /proc/1/limits.

Can I set fs.file-max in a pod securityContext?

No. fs.file-max is a global kernel setting, not a namespaced sysctl, so Kubernetes will not let a pod set it, even through --allowed-unsafe-sysctls. It is also rarely the limit you are hitting: EMFILE comes from the per-process nofile limit, while the system-wide table running out shows up as ENFILE. Set node-wide sysctls with a node config or privileged DaemonSet.

Why do containers get a 1024 open files limit after upgrading to containerd 2?

containerd 2.0 dropped LimitNOFILE from its reference containerd.service unit. Without it, containerd and every container it starts inherit systemd's default, which on most distributions is a 1024 soft limit. Add an explicit LimitNOFILE drop-in for containerd on the node, restart it, and recreate the pods so the new limit applies.

The call we'd make

Count the FDs before you touch a single manifest. If the number climbs, it is a leak and raising nofile only reschedules the outage, so fix the code. If the number is flat and high, raise the limit at the runtime, give it real headroom, and move on. And check the node sysctls for inotify and conntrack before you blame the app, because a shared ceiling makes an innocent pod look guilty. The limit is a smoke detector. Ripping the battery out because it keeps beeping is not maintenance.

Sources

Explore topics:KubernetesLinuxDevOps
React
Share this post
Cite this article
Plain text

Kiril Urbonas (2026). Fixing "Too Many Open Files" in Kubernetes Containers. DevOpsNess. Updated 2026-10-31. https://www.devopsness.com/blog/fix-too-many-open-files-k8s

Markdown

[Fixing "Too Many Open Files" in Kubernetes Containers](https://www.devopsness.com/blog/fix-too-many-open-files-k8s) (Kiril Urbonas, DevOpsNess, updated 2026-10-31)

KU

Kiril Urbonas

Founder & Chief Editor

586 articles
View all articles by Kiril Urbonas

You might have missed

Evergreen posts worth revisiting.