study.

Run as Non-root with an Explicit Writable Path · CKAD

Run only the required work without root privileges or a writable root filesystem.

← CKAD

Learn with progress and quizzes

First, understand the concepts.

User and permissions

runAsUser sets the process UID; runAsNonRoot rejects root execution. Review privilege escalation and capabilities separately.

Read-only root filesystem

readOnlyRootFilesystem restricts writes to the container root filesystem. Supply a separate volume for temporary files the application needs.

Requests and limits

Requests describe scheduling needs, while limits constrain runtime usage. CPU limiting and memory exhaustion have different consequences; choose values for the application.

Why it matters

An app can run without root and with a read-only root filesystem while retaining a deliberate temporary write path. Design restrictions and required behavior together.

Work through the reasoning

Verify the execution user

runAsUser specifies a UID; runAsNonRoot requires non-root execution. Check the actual identity with id and compare volume file ownership.

Separate writes from resources

readOnlyRootFilesystem does not make every separately mounted volume read-only. Requests serve placement while limits constrain execution resources; configure each for its purpose.

Learn by doing.

Use kubectl, a personal practice Kubernetes cluster, and a POSIX shell. Each lab uses its own namespace. Use example values only in Secrets. Cleanup removes the entire practice namespace.

Create a Pod running as UID 10001 and write results only under /work. Set fsGroup to provide group access to the emptyDir volume.

kubectl config current-context
kubectl create namespace study-ckad-security --dry-run=client -o yaml | kubectl apply -f -
kubectl apply -n study-ckad-security -f - <<'YAML'
apiVersion: v1
kind: Pod
metadata:
  name: worker
spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 10001
    runAsGroup: 10001
    fsGroup: 10001
  containers:
  - name: worker
    image: busybox:1.36
    command: ["sh", "-c", "id; echo scratch-ok > /work/result; sleep 3600"]
    securityContext:
      allowPrivilegeEscalation: false
      readOnlyRootFilesystem: true
      capabilities: {drop: ["ALL"]}
    resources:
      requests: {cpu: 10m, memory: 16Mi}
      limits: {cpu: 100m, memory: 32Mi}
    volumeMounts:
    - {name: scratch, mountPath: /work}
  volumes:
  - name: scratch
    emptyDir: {}
YAML
kubectl wait --for=condition=Ready pod/worker -n study-ckad-security --timeout=120s
kubectl logs worker -n study-ckad-security
kubectl exec worker -n study-ckad-security -- cat /work/result
kubectl exec worker -n study-ckad-security -- sh -c 'echo denied > /root-write-test' || true

How to check your result

Expect uid=10001 and a readable /work/result. Writing directly under root should fail. For volume permission errors inspect fsGroup and mounts; for Pending inspect requests and available resources.

Clean up practice resources

Run after checking the result. Confirm these resources belong to your practice run and contain no data you need to keep.

kubectl delete namespace study-ckad-security

Apply what you learned

The app writes cache to /tmp and fails after enabling read-only root. What minimal change could help?

Reveal the reasoning after trying

If temporary writes are required, mount a writable emptyDir at the intended path and align UID and permissions. Narrow the writable area before making the entire root filesystem writable again.

Ready to check your understanding?

How can an app write temporary files with a read-only root filesystem?

  1. Run the whole container privileged
  2. Mount a writable volume at the required path
  3. Remove every security setting
  4. Print Secrets to logs
Reveal the reasoning after trying

Mount a writable volume at the required path

A dedicated writable mount meets the app’s needs while retaining the root filesystem restriction.

Official documentation · Run as Non-root with an Explicit Writable Path