Pod Security Standards and Security Contexts: Hardening the Workload Boundary

Pod security standards in Kubernetes: privileged, baseline and restricted levels, admission labels, securityContext and a safe rollout.

Executive Summary: A pod spec can ask for nearly anything: root, privileged mode, the host’s network, host filesystem mounts. securityContext is where you declare how a pod should run, and Pod Security Standards are three predefined levels (privileged, baseline and restricted) that the built-in Pod Security Admission controller can enforce per namespace with a single label. Target restricted for application namespaces, roll it out with warn and audit before enforce, and add a policy engine when you need rules beyond the standard levels.
Isometric three-step staircase of red, amber and green blocks for privileged, baseline and restricted with a shield

Anyone who can create a pod with privileged: true, or mount the host’s root filesystem with hostPath, effectively has root on the node, and from there access to every other pod on it. In the RBAC article, pod creation rights already looked powerful. Pod security controls decide how much of that power a pod spec can actually claim.

A security context defines privilege and access settings for a pod or container: which user it runs as, whether it can escalate privileges, which Linux capabilities it has, whether its filesystem is read-only and which seccomp profile applies. These map directly onto the Docker hardening flags from container security hardening. Pod Security Admission (PSA), built into the API server and stable since Kubernetes 1.25, checks pods against the Pod Security Standards before they are created. It replaced PodSecurityPolicy, which was removed in that same release.

The three levels

Level Allows Use for
privileged Everything System namespaces: CNI, storage drivers, node agents
baseline Blocks known privilege escalations: privileged containers, host namespaces, hostPath volumes, host ports, dangerous capabilities A minimum for any namespace; easy to adopt for most workloads
restricted Baseline plus: must run as non-root, no privilege escalation, drop all capabilities, a seccomp profile, limited volume types Application namespaces

Enabling it: namespace labels

apiVersion: v1
kind: Namespace
metadata:
  name: billing
  labels:
    pod-security.kubernetes.io/enforce: restricted
    pod-security.kubernetes.io/enforce-version: v1.30
    pod-security.kubernetes.io/warn: restricted
    pod-security.kubernetes.io/audit: restricted
  • enforce rejects pods that violate the level.
  • warn allows them but returns a warning to the user, visible in kubectl apply output.
  • audit allows them and records an annotation in the API server audit log.

Pinning enforce-version prevents a cluster upgrade from tightening the rules under you without notice. One detail surprises people: enforcement applies to pods, not to Deployments. A Deployment that produces violating pods is accepted, and its ReplicaSet then fails to create pods, which shows up as events on the ReplicaSet rather than an error on apply. The warn mode does report on Deployments and other workload objects, which is another reason to keep it on.

A restricted-compliant pod spec

apiVersion: apps/v1
kind: Deployment
metadata:
  name: billing-api
  namespace: billing
spec:
  template:
    spec:
      automountServiceAccountToken: false
      securityContext:                    # pod level: applies to all containers
        runAsNonRoot: true
        runAsUser: 10001
        runAsGroup: 10001
        fsGroup: 10001                    # group ownership for mounted volumes
        seccompProfile:
          type: RuntimeDefault
      containers:
        - name: api
          image: ghcr.io/acme/billing-api:2.0.0
          securityContext:                # container level
            allowPrivilegeEscalation: false
            readOnlyRootFilesystem: true
            capabilities:
              drop: ["ALL"]
          volumeMounts:
            - name: tmp
              mountPath: /tmp
      volumes:
        - name: tmp
          emptyDir:
            sizeLimit: 64Mi

What each setting does:

  • runAsNonRoot makes the kubelet refuse to start a container that would run as UID 0. It can only verify numeric users, so images should use a numeric USER, or set runAsUser explicitly as here.
  • fsGroup makes mounted volumes group-owned by that group, solving most “permission denied” errors on persistent volumes for non-root containers.
  • seccompProfile: RuntimeDefault applies the container runtime’s default system call filter. Kubernetes does not apply it unless asked, which surprises people coming from Docker, where it is the default.
  • allowPrivilegeEscalation: false blocks gaining privileges through setuid binaries.
  • capabilities.drop: ["ALL"] removes every Linux capability. The restricted level only allows adding back NET_BIND_SERVICE.
  • readOnlyRootFilesystem is not required by the restricted level but is a strong addition; give the app emptyDir volumes for the paths it must write.

Container-level settings override pod-level ones where both exist. Putting user and seccomp settings at the pod level keeps init and sidecar containers covered too.

Rolling it out without breaking things

  1. Dry run. kubectl label --dry-run=server --overwrite ns billing pod-security.kubernetes.io/enforce=restricted reports which existing pods would violate the level, without changing anything.
  2. Warn and audit first. Set warn and audit to restricted and enforce to baseline on every namespace. Developers see warnings during deploys; nothing breaks.
  3. Fix images and charts. Most failures are images that run as root or write to their own filesystem, and Helm charts without security contexts.
  4. Enforce restricted namespace by namespace, keeping privileged only for system namespaces that need it, such as those running CNI or storage agents.

Beyond the standard levels

Pod Security Standards are deliberately fixed and simple. For organization-specific rules, such as only allowing images from your registry, requiring resource limits, requiring certain labels or verifying image signatures, use a policy engine:

  • ValidatingAdmissionPolicy, built into the API server and generally available in Kubernetes 1.30, expresses rules in CEL without running any extra components.
  • Kyverno writes policies as Kubernetes YAML and can also mutate resources (for example adding a default security context) and verify image signatures.
  • OPA Gatekeeper uses the Rego policy language and is common where OPA is already used elsewhere.

Looking further ahead, user namespace support for pods, set with hostUsers: false, reached beta in Kubernetes 1.30. It maps root inside a pod to an unprivileged user on the node, which limits the impact of container escapes considerably once it is widely available.

Frequently Asked Questions

What are Kubernetes Pod Security Standards?

Three predefined security levels for pods: privileged, which allows everything; baseline, which blocks known privilege escalations; and restricted, which also requires non-root users, no privilege escalation, dropped capabilities and a seccomp profile. The built-in Pod Security Admission controller enforces them per namespace.

What replaced PodSecurityPolicy?

Pod Security Admission, which enforces the Pod Security Standards through namespace labels, became stable in Kubernetes 1.25, the same release that removed PodSecurityPolicy. Policy engines such as Kyverno, OPA Gatekeeper or ValidatingAdmissionPolicy cover custom rules.

What is a securityContext in Kubernetes?

A section of the pod or container spec that sets privilege and access controls, such as the user and group to run as, whether privilege escalation is allowed, Linux capabilities, a read-only root filesystem and the seccomp profile.

Why are my pods not being created after enforcing restricted?

Pod Security Admission rejects violating pods, not the Deployments that create them, so the error appears in the ReplicaSet’s events. Run kubectl describe on the ReplicaSet to see which requirement failed, and use warn mode to catch violations at apply time.

Make the safe spec the default spec

Pod Security Standards give every namespace a clear, enforceable baseline with one label, and a restricted-compliant security context is only a dozen lines. Put that block in your Helm charts and templates, roll enforcement out through warnings first, and add a policy engine for the rules that are specific to your organization. The official documentation is worth keeping open: the Pod Security Standards documentation and the securityContext task guide. Related reading on this site: RBAC and ServiceAccounts.

Originally posted on 30 July 2024 and post last modified on 30 July 2024

Container Security DevOps Kubernetes Pod Security

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *