Learn Labs
State & Security

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:

kind: Namespacemetadata:name: shoplabels:pod-security.kubernetes.io/enforce: restrictedreject violating podspod-security.kubernetes.io/warn: restrictedand warn on apply
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

spec:automountServiceAccountToken: falseno API token in the podsecurityContext:runAsNonRoot: truerunAsUser: 10001also USER 10001 in the DockerfileseccompProfile: { type: RuntimeDefault }block unusual syscallscontainers:- securityContext:allowPrivilegeEscalation: falseno setuid tricksreadOnlyRootFilesystem: truecan't modify its own codecapabilities: { drop: ["ALL"] }
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 directory

An 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.

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:

kind: Rolenamespaced; ClusterRole for cluster-widemetadata: { name: pod-reader, namespace: shop }rules:- apiGroups: [""]resources: ["pods"]verbs: ["get", "list", "watch"]---kind: RoleBindingsubjects: [{ kind: ServiceAccount, name: monitor, namespace: shop }]roleRef: { kind: Role, name: pod-reader, apiGroup: rbac.authorization.k8s.io }
kubectl auth can-i list secrets -n shop --as=system:serviceaccount:shop:monitor
no

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

  1. Try to run a privileged pod in shop, then in labs. What's the difference, and what should labs enforce?
  2. Create the monitor service account and Role above, and test it with kubectl auth can-i --list --as=....
  3. Remove readOnlyRootFilesystem and write a file into /app. Then put it back and give the app a writable /tmp with an emptyDir.

On this page