Kubernetes Security Context: 6 Proven Settings, 3 Traps
The Kubernetes security context is the part of a Pod spec that decides which user a container runs as, which Linux capabilities it keeps, whether its file system is writable and which kernel filters apply. Most guides list the fields. Fewer show what each one does to a real container, or which settings break ordinary images. I ran three Pods with different security context settings on a k3s v1.37.2 cluster (Debian 13 VM) and read the result from inside each container: id, file ownership and /proc/self/status.

Contents
- The Kubernetes security context has two levels
- runAsNonRoot: a check, not a change
- runAsUser on an image built for root
- fsGroup and volumes
- readOnlyRootFilesystem in the Kubernetes security context
- What the kernel actually applied
- Pod Security Admission enforces it
- How to roll out a stricter security context
- A Kubernetes security context that passed
The Kubernetes security context has two levels
A Kubernetes security context can be set in two places. Settings under spec.securityContext apply to the whole Pod; settings under containers[].securityContext apply to one container and win where both set the same field. The test Pod used the old post's Pod-level block and gave one of its two containers its own user:
spec:
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
runAsNonRoot: true
containers:
- name: app
securityContext:
runAsUser: 2001
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities: {drop: ["ALL"]}
seccompProfile: {type: RuntimeDefault}
- name: plain
Inside the two containers:
# kubectl exec secured-app -c app -- id
uid=2001 gid=3000 groups=2000,3000
# kubectl exec secured-app -c plain -- id
uid=1000 gid=3000 groups=2000,3000
The container-level runAsUser: 2001 replaced the Pod's 1000 only in app. Both got the primary group 3000 and the fsGroup 2000 as an extra group.
runAsNonRoot: a check, not a change
runAsNonRoot: true does not make a container run as non-root. It refuses to start one that would run as root. With the standard nginx image, which runs as root, the Pod never started:
Warning Failed 11s (x2 over 11s) kubelet spec.containers{web}: Error: container has runAsNonRoot and image will run as root (pod: "nonroot-nginx_default(e8030bae-23d9-44bc-9718-c37dbe30522c)", container: web)
The status was CreateContainerConfigError, as the old FAQ said. The fix is to give the container a user, either in the image (USER in the Dockerfile) or with runAsUser.
runAsUser on an image built for root
Adding runAsUser: 1000 to the same nginx image passed the check and then crashed:
2026/11/19 02:39:12 [emerg] 1#1: mkdir() "/var/cache/nginx/client_temp" failed (13: Permission denied)
nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (13: Permission denied)
The Pod went into CrashLoopBackOff, even though the Kubernetes security context itself was valid. The image expects root to create its cache folders. A security context cannot fix an image that needs root; use an image built to run unprivileged, or mount writable volumes at the paths it writes to. This is the most common reason a hardened manifest "breaks the app", and the place to test first.
fsGroup and volumes
The app container wrote a file to an emptyDir volume:
-rw-r--r-- 1 2001 2000 2 Nov 19 02:39 f
drwxrwsrwx 2 0 2000 4096 Nov 19 02:39 /data
The folder belongs to group 2000 with the setgid bit (s in drwxrwsrwx), so new files inherit group 2000 and every container in the Pod with that group can share them. On large persistent volumes, applying fsGroup means changing ownership of existing files, which can be slow; the Kubernetes security context task page documents fsGroupChangePolicy to control that.
readOnlyRootFilesystem in the Kubernetes security context
touch: /etc/x: Read-only file system
tmp-ok
The image's own file system was read-only, while /tmp, mounted from an emptyDir, stayed writable. Every path the application writes to (temp files, caches, PID files) needs its own volume; find them by running the container read-only in a test namespace and reading the errors.
What the kernel actually applied
The clearest test of a Kubernetes security context is /proc/self/status:
# app (hardened container)
CapEff: 0000000000000000
NoNewPrivs: 1
Seccomp: 2
# plain (no container-level settings)
CapEff: 0000000000000000
NoNewPrivs: 0
Seccomp: 0
Three lessons. First, allowPrivilegeEscalation: false sets the kernel's no_new_privs flag (NoNewPrivs: 1); without it the flag was 0. Second, seccompProfile: RuntimeDefault switched seccomp on (mode 2, filter); the plain container had none (Seccomp: 0), so on this cluster a Pod gets no seccomp filter unless you ask for it. Third, both containers had no effective capabilities, because both ran as non-root users: dropping capabilities matters most for containers that run as root, and it is still worth setting so that a later image change to root does not bring them back. A side effect of having no capabilities: ping failed in both containers with "permission denied (are you root?)".
Pod Security Admission enforces it
I labelled a namespace with pod-security.kubernetes.io/enforce=restricted and applied the same Pod. It was rejected because of the plain container:
Error from server (Forbidden): error when creating "/lab/p3.yaml": pods "secured-app" is forbidden: violates PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "plain" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "plain" must set securityContext.capabilities.drop=["ALL"]), seccompProfile (pod or container "plain" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost")
The message lists exactly the fields that were missing from the Kubernetes security context, which makes it a good checklist. The Pod Security Standards page defines the baseline and restricted levels. Put the common settings at Pod level (seccompProfile can go there) so that a new sidecar container does not fail admission like plain did.
How to roll out a stricter security context
Changing the Kubernetes security context of a running service is a deployment like any other, and the failures above show why it should be staged. Apply the new settings to one replica or a test namespace first, watch for CreateContainerConfigError, CrashLoopBackOff and permission errors in the logs, and only then roll it out. Label the namespace with the Pod Security level in warn mode before enforce, so existing workloads report what they would fail without being blocked.
A Kubernetes security context that passed
- Use images that run as non-root; then set
runAsNonRoot: trueas the safety check. - Set
allowPrivilegeEscalation: falseandcapabilities.drop: ["ALL"]on every container. - Set
seccompProfile.type: RuntimeDefault, at Pod level. - Use
readOnlyRootFilesystem: trueplusemptyDirmounts for writable paths. - Use
fsGroupfor shared volumes. - Enforce it with the
restrictedPod Security level per namespace, after testing inwarnmode.
For other gaps a default cluster leaves open (secrets, network policy, resource limits), see is Kubernetes enough for production. For how Pods, ReplicaSets and Services fit together on k3s, the Kubernetes deep dive uses the same kind of lab.
Comments
Post a Comment