Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 127Kubernetes · Security

How to apply Pod Security Standards to a namespace

A Pod that runs as root, with privileged mode or host mounts, can take over the node. Kubernetes has a built-in guard: Pod Security Admission with three levels — privileged, baseline, restricted — switched on per namespace with labels. This how-to tests a namespace against restricted first, turns it on, and fixes a Pod so it passes.

  • 5 Lessons
  • 1 Commands

$ run as a normal user# run as root (or with sudo)

127.1

The quick answer

kubectl label --dry-run=server --overwrite ns shop pod-security.kubernetes.io/enforce=restricted
kubectl label --overwrite ns shop pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/enforce-version=latest pod-security.kubernetes.io/warn=restricted pod-security.kubernetes.io/audit=restricted
kubectl get ns shop --show-labels

The first line is a dry run: it shows which running Pods would break, and changes nothing.

127.2

Step by step

  1. The three levels:
LevelAllows
privilegedEverything (only for system namespaces like CNI, storage drivers).
baselineBlocks the worst: privileged, host network/PID, host paths, dangerous capabilities.
restrictedAlso: must run as non-root, drop ALL capabilities, no privilege escalation, a seccomp profile. The goal for apps.
  1. Three modes (labels): enforce (refuse), warn (warning to the user), audit (note in the audit log). Start with warn + audit, then add enforce.
  2. Dry run (quick answer) — fix the Pods it lists first.
  3. **A Pod spec that passes restricted:**
deployment.yaml (part)
    spec:
      securityContext:
        runAsNonRoot: true
        runAsUser: 10001
        seccompProfile:
          type: RuntimeDefault
      containers:
        - name: api
          image: registry.example.com/shop/api:1.4.2
          securityContext:
            allowPrivilegeEscalation: false
            readOnlyRootFilesystem: true
            capabilities:
              drop: ["ALL"]

The image must run as a non-root user (a USER in the Dockerfile). readOnlyRootFilesystem is not required by restricted but good practice (mount emptyDir for /tmp).

  1. Turn it on (quick answer) and watch the warnings when people deploy:
kubectl apply -f deployment.yaml -n shop
Warning: would violate PodSecurity "restricted:latest": allowPrivilegeEscalation != false …
  1. Defaults for new namespaces: add the labels in your namespace template / GitOps repo; or set a cluster-wide default in the API server's admission config. For more rules (no latest, signed images) add Kyverno.

127.3

Check that it worked

A non-compliant Pod is refused:

kubectl run test -n shop --image=nginx:1.27
Error from server (Forbidden): pods "test" is forbidden: violates PodSecurity "restricted:latest": allowPrivilegeEscalation != false …

…and the fixed Deployment runs.

127.4

Common problems

ProblemCauseFix
Deployment created, but no PodsThe ReplicaSet's Pods are refused (the Deployment itself is allowed).kubectl describe rs / events show the PodSecurity error; fix the spec.
runAsNonRoot but the container runs as rootThe image has no USER.Set runAsUser, or fix the Dockerfile (USER 10001).
The app needs to write filesreadOnlyRootFilesystem: true.Mount emptyDir volumes where it writes.
A system tool (CNI, CSI) breaksIt needs privileges.Keep its namespace privileged; protect it with RBAC.
nginx official image fails restrictedIt starts as root and binds port 80.Use nginxinc/nginx-unprivileged (port 8080).
Labels set, nothing happensOld cluster (< 1.25) or PSA disabled.Check the version; use Kyverno policies instead.

127.5

Go further

1How do you see which running Pods would break, before enforcing restricted?
2Which settings does a container need to pass restricted?

Run as non-root, allowPrivilegeEscalation: false, drop ALL capabilities, and a seccomp profile (RuntimeDefault).

All commands in this chapter