Kubernetes Pod Security: Security Contexts and Pod Security Standards
Harden pods with securityContext (non-root, read-only filesystem, dropped capabilities, seccomp) and enforce baseline/restricted Pod Security Standards.
Published December 2, 2026
Containers are not virtual machines: a container running as root with extra privileges can often escape to the node. Pod security settings remove those privileges, and Pod Security Admission enforces a minimum standard across namespaces so insecure pods are rejected before they run.
The concept
A pod or container securityContext controls the process's privileges: runAsNonRoot and runAsUser to avoid root, allowPrivilegeEscalation: false to block setuid escalation, readOnlyRootFilesystem to make the image immutable at runtime, dropping Linux capabilities (drop: [ALL]) and adding back only what is needed, and a seccomp profile (RuntimeDefault) to filter dangerous system calls.
Pod Security Standards define three levels: privileged (no restrictions), baseline (blocks known privilege escalations such as privileged containers and host namespaces) and restricted (hardened best practices, requiring non-root and dropped capabilities). Pod Security Admission, built into Kubernetes, enforces them per namespace through labels, with modes for enforce, audit and warn.
Hardened securityContext
| Setting | Recommended | Why |
|---|---|---|
| runAsNonRoot | true | Root in container eases escapes |
| allowPrivilegeEscalation | false | Blocks setuid/sudo style escalation |
| readOnlyRootFilesystem | true | Prevents tampering; write to volumes only |
| capabilities.drop | [ALL] | Removes kernel privileges not needed |
| seccompProfile | RuntimeDefault | Filters risky syscalls |
| privileged / hostNetwork / hostPID | false | Direct access to the node |
Pod Security Standards
| Level | Allows | Use for |
|---|---|---|
| privileged | Everything | System components (CNI, storage drivers) |
| baseline | Most apps; blocks privileged/host access | General workloads, starting point |
| restricted | Only hardened pods | Application namespaces (goal) |
Rollout
- Label namespaces with warn/audit=restricted first
- Fix workloads that trigger warnings
- Switch to enforce=baseline, then enforce=restricted
- Keep privileged only for system namespaces
Practical examples
A restricted-compliant container
Most web apps run fine like this.
securityContext:
runAsNonRoot: true
runAsUser: 10001
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities: { drop: [ALL] }
seccompProfile: { type: RuntimeDefault }
volumeMounts:
- { name: tmp, mountPath: /tmp } # writable scratch spaceEnforce per namespace with labels
Pod Security Admission reads these labels.
kubectl label namespace shop \
pod-security.kubernetes.io/enforce=restricted \
pod-security.kubernetes.io/warn=restricted \
pod-security.kubernetes.io/audit=restrictedMake images run as non-root
Set a numeric user in the Dockerfile.
FROM node:22-slim
WORKDIR /app
COPY --chown=10001:10001 . .
USER 10001
CMD ["node", "server.js"]Common mistakes
- Running containers as root because the image defaults to it.
- Granting privileged: true to fix a permission error.
- readOnlyRootFilesystem without providing writable volumes for temp files.
- Enforcing restricted everywhere at once and breaking system components.
Key takeaways
- securityContext removes unnecessary privileges.
- Run as non-root, drop all capabilities, use RuntimeDefault seccomp.
- Pod Security Admission enforces baseline/restricted per namespace.
- Roll out with warn/audit before enforce.
FAQ
What replaced PodSecurityPolicy?
PodSecurityPolicy was removed in Kubernetes 1.25; Pod Security Admission and policy engines like Kyverno or Gatekeeper replace it.
Do I need a policy engine?
For custom rules (allowed registries, required labels), Kyverno or OPA Gatekeeper complement Pod Security Admission.
Why does my app fail with read-only filesystem?
It writes to the container filesystem (often /tmp or a cache directory); mount an emptyDir volume there.