Skip to content

runAsNonRoot and image will run as root: admission yes, kubelet no

Securitytested on Kubernetes v1.37.1

Pod Security Admission only checks whether runAsNonRoot true is set. Whether the image really starts as non-root is only noticed by the kubelet before it starts the container. If the image runs as root, the pod stays in CreateContainerConfigError.

Symptom

k -n tag09-strict apply -f shell.yaml
pod/shell created
NAME    READY   STATUS                       RESTARTS   AGE
shell   0/1     CreateContainerConfigError   0          7s

The pod is accepted – even in a namespace with enforce restricted. The error only appears when the container is created.

Diagnosis

The message is in the pod's events.

k -n tag09-strict describe pod shell | grep -m1 'Error:'
  ... Error: container has runAsNonRoot and image will run as root (pod:
  "shell_tag09-strict(b26d…)", container: c)

Causes

busyboxThe image has no USER

busybox has no USER and therefore starts as root. runAsNonRoot forbids that, but doesn't name another user.

Uid 65532For comparison – an image with its own user

notes-api runs as 65532 and starts in the same namespace with runAsNonRoot without an error. What matters is the user the image starts as.

k -n tag09-strict exec deploy/notes -- \
  grep -E '^Uid|CapEff|NoNewPrivs|Seccomp:' /proc/1/status
Uid:	65532	65532	65532	65532

Fix

Specify a numeric user. In the course the pod gets runAsUser 1000 next to runAsNonRoot – after that it starts, and id shows the new UID.
k -n tag09-strict delete pod shell --now
sed -i 's/^    runAsNonRoot: true/&\n    runAsUser: 1000/' shell.yaml
k -n tag09-strict apply -f shell.yaml
k -n tag09-strict wait --for=condition=Ready pod/shell
k -n tag09-strict exec shell -- id
pod/shell condition met
uid=1000 gid=0(root) groups=0(root)

Don't: Removing runAsNonRoot to make the pod start. In a restricted namespace admission then rejects it.

▶
Break it yourself and find it – free
CKS hands-on, day 9: Pod Security Admission with warn, audit and enforce, fixing violations and runAsNonRoot.
Go to day 9 →

FAQ

Why does Pod Security Admission let the pod through?

PSA only checks whether runAsNonRoot true is set, not the user in the image.

Related error patterns

More