Skip to content
Tutorials Center
My learning

Topics

Lesson 48 of 78 ยท Security

Pod Security and Security Contexts

Lock down pods with securityContext: runAsNonRoot, dropped capabilities, seccomp and read-only filesystems, then enforce the Baseline and Restricted levels with Pod Security Admission.

A container is not a virtual machine. It is a normal Linux process with some isolation around it, sharing the node's kernel with every other container. If the process inside is compromised, the damage depends on what the process was allowed to do: which user it runs as, which Linux capabilities it holds, whether it can write to its own filesystem, and whether it can ask the kernel for dangerous system calls.

Kubernetes lets you set all of this per pod and per container in a field called securityContext. A second mechanism, Pod Security Admission, lets cluster administrators require safe settings in a namespace so that nobody deploys a risky pod by accident. This lesson covers both. The Linux background (users, capabilities, no-new-privileges) is in Non-Root Users and Capabilities in the Docker tutorial; here you see how Kubernetes expresses the same ideas.

Where securityContext lives

There are two places. Settings in spec.securityContext apply to the whole pod, and settings in spec.containers[].securityContext apply to one container and override the pod-level value when both exist. Some fields exist only at one level (for example fsGroup is pod-level, capabilities is container-level).

YAML
apiVersion: v1
kind: Pod
metadata:
  name: hardened
spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 10001
    runAsGroup: 10001
    fsGroup: 10001
    seccompProfile:
      type: RuntimeDefault
  containers:
    - name: app
      image: example/api:2.3
      securityContext:
        allowPrivilegeEscalation: false
        readOnlyRootFilesystem: true
        capabilities:
          drop: ["ALL"]
      volumeMounts:
        - name: tmp
          mountPath: /tmp
  volumes:
    - name: tmp
      emptyDir: {}

The hardening checklist

Not root runAsNonRoot and a numeric runAsUser.
No escalation allowPrivilegeEscalation false.
Drop capabilities Drop ALL, add back only what is needed.
Read-only root Writable paths only through volumes.
1 / 2

The important fields

Run as non-root. runAsNonRoot: true makes the kubelet refuse to start a container whose image would run as UID 0. It checks the numeric user, so the image must declare a numeric USER (such as 10001) or you must set runAsUser. A named user cannot be verified and fails with an error like container has runAsNonRoot and image has non-numeric user.

Filesystem group. fsGroup makes Kubernetes change the group ownership of mounted volumes to that GID and add the group to the process, so a non-root user can write to a persistent volume.

Privilege escalation. allowPrivilegeEscalation: false sets the Linux no_new_privs flag. A process then cannot gain more privileges than its parent, which stops setuid binaries from helping an attacker. It is always true when a container is privileged or has CAP_SYS_ADMIN.

Capabilities. Root is split into capabilities. The runtime gives containers a default set, and the secure pattern is to drop everything and add back only what is truly needed:

YAML
capabilities:
  drop: ["ALL"]
  add: ["NET_BIND_SERVICE"]   # only if you must bind a port below 1024

Read-only root filesystem. readOnlyRootFilesystem: true means the application cannot modify its own files. Malware cannot drop a binary, and a bug cannot corrupt the image layer. If the app needs scratch space, mount an emptyDir at /tmp (as in the manifest above) or another specific path.

Seccomp. The kernel exposes hundreds of system calls, and most programs use only a few dozen. A seccomp profile filters the rest. type: RuntimeDefault applies the container runtime's built-in profile, which blocks calls such as mount and reboot. It is a cheap, big win, and it is required by the Restricted level. Unconfined (the old default in Kubernetes) turns the filter off. Localhost loads a custom profile from the node.

Dangerous switches. privileged: true gives the container nearly everything the host has. hostNetwork, hostPID, hostIPC and hostPath volumes break the isolation between container and node. Only system components such as CNI plugins and node agents should ever need them.

Pod Security Admission

Writing a good securityContext is a convention; Pod Security Admission (PSA) makes it a rule. PSA is a built-in admission controller that checks every pod against one of three predefined Pod Security Standards:

  • Privileged: anything goes. For system namespaces like kube-system.
  • Baseline: prevents known privilege escalations while staying easy to adopt. It forbids privileged containers, host namespaces, host ports and hostPath volumes, extra capabilities beyond a short safe list, and an Unconfined seccomp profile.
  • Restricted: the hardened profile. On top of Baseline it requires allowPrivilegeEscalation: false, runAsNonRoot: true (and not user 0), dropping ALL capabilities (only NET_BIND_SERVICE may be added back), a seccomp profile of RuntimeDefault or Localhost, and limits the volume types to safe ones such as configMap, secret, emptyDir, projected, downwardAPI, persistentVolumeClaim and CSI ephemeral volumes.

