⚡ ~/naveed K8s Hub
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
Phase 3 — Security Module 17 of 24 Free & Open Access

Kubernetes Pod & Workload Security

Pod security limits what a container can do on its node. A securityContext sets the user, filesystem and Linux capabilities, and Pod Security Admission enforces the privileged, baseline or restricted standard per namespace. This module covers runAsNonRoot, read-only root filesystems and dropping capabilities.

Last updated: · By Naveed Ahmed, Certified Kubernetes Administrator

17 - Pod & Workload Security

Why this matters

Even a hardened cluster is exposed if any pod can run privileged, mount the host filesystem, or escalate to root. This is the layer that stops container breakout.

Read this first — Definitions & Explanations

Run as non-root

Containers should not run as UID 0 unless absolutely required. Reduces blast radius if compromised.

Pod Security Admission (PSA)

Built-in admission that enforces Pod Security Standards profiles (privileged, baseline, restricted) per namespace.

allowPrivilegeEscalation: false

Prevents a process from gaining more privileges than its parent (important hardening flag).

readOnlyRootFilesystem

Makes container root filesystem read-only so malware can’t easily write binaries to disk (app must support it).

Capabilities / seccomp / AppArmor

Drop Linux capabilities you don’t need; use seccomp/AppArmor profiles to restrict syscalls and behavior.

Official docs (read for detail)

Key Concepts

YouTube search terms

Hands-on lab (on prod-sim)

# Enforce restricted PSA on a namespace
kubectl create namespace locked-down
kubectl label namespace locked-down \
  pod-security.kubernetes.io/enforce=restricted \
  pod-security.kubernetes.io/enforce-version=latest

# Try to run a privileged pod — should be REJECTED outright
kubectl run priv --image=nginx -n locked-down --overrides='
{"spec":{"containers":[{"name":"priv","image":"nginx","securityContext":{"privileged":true}}]}}'
# expect: Error from server (Forbidden): violates PodSecurity "restricted:latest"

# Try a compliant pod
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: safe-pod
  namespace: locked-down
spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 1000
    seccompProfile:
      type: RuntimeDefault
  containers:
  - name: safe-pod
    image: nginx
    securityContext:
      allowPrivilegeEscalation: false
      readOnlyRootFilesystem: true
      capabilities:
        drop: ["ALL"]
    ports:
    - containerPort: 8080
EOF
kubectl get pod safe-pod -n locked-down   # should succeed (nginx may still fail readOnlyRootFS at runtime — that's the point, dig into why)

# Compare: same "safe-pod" spec in default (privileged) namespace works, but in `locked-down` you had to think about every setting
kubectl describe pod safe-pod -n locked-down | grep -A10 Events

# hostPath / hostNetwork abuse demo (should also be blocked under restricted)
kubectl run hostnet --image=nginx -n locked-down --overrides='{"spec":{"hostNetwork":true,"containers":[{"name":"hostnet","image":"nginx"}]}}'

Common mistakes and how to avoid them

  • Setting runAsNonRoot: true on an image that runs as root. The container fails with CreateContainerConfigError because the kubelet verifies the user at start. Set runAsUser to a non-zero ID or fix the image's USER.
  • Dropping all capabilities and breaking the app. drop: ["ALL"] is correct, but a process binding port 80 then needs NET_BIND_SERVICE. Add back only the capability you need, or listen on a port above 1024.
  • Enabling readOnlyRootFilesystem without writable mounts. Applications that write temp files crash immediately. Mount an emptyDir at /tmp and at any other path the app writes.
  • Expecting Pod-level securityContext to cover everything. Some fields are container-level only: allowPrivilegeEscalation, capabilities, privileged and readOnlyRootFilesystem. Set them on each container.
  • Using privileged: true to fix a permission error. It grants near-complete access to the host. Find the specific capability or volume permission that is missing and grant only that.

Quick answers

What does allowPrivilegeEscalation: false do?

It stops a process from gaining more privileges than its parent, which blocks setuid binaries and similar tricks from raising rights inside the container. The restricted Pod Security Standard requires it, so set it on every container even when the app never needs extra privileges.

What are the three Pod Security Standards?

Privileged is unrestricted. Baseline blocks known escalations such as privileged containers, host networking and hostPath volumes. Restricted adds hardening: run as non-root, drop all capabilities, forbid privilege escalation and use a seccomp profile. Pod Security Admission applies one level per namespace through labels.

Why does a Pod fail with a runAsNonRoot error?

The kubelet checks the container's user before starting it. If runAsNonRoot is true and the image runs as root or declares no numeric user, container creation fails with CreateContainerConfigError. Set runAsUser to a non-zero ID or change the image's USER.

Notes

(fill in your own words after watching + labbing)

📋 Self-Assessment Mastery Checklist (4 Competencies)
🧠 Practice Exam Questions (Module 17 MCQs)
⚡ Take Quiz & Save Progress in Tracker

Review these sample exam questions out loud, test your retrieval, and then unlock official scoring in the interactive tracker.

Question 1: Running as non-root is a:
  • A. Pod/container security best practice
  • B. Requirement to use Services
  • C. Way to bypass NetworkPolicy
  • D. Helm-only feature
✓ Correct Answer: A (Pod/container security best practice)
Option A ('Pod/container security best practice') is the standard production architectural best practice.
Question 2: Pod Security Admission (PSA) can enforce:
  • A. baseline/restricted-style pod security standards
  • B. etcd compaction
  • C. Ingress class names
  • D. StorageClass provisioners
✓ Correct Answer: A (baseline/restricted-style pod security standards)
Option A ('baseline/restricted-style pod security standards') is the standard production architectural best practice.
Question 3: allowPrivilegeEscalation: false helps prevent:
  • A. Gaining more privileges than the parent process
  • B. DNS lookups
  • C. PVC binding
  • D. Service creation
✓ Correct Answer: A (Gaining more privileges than the parent process)
Option A ('Gaining more privileges than the parent process') is the standard production architectural best practice.
Question 4: readOnlyRootFilesystem makes the container root FS:
  • A. Read-only (when supported/configured)
  • B. Encrypted
  • C. Shared across nodes
  • D. Faster always
✓ Correct Answer: A (Read-only (when supported/configured))
Option A ('Read-only (when supported/configured)') is the standard production architectural best practice.
Question 5: seccomp/AppArmor profiles can:
  • A. Restrict syscalls/behaviors of containers
  • B. Replace RBAC
  • C. Provision PVs
  • D. Scale Deployments
✓ Correct Answer: A (Restrict syscalls/behaviors of containers)
Option A ('Restrict syscalls/behaviors of containers') is the standard production architectural best practice.
← Previous Module (16) Cluster Hardening & CIS Benchmarks Next Module (18) → Supply Chain Security & Admission Control