← blog · November 23, 2026

Pod Security Standards and securityContext in Kubernetes: Non-Root Containers, Read-Only Filesystems and a Migration That Breaks Nothing

How to write a pod that satisfies the restricted profile, how to prepare the image, how to move Pod Security Admission from warn to enforce on an existing cluster, and why traps like named users, low ports and fsGroup appear.

Running a container as root, with the full default set of Linux capabilities and a writable root filesystem, is still the default. When a vulnerability inside the image is exploited, the attacker inherits the sum of those defaults: they can install packages, replace binaries, try privilege escalation through setuid programs, and reach a wider surface of the kernel through system calls. Kubernetes gives you two tools to shrink that surface: the securityContext fields at pod and container level, and Pod Security Admission, which makes them mandatory per namespace. This post covers how to set both up, how to migrate an existing cluster without breaking it, and the traps people hit most often.

Three levels: privileged, baseline, restricted

Pod Security Standards define three profiles. privileged imposes nothing. baseline closes the known escalation paths: privileged containers, host network and PID namespaces, hostPath mounts, dangerous capabilities. restricted goes further and asks the workload to harden itself: a non-root user, privilege escalation disabled, all capabilities dropped and a seccomp profile set.

For application workloads the target should be restricted. baseline is a useful stepping stone but not enough on its own; a container running as root with every default capability passes baseline.

A pod that satisfies restricted

The following manifest meets every requirement of the restricted profile and also adds a read-only root filesystem, which the profile does not require:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      securityContext:
        runAsNonRoot: true
        runAsUser: 10001
        runAsGroup: 10001
        fsGroup: 10001
        fsGroupChangePolicy: OnRootMismatch
        seccompProfile:
          type: RuntimeDefault
      containers:
        - name: web
          image: registry.example.com/web:1.4.2
          ports:
            - containerPort: 8080
          securityContext:
            allowPrivilegeEscalation: false
            readOnlyRootFilesystem: true
            capabilities:
              drop: ["ALL"]
          volumeMounts:
            - name: tmp
              mountPath: /tmp
      volumes:
        - name: tmp
          emptyDir: {}

Every line is there for a reason. runAsNonRoot tells the kubelet to refuse to start the container if it would run as root. allowPrivilegeEscalation: false sets the no_new_privs flag on the process, so executing a setuid binary grants nothing. drop: ["ALL"] removes the capabilities the runtime hands out by default, such as changing file ownership or opening raw sockets. The RuntimeDefault seccomp profile enables the runtime's built-in syscall filter; Kubernetes does not turn it on by itself, so if you leave it out the container runs unfiltered.

Preparing the image

The manifest alone is not enough; the image has to be able to run without root. Define the user with a numeric ID in the Dockerfile:

FROM debian:bookworm-slim
RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app
COPY --chown=10001:10001 ./bin/web /usr/local/bin/web
USER 10001:10001
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/web"]

The reason for USER 10001 instead of USER app is the first trap below.

Traps

A user defined by name. If the image says USER app and the pod has no runAsUser, the kubelet cannot verify whether that name maps to root, because it does not read the image's /etc/passwd. With runAsNonRoot enabled the container never starts and fails with a "non-numeric user" error. Use a numeric ID in the image or set runAsUser in the pod.

Low ports. A non-root process may fail to bind a port below 1024. Adding the NET_BIND_SERVICE capability often does not behave as expected, because for a non-root user an added capability does not end up in the effective set when the process starts. The cleanest fix is to listen on a high port such as 8080 and map it in the Service with port: 80 and targetPort: 8080.

Read-only root filesystem. Many applications quietly write somewhere: /tmp, cache directories, PID files, configuration generated at startup. With readOnlyRootFilesystem on, those writes fail with "read-only file system". Find every directory the process writes to and mount an emptyDir there. A practical way to find them is to run the container once with a writable filesystem and check which paths changed with docker diff or a similar tool.

fsGroup on large volumes. When fsGroup is set, the kubelet recursively changes group ownership and permissions of every file in the volume at mount time. On a volume with millions of files the pod can take minutes to start. fsGroupChangePolicy: OnRootMismatch only does this when the ownership of the volume root does not match, which fixes the problem in most cases.

The Deployment is accepted, the pods never appear. Pod Security Admission only rejects Pod objects. A non-compliant Deployment is accepted by the API, kubectl apply succeeds, and then the ReplicaSet cannot create pods. The symptom is a ready count stuck at zero; the actual error sits in the ReplicaSet events as FailedCreate. This is why you must use warn mode before enforcing.

Tools that depend on setuid. After allowPrivilegeEscalation: false, sudo, ping or any other setuid program inside the image no longer gets the privileges it expects. A production image should not need them; if it does, the image needs rethinking.

Migrating an existing cluster without breaking it

Pod Security Admission is driven by namespace labels, and each level can be applied in three modes: enforce rejects, warn returns a warning to the client, audit writes to the audit log. Migrate in this order.

First, run a server-side dry run to see which workloads would break. This does not apply the label; it evaluates the existing pods and prints warnings:

kubectl label --dry-run=server --overwrite ns --all \
  pod-security.kubernetes.io/enforce=restricted

Then put warn and audit labels on the target namespaces while keeping enforcement at baseline:

kubectl label --overwrite ns app \
  pod-security.kubernetes.io/enforce=baseline \
  pod-security.kubernetes.io/warn=restricted \
  pod-security.kubernetes.io/audit=restricted

Warn mode also applies to workload objects such as Deployments, so teams see what is missing in their own terminal during kubectl apply. Once the warnings are gone, switch enforce to restricted. You can also pin the profile version with pod-security.kubernetes.io/enforce-version; if you do not, a cluster upgrade can change what the profile checks.

Built-in admission or a policy engine

Pod Security Admission ships with every cluster, needs no installation or maintenance, and its rules are defined by the community and fixed. That is also its weakness: you cannot change the rules, exemptions are limited, and even the restricted profile does not require a read-only root filesystem or an allowed image registry.

My recommendation is to always build the baseline with Pod Security Admission and add a policy engine only for the rules it does not cover. Moving every rule into a policy engine ties basic security to the health of an extra component that must keep running. When the engine is down or misconfigured, the built-in control is still in place.

When not to choose restricted

Components that work at node level, such as network plugins, storage drivers, log collectors and node monitoring agents, genuinely need host access. Trying to squeeze them into restricted is wasted effort. The right approach is to put them in dedicated namespaces, label those namespaces explicitly as privileged, and use access control to prevent application workloads from being deployed there. The list of privileged namespaces should stay short and reviewable.

For application workloads there is rarely a valid reason to deviate from restricted. When an application insists on root, the problem is usually the image, not the security rule.