You choose the level with labels on the namespace. Each label has a mode that says what happens on a violation:

YAML
apiVersion: v1
kind: Namespace
metadata:
  name: shop
  labels:
    pod-security.kubernetes.io/enforce: baseline
    pod-security.kubernetes.io/enforce-version: latest
    pod-security.kubernetes.io/warn: restricted
    pod-security.kubernetes.io/audit: restricted
  • enforce: the pod is rejected.
  • warn: the pod is accepted, but the user gets a warning in kubectl.
  • audit: the pod is accepted and the violation is written to the audit log.

A smooth rollout is to start with warn and audit at the level you want, fix the workloads that complain, and only then set enforce. Because PSA checks the pod template of Deployments through warn and audit, you learn about problems early. You can try a label without changing anything with a server-side dry run, a read-only command here:

CONSOLE
$ kubectl label --dry-run=server --overwrite ns shop \
    pod-security.kubernetes.io/enforce=restricted
Warning: existing pods in namespace "shop" violate the new PodSecurity enforce level "restricted:latest"
Warning: api-7d9c5-x2k4p: allowPrivilegeEscalation != false, unrestricted capabilities, runAsNonRoot != true, seccompProfile
namespace/shop labeled (server dry run)

For rules that PSA cannot express (specific registries, required labels), see Admission Control and Policy.

A checker you can run

Here is a stand-in for what PSA does. The function reads a pod manifest as a Python dictionary and returns the violations for Baseline and Restricted. It follows the main rules of the standards, but it is a teaching model: the real list has more checks (AppArmor, SELinux, sysctls, proc mount type and so on).

Python A mini Pod Security Standards checker

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output shows the typical staircase. The hardened pod passes Restricted. A "plain" pod that only names an image is fine under Baseline but fails Restricted in several ways, which is why many official images need a small securityContext before they can run in a strict namespace. The risky agent fails even Baseline, so it needs a Privileged namespace.

The second stand-in plays the part of the namespace labels and the three modes:

Python Enforce, warn and audit modes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Setting runAsNonRoot: true with an image whose USER is a name. Use numeric IDs.
  • Turning on readOnlyRootFilesystem and then watching the app crash because it writes to /tmp, /var/cache or a log directory. Mount emptyDir volumes for those paths.
  • Forgetting that fsGroup and runAsUser interact: a non-root user needs group access to mounted volumes.
  • Granting privileged to "make it work" instead of finding the one capability that is missing. Check the application's error, then add only that capability.
  • Applying enforce: restricted to a namespace before checking existing workloads. Start with warn.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A container's securityContext sets runAsUser: 2000, and the pod-level securityContext sets runAsUser: 1000. Which user does the container run as?

    Container-level securityContext fields take precedence over pod-level fields of the same name.
  2. 2. Which set of settings is the usual secure pattern for capabilities?

    Start from zero capabilities and add back individually. Adding ALL or running privileged does the opposite.
  3. 3. A namespace has enforce: baseline and warn: restricted. A pod that satisfies Baseline but not Restricted is created. What happens?

    Baseline is enforced, so the pod is allowed. The warn label points at Restricted, which the pod fails, so the user sees a warning.
  4. 4. Why does readOnlyRootFilesystem: true often come with an emptyDir mounted at /tmp?

    The root filesystem is locked, so any path the app must write to needs a writable volume mounted over it.
  5. 5. Which Pod Security level requires allowPrivilegeEscalation: false and a RuntimeDefault seccomp profile?

    Restricted adds these hardening requirements on top of Baseline. Baseline only blocks known dangerous settings.

Summary

  • securityContext sets the user, group, capabilities, privilege escalation, filesystem and seccomp behavior of pods (pod level) and containers (container level, which wins).
  • The hardened recipe: non-root numeric user, allowPrivilegeEscalation: false, drop ALL capabilities, read-only root filesystem and seccompProfile: RuntimeDefault.
  • Avoid privileged, host namespaces, host ports and hostPath volumes outside system components.
  • Pod Security Admission enforces the Privileged, Baseline or Restricted standard per namespace with enforce, warn and audit labels.
  • Roll out gradually: warn and audit first, enforce when workloads are clean.

Next: rules that go beyond built-in levels, in Admission Control and Policy.