All guides

securityContext: the settings every pod should carry

The Kubernetes application security checklist recommends a short list of securityContext settings. Three can be set once for the pod. Three exist only on the container and go on every container.

An open laptop on a wooden table in front of bookshelves

Six settings and what they do

The application security checklist in the Kubernetes documentation lists the first five under base hardening and the seccomp profile under advanced hardening.

  • runAsNonRoot: true requires the container to run as a user other than root. The kubelet validates the image at runtime and fails to start the container if it would run as UID 0.
  • runAsUser is the UID that runs the container's entrypoint. It defaults to the user specified in the image metadata.
  • allowPrivilegeEscalation: false is the setting that governs whether a process may gain privileges beyond its parent's. Running a setuid binary is one way a process gains them. It decides whether the no_new_privs flag is set on the container process. If the field is not defined, it defaults to true.
  • readOnlyRootFilesystem: true mounts the container's root filesystem as read-only. The default is false.
  • capabilities.drop: ["ALL"] drops all Linux capabilities. With no capabilities field, a container gets the default set granted by the container runtime. The checklist says to drop all and add back only the specific ones the container needs.
  • seccompProfile.type: RuntimeDefault applies the container runtime's default seccomp profile. If no profile is specified, the default is Unconfined, unless seccomp defaulting is enabled on the node with the kubelet flag --seccomp-default.

Pod level and container level

A pod has two places for these fields. spec.securityContext holds pod-level attributes and common container settings. securityContext on a container applies to that container. When a field is set in both, the container's value takes precedence.

  • Both levels: runAsUser, runAsGroup, runAsNonRoot, seccompProfile, seLinuxOptions, appArmorProfile, windowsOptions.
  • Pod level only: fsGroup, fsGroupChangePolicy, supplementalGroups, supplementalGroupsPolicy, sysctls, seLinuxChangePolicy.
  • Container level only: allowPrivilegeEscalation, readOnlyRootFilesystem, capabilities, privileged, procMount.

Three of the six settings exist only on the container. Set them on every container in the pod, init containers included.

One full example

YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      securityContext:
        runAsNonRoot: true
        runAsUser: 10001
        runAsGroup: 10001
        seccompProfile:
          type: RuntimeDefault
      containers:
        - name: app
          image: registry.example.com/my-app:1.0.0
          securityContext:
            allowPrivilegeEscalation: false
            readOnlyRootFilesystem: true
            capabilities:
              drop: ["ALL"]

The name, the image and the ID 10001 are choices made for this example. Use the UID and GID that your image is built for.

runAsGroup sets the primary group ID for all processes in the containers. If you omit it, the primary group ID is root (0).

Combinations that do not work

  • A root image with runAsNonRoot: true. The kubelet fails to start the container. Set runAsUser to a non-zero UID, and configure appropriate permissions on the files and directories inside the image, as the checklist says.
  • allowPrivilegeEscalation: false on a privileged container. The documentation calls the setting inconsistent with a container that runs as privileged or has the capability CAP_SYS_ADMIN. It cannot be set in combination with either.
  • A seccomp profile on a privileged container. It is not possible to apply one. Privileged containers always run as Unconfined.
  • Capability names with the CAP_ prefix. In a manifest you omit that part. To add CAP_SYS_TIME, write SYS_TIME.
  • Linux fields on Windows pods. The API reference notes for runAsUser, allowPrivilegeEscalation, readOnlyRootFilesystem, capabilities and seccompProfile that the field cannot be set when spec.os.name is windows.

How to verify a running pod

The Kubernetes task page checks its example pod, security-context-demo, with these commands:

Shell

kubectl exec -it security-context-demo -- id
kubectl exec -it security-context-demo -- sh
kubectl get pod security-context-demo -o yaml

The first prints the UID, the primary group and the supplementary groups of the process. In the shell opened by the second, cd /proc/1 and cat status print the capability bitmaps of process 1 on the CapPrm and CapEff lines. The third prints the pod object, with its status, as YAML. Replace the pod name with your own.

How to enforce the settings

The restricted Pod Security Standard requires four of the six:

  • allowPrivilegeEscalation must be false.
  • runAsNonRoot must be true, and runAsUser must not be 0.
  • The seccomp profile must be RuntimeDefault or Localhost.
  • Containers must drop ALL capabilities. The only one they may add back is NET_BIND_SERVICE.

Pod Security Admission checks that level for a namespace. With the label pod-security.kubernetes.io/warn: restricted, a violation triggers a user-facing warning and the pod is still allowed. With the enforce label, the pod is rejected.

First steps

  1. Add the pod-level and the container-level settings to the pod template of each workload.
  2. Set the warn label for restricted on a namespace and read the warnings that the API server returns.
  3. Follow the checklist item on namespaces: deploy the application into a namespace that enforces an appropriate Pod Security Standard.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides