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

SettingRecommendedWhy
runAsNonRoottrueRoot in container eases escapes
allowPrivilegeEscalationfalseBlocks setuid/sudo style escalation
readOnlyRootFilesystemtruePrevents tampering; write to volumes only
capabilities.drop[ALL]Removes kernel privileges not needed
seccompProfileRuntimeDefaultFilters risky syscalls
privileged / hostNetwork / hostPIDfalseDirect access to the node

Pod Security Standards

LevelAllowsUse for
privilegedEverythingSystem components (CNI, storage drivers)
baselineMost apps; blocks privileged/host accessGeneral workloads, starting point
restrictedOnly hardened podsApplication namespaces (goal)

Rollout

  1. Label namespaces with warn/audit=restricted first
  2. Fix workloads that trigger warnings
  3. Switch to enforce=baseline, then enforce=restricted
  4. Keep privileged only for system namespaces
Rolling out enforcement without breaking apps.

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 space

Enforce 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=restricted

Make 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.

More DevOps articles