Interview's MateKubernetes
Interview's Mate

Kubernetes

K8s · Workloads · GitOps · Cloud

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Kubernetes topics
Lesson 107/ 160
06. Security, RBAC & Governance

SecurityContext: runAsNonRoot, readOnlyRootFilesystem, Dropping Capabilities

Harden pods with non-root users, read-only roots, and dropped Linux capabilities.

IntermediaterunAsNonRoot · readOnlyRootFilesystem · Capabilities3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

SecurityContext gives a worker a numbered helmet, removes dangerous tools, and locks the main toolbox while leaving one approved scratch desk. The simple Kubernetes rule is: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.

Think of it like…

SecurityContext gives a worker a numbered helmet, removes dangerous tools, and locks the main toolbox while leaving one approved scratch desk.

Where you’ve seen it

During an access review, the security engineer runs kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status. Expected evidence: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities. If the run instead shows this symptom, stop and restore the last known state: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling.

Words you’ll hear

runAsNonRoot
For this lesson, runAsNonRoot is the controlling idea: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
readOnlyRootFilesystem
readOnlyRootFilesystem is the checkpoint; run kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status and inspect the owner chain. An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling.
Capabilities
Capabilities is the outcome to preserve; the cost is explicit: Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files.
Verification evidence
The evidence command is kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status. It connects the declaration to this behavior: Harden pods with non-root users, read-only roots, and dropped Linux capabilities.

Visual diagram

Loading interactive diagram

Theory & deep dive

How runAsNonRoot reaches the intended state

Harden pods with non-root users, read-only roots, and dropped Linux capabilities. A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.

The concrete checkpoint is kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status; compare the runAsNonRoot owner, readOnlyRootFilesystem state, and Capabilities outcome instead of treating YAML acceptance as proof.

Verify readOnlyRootFilesystem, not only object creation

Run kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status after the change and correlate the result with Events and the owning object. A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.

Key takeaway: runAsNonRoot is implemented only when this invariant remains observable: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.

readOnlyRootFilesystem: failure boundary and production cost

An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling. The prerequisite contract is concrete: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.

Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files. A safe design limits blast radius, records the last good configuration, and uses kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status as a deterministic rollback checkpoint.

Recover from readOnlyRootFilesystem drift

Capture the current state with kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status, then correct the controlling manifest rather than patching a child object; the relevant divergence signal is specific: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling.

Key takeaway: Accept the trade-off consciously: Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files.

How it works, step by step

Step 1 of 4
Identity owner

1. Model the trust boundary

Name the identity, action, resource, and trust boundary before choosing a control: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.

runAsNonRoot contract → owned state
Key numbers at this step
Goal
Harden pods with non-root users, read-only roots, and dropped Linux capabilities.
Owner
runAsNonRoot

When to use it & trade-offs

What it is

Harden pods with non-root users, read-only roots, and dropped Linux capabilities. A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.

Why it matters: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling. The invariant at risk is specific: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities. The quickest fact check before changing the design is kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status.

When to reach for it

  • Use runAsNonRoot to enforce this invariant: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
  • Use readOnlyRootFilesystem evidence from kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status in reviews and incident timelines. Harden pods with non-root users, read-only roots, and dropped Linux capabilities.
  • Choose the hosted security service route only when this cost is justified by governance or scale: Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files.

When to avoid or be careful

  • Avoid assuming runAsNonRoot is complete after YAML submission; the required behavior is concrete: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
  • Avoid treating the next symptom as random; check the owner and state with kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status first: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling.
  • Avoid adopting a heavier platform wrapper before measuring its cost: Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files.

Native, manual, and managed paths for runAsNonRoot

Admission and least privilege
Best for: Harden pods with non-root users, read-only roots, and dropped Linux capabilities. A concrete incident check is kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status.

Strengths: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.

Watch out: Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files. Recovery must also recognize this signal: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling.

Runtime-only detection
Best for: Quickly testing the readOnlyRootFilesystem assumption with kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status; the expected invariant is: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.

Strengths: kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status gives immediate feedback without first building a platform layer.

Watch out: A manual change can hide drift, and unlike runAsNonRoot, it does not make this symptom self-documenting: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling.

Hosted security service
Best for: Teams that need Capabilities integrated with guardrails; the evidence path is kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status.

Strengths: Platform controls can wrap this exact behavior in policy or dashboards: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.

Watch out: The added platform ownership buys value at this cost: Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files.

Interview signals & numbers to know

When this topic comes up

  • Interview anchor: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
  • Trace runAsNonRoot to Capabilities through readOnlyRootFilesystem; the invariant is: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
  • Recognize the symptom: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling.
  • Justify the cost: Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files.
  • Verify with kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status instead of only inspecting YAML.

Benchmarks & rules of thumb

0 -
UID is unacceptable for runAsNonRoot workloads
3 -
controls to inspect: identity, filesystem, and capabilities
1 -
readOnlyRootFilesystem does not make mounted volumes immutable

Common mistakes & follow-ups

Common interview mistakes

  • Debugging only the child object misses the runAsNonRoot owner contract: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
  • Ignoring this telltale condition leads to unrelated changes: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling.
  • Repeating kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status without an owner for remediation wastes retry cost. Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files.

Likely interviewer follow-ups

  • How would API output and runtime evidence prove this behavior: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities?
  • Which owner object, logs, and Events would you inspect for this symptom-An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling-?
  • At what threshold would this cost become unacceptable: Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files?
Interview tip: Interview sequence: state the invariant, run kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status, name the failure, then justify the cost. Invariant: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities. Failure: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling. Cost: Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply SecurityContext: runAsNonRoot, readOnlyRootFilesystem, Dropping Capabilities with generative AI interview Q&A and hands-on practice.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Pod Security Standards (Privileged, Baseline, Restricted) & Pod Security Admission (PSA)
Next topic
Seccomp & AppArmor Profiles in Pod Specs