Appearance

Security contexts and Pod Security

Run a container with fewer privileges and verify that it can still write where the application needs to.

What you will understand

  • Explain how a security context configures process identity and container privileges.
  • Mount a writable directory for a process while keeping the root filesystem read-only.
  • Use Pod Security Admission to check a manifest before a Pod starts.

Builds on: Authentication, RBAC, and ServiceAccounts

Choose what the application process can do

A report worker needs to write a status file to /work. It has no reason to alter system files or gain root privileges. A security context lets you set the Linux identity and privileges under which a Pod or container runs. The Pod can set defaults such as a user and group ID. Container-level settings govern details such as Linux capabilities and privilege escalation.

The example runs as user and group 1000. It drops all Linux capabilities, which are individual privileges processes can otherwise hold. allowPrivilegeEscalation: false prevents the process from gaining more privileges through operations such as running a set-user-ID program. The runtime default seccompProfile limits available system calls. Toggle the settings in the scene and watch which parts of the process boundary they change.

Give the process one directory it can write to

The process can run as a non-root user on a read-only root filesystem while a separate writable mount handles its output.

  1. Set the process UID and group

    • Container processUID/GID 1000; non-root
    • Kernel privilegesdrop ALL; no privilege escalation
    • Seccomp profileRuntimeDefault syscall filter
    • Root filesystemMounted read-only
    • Work volume/work; group-accessible emptyDir

    The process runs as UID 1000. runAsNonRoot checks that it is not configured to execute as root.

  2. Drop unneeded capabilities

    Dropping capabilities and disabling privilege escalation reduce privilege paths independently of the UID choice.

  3. Keep the root filesystem read-only

    The root filesystem is read-only, so an application writing there should receive an error rather than mutate its executable environment.

  4. Mount /work with suitable permissions

    A separately mounted emptyDir provides /work with suitable group permissions; read-only root does not override this mount.

Linux node with RuntimeDefault seccomp and an emptyDir volume; not a complete hostile-code sandbox.

Inspect YAML or commands

Choose process identity

runAsNonRoot: true
runAsUser: 1000

Protect the image filesystem

readOnlyRootFilesystem: true
Explore another case

Give the process a writable working directory

Save the manifest as restricted-writer.yaml. Its root filesystem is read-only, but the container mounts an emptyDir volume at /work. A writable mount on the right directory lets the process create its status file. fsGroup: 1000 provides group-related access on supported volumes. It is worth testing the exact process UID and path because a mount that exists can still be inaccessible to the app.

apiVersion: v1
kind: Pod
metadata:
  name: restricted-writer
spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 1000
    runAsGroup: 1000
    fsGroup: 1000
    seccompProfile:
      type: RuntimeDefault
  containers:
    - name: writer
      image: busybox:1.36
      command: ["sh", "-c", "id; echo ready > /work/status; sleep 3600"]
      securityContext:
        allowPrivilegeEscalation: false
        readOnlyRootFilesystem: true
        capabilities:
          drop: [ALL]
      volumeMounts:
        - name: work
          mountPath: /work
  volumes:
    - name: work
      emptyDir: {}

Test the namespace’s Pod security policy

Pod Security Standards define three levels of constraints called Privileged, Baseline, and Restricted. Pod Security Admission enforces, warns, or audits against a chosen level in a namespace. Label a disposable namespace to enforce Restricted at version v1.37. Apply the passing example, then read its status file and verify the process identity. To observe an admission denial, try a disposable copy with an unsafe field such as privileged: true using server-side dry run. Read the rejection to see which requirement it violated.

# Use a new disposable namespace with no production workloads.
kubectl create namespace security-practice
kubectl label namespace security-practice \
  pod-security.kubernetes.io/enforce=restricted \
  pod-security.kubernetes.io/enforce-version=v1.37
kubectl apply -n security-practice -f restricted-writer.yaml
kubectl exec -n security-practice restricted-writer -- cat /work/status
kubectl exec -n security-practice restricted-writer -- id

Which Pod does Restricted admission accept?

Namespace policy can reject a proposed Pod before it reaches the scheduler. Check an unsafe and a compliant proposal against the same policy.

  1. Choose the namespace policy

    Namespace labels choose the Restricted standard and version for enforcement; the labels themselves require controlled ownership.

  2. Evaluate the Pod proposal

    The privileged container violates Restricted requirements. Authentication and RBAC can succeed before this separate admission rejection.

  3. Observe a validation denial

    Enforcement rejects the violating Pod, so this Pod never becomes a node execution request.

  4. Admit the compliant Pod

    After using compliant fields, a new proposal can persist and reach the node. Runtime permissions still need actual testing.

Pod Security Admission is enabled with no exemption for the training namespace or identity.

Explore another case

Use the workload security lab to check access controls from the workload’s point of view.

Separate admission errors from runtime errors

Passing admission means that the manifest met the selected checks. The application can still fail after it starts if a required cache or temporary directory is read-only. Conversely, a runtime failure writing a file gives you no evidence that Pod Security Admission rejected the Pod. Verify the Pod exists, then check its runtime identity, mounts, and permissions. A namespace’s security labels do not evict previously admitted Pods.

Check your understanding

The Pod was admitted, but the worker gets a permission error writing to /tmp. What would you inspect before lifting the read-only setting?

Check the worker’s expected write paths and which volumes are mounted. If the process genuinely needs writable temporary space, mount it at /tmp with permissions the process can use, then verify the write.

Primary documentation

Go to the source

Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise

Search the book

Search loads when you open it.

Keyboard shortcuts

Ctrl + K / ⌘ + K
Search the book
?
Open this guide
Tab / Shift + Tab
Move between controls
Enter / Space
Activate a focused button or disclosure
← / →
Focused visual timeline · previous or next step
Home / End
Focused visual timeline · first or last point
Esc
Close the open dialog or menu