Skip to content

InsightsSecurity

Rolling out Pod Security Admission without breaking deployments

How to move namespaces to the baseline and restricted Pod Security Standards using audit and warn modes first, what the restricted level requires of a pod, and why a Deployment can be accepted while its pods are rejected.

Published November 1, 20266 min read

A pod that runs as root, mounts the node's filesystem or asks for a privileged container is one bug away from owning the node it runs on. Kubernetes has a built-in check for this, Pod Security Admission, stable since v1.25. It is a few labels on a namespace, and it is easy to turn on in a way that blocks a deploy on a Friday afternoon.

The safe way is to measure first, enforce second. This article covers what the three levels mean, how the three modes let you do that, what a compliant pod looks like, and the gaps that surprise teams.

Levels and modes

Pod Security Admission applies one of three Pod Security Standards, called levels, in one or more modes. Both are set with labels on the namespace.

LevelWhat it allows
privilegedEverything. No restrictions.
baselineBlocks the known privilege escalations: privileged containers, host namespaces (hostNetwork, hostPID, hostIPC), hostPath volumes, most added capabilities, and an explicit Unconfined seccomp profile
restrictedBaseline plus current hardening practice: non-root, no privilege escalation, all capabilities dropped, a seccomp profile set, and a short list of allowed volume types
ModeWhat happens on a violation
enforceThe pod is rejected
auditThe request is allowed and an annotation is added to the audit log event
warnThe request is allowed and the client receives a warning

The labels follow the pattern pod-security.kubernetes.io/<mode>: <level>, with an optional pod-security.kubernetes.io/<mode>-version that pins the policy to the version shipped with a Kubernetes minor release, or latest. A namespace can set any combination, and each mode can use a different level.

Pin the version on enforce. With latest, a cluster upgrade can tighten the policy at the same moment as the control plane changes, and you lose the ability to tell which change broke a deploy.

Step 1: see what would break

Audit and warn never block anything, so turn them on everywhere first. Warn is the useful one for developers, because the message appears in their kubectl apply output or CI log.

audit and warn at baseline on every namespace
kubectl label --overwrite ns --all \
  pod-security.kubernetes.io/audit=baseline \
  pod-security.kubernetes.io/warn=baseline

Audit and warn also apply to workload resources such as Deployments, Jobs and CronJobs, by checking their pod templates. A violating Deployment produces a warning when it is applied, not when its pods are eventually created.

Before setting enforce, ask the API server what would happen to what is already running. A server-side dry run evaluates existing pods against the new level and returns violations as warnings without changing the label:

dry-run enforcement across the cluster
kubectl label --dry-run=server --overwrite ns --all \
  pod-security.kubernetes.io/enforce=baseline

Run it for restricted as well and keep the output as your backlog. The same warnings appear when you add or change an enforce label for real, so the dry run is how you avoid learning from the real one.

Platform namespaces are the likely offenders. CNI plugins, CSI drivers, node agents and log collectors legitimately need host access, so some namespaces will stay at privileged. That is fine if it is a deliberate, listed decision and not the default.

Step 2: make workloads compliant

Baseline is usually cheap: most applications already avoid privileged containers and host mounts. Restricted is where the work is. A pod that satisfies it looks like this:

restricted-compliant pod
apiVersion: v1
kind: Pod
metadata:
  name: orders
spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 10001
    seccompProfile:
      type: RuntimeDefault
  containers:
    - name: app
      image: registry.example.com/orders:1.4.2
      securityContext:
        allowPrivilegeEscalation: false
        capabilities:
          drop: ["ALL"]
      ports:
        - containerPort: 8080

The common failures, in rough order of frequency:

  • Image runs as root. runAsNonRoot: true makes the kubelet refuse to start a container whose image user is root. Fix the image with a numeric USER (see container image hygiene), or set runAsUser to a non-zero value.
  • No seccomp profile. Restricted requires RuntimeDefault or Localhost to be set explicitly. Leaving it unset is a violation.
  • allowPrivilegeEscalation not set to false. It must be set on every container, init containers included.
  • Capabilities not dropped. Restricted requires drop: ["ALL"]. The only capability it allows you to add back is NET_BIND_SERVICE, which matters if an app binds to a port below 1024. Listening on 8080 instead is simpler.
  • Disallowed volume types. Restricted allows configMap, csi, downwardAPI, emptyDir, ephemeral, persistentVolumeClaim, projected and secret. A hostPath or an in-tree volume type outside that list fails.
  • Helm charts and operators. Third-party charts often expose these settings under a values key. Check for them before forking a chart.

If the application writes to its own filesystem, a non-root user cannot do that to image paths owned by root. Mount an emptyDir at the directory it writes to, or set fsGroup, rather than going back to root.

Step 3: enforce, with warnings still on

When the dry run is clean, enforce baseline and keep warning at restricted. This gives you a floor that blocks the dangerous cases, and a visible nudge toward the next level:

namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: payments
  labels:
    pod-security.kubernetes.io/enforce: baseline
    pod-security.kubernetes.io/enforce-version: v1.37
    pod-security.kubernetes.io/audit: restricted
    pod-security.kubernetes.io/audit-version: v1.37
    pod-security.kubernetes.io/warn: restricted
    pod-security.kubernetes.io/warn-version: v1.37

Use the minor version of your own cluster. Move a namespace to enforce: restricted once its warnings have stayed at zero through a few deploys.

To find namespaces nobody has labelled yet:

namespaces with no enforce level
kubectl get namespaces --selector='!pod-security.kubernetes.io/enforce'

What surprises people

Enforce applies to pods, not to Deployments. A Deployment that violates the enforced level is accepted by the API server, and the failure shows up later as a ReplicaSet that cannot create pods. The symptom is a rollout that never progresses, with the reason in the ReplicaSet's events. This is why warn and audit on the pod template matter: they catch it at apply time.

Setting a label does not touch running pods. Existing pods keep running, even if they would now be rejected. The violation surfaces when they are rescheduled, after a node drain or a cluster upgrade. Treat the dry-run output as a list of future incidents, and see PodDisruptionBudgets and safe node drains for why a drain is when this appears.

Namespace labels are an access-control surface. Whoever can edit a namespace can lower its level. Restrict namespace updates with RBAC, and consider a policy engine rule that rejects changes to the pod-security.kubernetes.io/ labels outside the platform team.

Exemptions skip everything. Cluster-wide defaults and exemptions are set in the API server's admission configuration, which managed Kubernetes services often do not let you edit. Where you can, set defaults so unlabelled namespaces are not privileged:

admission-config.yaml
apiVersion: apiserver.config.k8s.io/v1
kind: AdmissionConfiguration
plugins:
  - name: PodSecurity
    configuration:
      apiVersion: pod-security.admission.config.k8s.io/v1
      kind: PodSecurityConfiguration
      defaults:
        enforce: "baseline"
        enforce-version: "latest"
        audit: "restricted"
        audit-version: "latest"
        warn: "restricted"
        warn-version: "latest"
      exemptions:
        usernames: []
        runtimeClasses: []
        namespaces: []

The file is passed to kube-apiserver with --admission-control-config-file. An exempt user, runtime class or namespace is ignored entirely: no enforcement, no audit, no warning. The Kubernetes documentation advises against exempting controller service accounts such as the ReplicaSet controller, because that implicitly exempts anyone who can create the matching workload.

Make it the default for new namespaces

A rollout that only covers today's namespaces decays. Apply the labels in whatever creates namespaces: the golden path, the Terraform module or the GitOps repository. Then a new namespace starts at baseline with restricted warnings, and the exception, not the rule, needs a justification. This pairs well with default-deny NetworkPolicies, which close the network side of the same problem.

Rollout checklist

  1. Set audit and warn to baseline on every namespace, then to restricted, and collect the warnings.
  2. Dry-run enforce at each level and keep the output as a backlog.
  3. Fix images and manifests: non-root user, seccomp RuntimeDefault, no privilege escalation, capabilities dropped.
  4. Decide, and write down, which platform namespaces stay privileged.
  5. Enforce baseline with a pinned version, keep warning at restricted, then promote namespaces one at a time.
  6. Restrict who can edit the Pod Security labels.
  7. Put the labels into namespace creation so new namespaces start labelled.