Security
Pod Security Admission, security contexts, service accounts, and RBAC
A container is a process on a shared kernel. If it runs as root, with extra capabilities, a writable filesystem, and an API token it doesn't need, then one vulnerability in the app goes a long way. Kubernetes has a layer for each of those.
Pod Security Admission
The shop namespace enforces the restricted profile:
kubectl -n shop run rooty --image=nginx:1.30.5
Error from server (Forbidden): pods "rooty" is forbidden: violates PodSecurity "restricted:latest":
allowPrivilegeEscalation != false (container "rooty" must set securityContext.allowPrivilegeEscalation=false),
unrestricted capabilities (container "rooty" must set securityContext.capabilities.drop=["ALL"]),
runAsNonRoot != true (pod or container "rooty" must set securityContext.runAsNonRoot=true),
seccompProfile (pod or container "rooty" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost")The API server rejected the pod before it existed. The three profiles are
privileged (anything), baseline (blocks known escalations: host
namespaces, privileged containers) and restricted (current best practice).
Admission checks pods when they're created, so existing pods aren't evicted
when you tighten a namespace; use warn and audit modes first.
The web pod's security context
kubectl -n shop exec deploy/web -- sh -c 'id; touch /app/x; ls /var/run/secrets/kubernetes.io'
uid=10001 gid=0(root) groups=0(root)
touch: cannot touch '/app/x': Read-only file system
ls: cannot access '/var/run/secrets/kubernetes.io': No such file or directoryAn attacker with code execution in this container can't write to the image, can't gain privileges, and has no credentials for the Kubernetes API.
readOnlyRootFilesystem breaks apps that write temporary files, caches
or PID files to the image's filesystem. Give them an emptyDir volume
mounted at exactly the paths they need (/tmp, a cache directory)
rather than dropping the setting.
Service accounts and RBAC
Every pod runs as a service account; by default its token is mounted into the pod, and anything in the pod can use it against the API server. Most apps never call the API, so this module turns the mount off.
For pods that do need the API (operators, CI runners, the labs), RBAC grants exactly what's needed:
kubectl auth can-i list secrets -n shop --as=system:serviceaccount:shop:monitor
noThe labs in this module use an admin kubeconfig, which is fine for a
lab and never for automation. Watch for the permissions that are
effectively admin: create pods (mount any Secret, run as any service
account in the namespace), list secrets, escalate/bind on roles,
nodes/proxy, and anything with *.
The rest of the list
- Images: pin versions (this module pins every image), scan them, pull from registries you control, and consider signature verification.
- NetworkPolicies: default deny (NetworkPolicies).
- Secrets: encryption at rest, external secret stores, narrow RBAC (Configuration).
- The API server: not on the public internet; audit logging on.
Lab
- Try to run a privileged pod in
shop, then inlabs. What's the difference, and what shouldlabsenforce? - Create the
monitorservice account and Role above, and test it withkubectl auth can-i --list --as=.... - Remove
readOnlyRootFilesystemand write a file into/app. Then put it back and give the app a writable/tmpwith anemptyDir.