Repository navigation
[backport-0.21] engine: keep cgroups inside the engine's container under Kubernetes - #15096
Merged
Merged
Conversation
A privileged container on cgroup v2 under Kubernetes' containerd shares the host's cgroup namespace, so /sys/fs/cgroup is the node's whole tree. Every exec's cgroup, /buildkit/<id>, then landed at the node's root, beside kubepods.slice, where the pod's CPU and memory limits don't apply, so execs ran unthrottled and unbounded. When the entrypoint's own cgroup isn't its namespace root, bind its own cgroup over /sys/fs/cgroup and re-run itself in a new cgroup namespace rooted there. Everything below then sees the container's cgroup as the root, as it does under Docker's private cgroup namespace, and the existing setup runs unchanged inside it. unshare comes from Wolfi's util-linux-misc, now installed in the engine image; Wolfi's busybox has no unshare applet. Signed-off-by: Erik Sipsma <[email protected]>
…y limit With the engine's cgroups inside its container, an exec now meets the container's memory limit. Kubelet sets memory.oom.group=1 on every container's cgroup, so the kernel then killed every process in the container: the engine restarted, dropping every session, and reset its cache after the unclean shutdown. Clear memory.oom.group on the engine's own container cgroup when the entrypoint takes over its cgroup, so the OOM killer takes only what it picks, as under Docker: the exec fails and the engine keeps running. Signed-off-by: Erik Sipsma <[email protected]>
marcosnils
approved these changes
Nov 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport to 0.21 of #15095, with the same two commits.
What this changes for 0.21 users
Under Kubernetes, the engine's pod CPU and memory limits didn't apply to execs. The Helm chart runs the engine privileged, and on cgroup v2 containerd then shares the node's cgroup namespace with it. Every exec's cgroup,
/buildkit/<id>, landed at the node's root (/sys/fs/cgroup/buildkit), besidekubepods.sliceand outside the pod. On k3s with v0.21.11 and a pod limited to 1 CPU, one exec used 3.97 cores and was never throttled.With this backport, the entrypoint gives the engine its own cgroup namespace, rooted at its container's cgroup, whenever it doesn't already start at one. Execs then nest under the engine's container and its limits, as they do under Docker. The engine image gains Wolfi's
util-linux-miscforunshare, about 22 MB.On 0.21 the engine process itself stayed in the pod. Leaving the pod is a 1.0-beta problem, fixed by the main PR.
Differences from main
The change applied as is, to
toolchains/engine-devinstead of.dagger/modules/engine-dev.Verification
I verified an engine image built from this branch on k3s v1.37.2 with the v0.21.11 Helm chart and a pod limit of 1 CPU and 2 GiB:
The engine runs in
<container scope>/init, and execs in<container scope>/buildkit/<id>. Nothing new appears at the node's cgroup root.A 4-thread busy-loop exec is held to 1 CPU (0.87 cores for the exec, 1.00 for the pod).
The probes and
kubectl execstill work.Under Docker, nothing changes with the default private cgroup namespace.
As on main, the second commit clears kubelet's
memory.oom.groupon the engine's container cgroup. A 3 GB exec in a 2 GiB pod then fails with exit 137 inside the exec, and the engine keeps running without a restart.