☸️Basic Kubernetes Security Labs

🎯 Objective

Learn to secure containers in Kubernetes using securityContext, Pod Security Admission, and runtime profiles (seccomp, AppArmor).


🧰 Prerequisites

  • Kubernetes cluster (Minikube, kind, or real)

  • kubectl configured

  • Cluster admin privileges

  • Optional tools: kube-bench, kubescape, trivy


🔹 Lab 1: Run Containers as Non-Root

Step 1: Create the pod spec

# non-root-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: non-root-demo
spec:
  containers:
  - name: app
    image: busybox
    command: ["sh", "-c", "id && sleep 3600"]
    securityContext:
      runAsUser: 1000
      allowPrivilegeEscalation: false

Step 2: Apply and check logs

✅ Expected: User ID is 1000, not root.


🔹 Lab 2: Drop Linux Capabilities

✅ Expected: ping fails due to dropped capabilities.


🔹 Lab 3: Use Read-Only Root Filesystem

✅ Expected: Cannot write to root filesystem.


🔹 Lab 4: Apply Seccomp and AppArmor (if supported)

✅ Expected: Pod uses restricted syscall set.


🔹 Lab 5: Enforce Policies with Pod Security Admission (PSA)

Step 1: Create secure namespace

Create a restricted policy in your own namespace.

First get the name of your namespace:

If your namespace would be named afa01-vm-0-ns then this is the command for this (replace with your own namespace first)

You may check that the label has been added correctly using:

Step 2: Try applying an insecure pod

❌ Expected: Pod creation denied by PSA.

Step 3: Clean up and remove label again

To be able to create unrestricted pods again just remove the label

You may check that the label has been removed correctly using:


✅ Wrap-Up

  • ✅ Ran containers as non-root

  • ✅ Dropped capabilities

  • ✅ Used read-only filesystems

  • ✅ Applied seccomp and AppArmor profiles

  • ✅ Used Pod Security Admission to block insecure pods


Last updated