Skip to main content
Security & multi-tenancyLesson 4 of 13

Pod Security Standards and securityContext

If a Pod runs as root (the admin account) or borrows node privileges, one breached container can put the whole node at risk. Let's follow where a namespace's security standard stops a risky Pod, and what a developer must fix to get it through.

Reject level (enforce)None
Submit result—
Running Pods0
Inside the API serverDeveloperkubectl applyPod specYAMLAPI serverauthN · authZAdmissionPod security checkshopsecurity labelsetcdcluster storekubeletruns containerswebPodRead level
RequestResponse / reportWatchAppears in a later step

What you will learn

  • privileged vs baseline vs restricted
  • What the enforce, audit and warn modes do
  • Why rejections are less visible with a Deployment
  • Threats blocked by four securityContext fields

What each level blocks, and securityContext fields​

ItemWhat it blocksWhat happens when you apply it
baselinePrivileged containers, sharing node network or processes, mounting node pathsMost regular applications pass as is
restrictedbaseline + running as root, privilege escalation, kernel capabilities, missing seccompImages built to run as root are rejected or fail to start
runAsNonRootGaining node root after a container escapeImages built to run as root fail to start
readOnlyRootFilesystemChanging executables or planting toolsYou must attach a separate writable volume for temp files
capabilities.dropAbuse of special privileges like network config or file ownership changesUsing a port below 1024 needs one capability added back
allowPrivilegeEscalation: falseGaining privileges at runtimeUsually no impact

The full set of API server gates (authentication, authorization, admission) is covered in How an API Request Gets Stored.

Try it yourself​

kubectl get namespaces -L pod-security.kubernetes.io/enforce,pod-security.kubernetes.io/warn # Level and mode per namespace
kubectl run psa-test --image=nginx --privileged -n <namespace> --dry-run=server # baseline rejects it; nothing is stored
kubectl get events -n <namespace> --field-selector reason=FailedCreate # Deployed but no Pods: rejection events
kubectl get pod <pod> -n <namespace> -o jsonpath='{.spec.containers[*].securityContext}' # Current securityContext
kubectl label --dry-run=server --overwrite namespace <namespace> pod-security.kubernetes.io/enforce=restricted # Which Pods break before raising enforce

Check yourself

  1. Namespace shop has enforce: baseline. A developer with permission to create Pods submits a Pod with privileged: true. What happens?
  2. The same privileged spec is submitted as a Deployment. The rollout looks successful but there are zero Pods. Where is the cause?
  3. A Pod fixed with runAsNonRoot: true passes admission but fails to start. What is the most likely cause?

Further reading: Pod Security Standards · Pod Security Admission · Configure a security context for a Pod or container