Skip to content

[backport-0.21] engine: keep cgroups inside the engine's container under Kubernetes - #15096

Merged
sipsma merged 2 commits into
backport-0.21from
sipsma/backport-0.21-engine-cgroupns
Nov 9, 2026
Merged

sipsma merged 2 commits into
backport-0.21from
sipsma/backport-0.21-engine-cgroupns

Conversation

@sipsma

@sipsma sipsma commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

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), beside kubepods.slice and 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-misc for unshare, 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-dev instead 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 exec still work.

  • Under Docker, nothing changes with the default private cgroup namespace.

  • As on main, the second commit clears kubelet's memory.oom.group on 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.

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]>
@sipsma
sipsma merged commit f13565f into backport-0.21 Nov 9, 2026
77 checks passed
@sipsma
sipsma deleted the sipsma/backport-0.21-engine-cgroupns branch November 9, 2026 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants