Sub topic 12
Enforcing Pod security standards per namespace
This chapter assumes you can run kubectl, read YAML, and already know what Pods and namespaces are. It covers Pod Security Admission (PSA), the built-in admission controller that checks Pods against the Pod Security Standards using labels on a namespace.
What PSA does
Pod Security Admission inspects each Pod at creation time and compares its security settings with one of three standard profiles. You choose the profile and the reaction by labeling the namespace. No extra objects are needed.
The three profiles are:
| Profile | Meaning |
|---|---|
| privileged | No restrictions. Allows known privilege escalations. |
| baseline | Blocks known escalations, such as host namespaces and privileged containers. Works for most ordinary workloads. |
| restricted | Follows current hardening practice. Requires running as non-root, dropping capabilities, and a seccomp profile. |
Choosing a mode
Each profile can be applied in one of three modes. The label key has the form pod-security.kubernetes.io/<mode>.
| Mode | Effect on a violating Pod |
|---|---|
| enforce | The API server rejects the Pod. |
| audit | The Pod is allowed, and a violation is recorded in the audit log. |
| warn | The Pod is allowed, and the user sees a warning. |
You can set several modes at once. A common rollout is warn and audit at restricted, with enforce at baseline. That way you see what would break before you tighten the rules.
Labeling a namespace
You can set the labels in the namespace manifest.
apiVersion: v1
kind: Namespace
metadata:
name: team-a
labels:
team: a
pod-security.kubernetes.io/enforce: baselineYou can also label an existing namespace.
kubectl label namespace team-a \
pod-security.kubernetes.io/warn=restricted \
pod-security.kubernetes.io/audit=restrictedTo pin the profile to a Kubernetes version, add pod-security.kubernetes.io/enforce-version. Its value is either latest or a version such as v1.30. Pinning keeps the rules stable when you upgrade the cluster.
Testing the effect
With enforce: baseline on team-a, a privileged Pod is rejected.
kubectl run bad --image=nginx --namespace team-a \
--overrides='{"spec":{"containers":[{"name":"bad","image":"nginx","securityContext":{"privileged":true}}]}}'The API server returns a violates PodSecurity "baseline:latest" error that names the offending field. The same Pod in a namespace without the label is admitted.
Common errors
- Existing Pods are not evicted. Enforcement happens at admission. Changing a label does not stop Pods that are already running. Only new Pods are checked.
- Workload controllers hide the error. If you create a Deployment, the Deployment itself is accepted. The ReplicaSet then fails to create Pods, and you see the rejection in the ReplicaSet events. In
warnandauditmodes, controllers such as Deployment also get checked against the Pod template. - Typos in label values. An invalid level or mode value makes PSA fall back to its configured defaults and log a warning. Check the spelling.
- Label on the wrong object. The labels go on the Namespace, not on the Pod.
Where it fits
PSA limits what a Pod may ask for. It does not limit traffic (NetworkPolicy), API access (RBAC), or consumption (ResourceQuota). Together these controls make a namespace a useful team boundary, a topic the last chapter evaluates.
Exercise
A namespace has the labels enforce=baseline and warn=restricted. You create a Pod that runs as root but is otherwise unprivileged. What happens?
Show answer
The Pod is admitted, because running as root does not violate the baseline profile. It does violate restricted, so kubectl prints a warning. Nothing blocks it.
Exercise
You add pod-security.kubernetes.io/enforce=restricted to a namespace that already has running Pods that would violate it. Are the Pods stopped?
Show answer
No. PSA only checks Pods at creation. The running Pods continue, but a restart or reschedule that creates a replacement Pod will be rejected. To preview this risk, use warn and audit first.