$ run as a normal user# run as root (or with sudo)
The quick answer
kubectl label --dry-run=server --overwrite ns shop pod-security.kubernetes.io/enforce=restricted
kubectl label --overwrite ns shop pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/enforce-version=latest pod-security.kubernetes.io/warn=restricted pod-security.kubernetes.io/audit=restricted
kubectl get ns shop --show-labelsThe first line is a dry run: it shows which running Pods would break, and changes nothing.
Step by step
- The three levels:
| Level | Allows |
|---|---|
privileged | Everything (only for system namespaces like CNI, storage drivers). |
baseline | Blocks the worst: privileged, host network/PID, host paths, dangerous capabilities. |
restricted | Also: must run as non-root, drop ALL capabilities, no privilege escalation, a seccomp profile. The goal for apps. |
- Three modes (labels):
enforce(refuse),warn(warning to the user),audit(note in the audit log). Start withwarn+audit, then addenforce. - Dry run (quick answer) — fix the Pods it lists first.
- **A Pod spec that passes
restricted:**
spec:
securityContext:
runAsNonRoot: true
runAsUser: 10001
seccompProfile:
type: RuntimeDefault
containers:
- name: api
image: registry.example.com/shop/api:1.4.2
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]The image must run as a non-root user (a USER in the Dockerfile). readOnlyRootFilesystem is not required by restricted but good practice (mount emptyDir for /tmp).
- Turn it on (quick answer) and watch the warnings when people deploy:
kubectl apply -f deployment.yaml -n shop
Warning: would violate PodSecurity "restricted:latest": allowPrivilegeEscalation != false …- Defaults for new namespaces: add the labels in your namespace template / GitOps repo; or set a cluster-wide default in the API server's admission config. For more rules (no
latest, signed images) add Kyverno.
Check that it worked
A non-compliant Pod is refused:
kubectl run test -n shop --image=nginx:1.27
Error from server (Forbidden): pods "test" is forbidden: violates PodSecurity "restricted:latest": allowPrivilegeEscalation != false ……and the fixed Deployment runs.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Deployment created, but no Pods | The ReplicaSet's Pods are refused (the Deployment itself is allowed). | kubectl describe rs / events show the PodSecurity error; fix the spec. |
runAsNonRoot but the container runs as root | The image has no USER. | Set runAsUser, or fix the Dockerfile (USER 10001). |
| The app needs to write files | readOnlyRootFilesystem: true. | Mount emptyDir volumes where it writes. |
| A system tool (CNI, CSI) breaks | It needs privileges. | Keep its namespace privileged; protect it with RBAC. |
nginx official image fails restricted | It starts as root and binds port 80. | Use nginxinc/nginx-unprivileged (port 8080). |
| Labels set, nothing happens | Old cluster (< 1.25) or PSA disabled. | Check the version; use Kyverno policies instead. |
Go further
- Command pages: kubectl.
- Next how-tos: Enforce a Kubernetes policy with Kyverno, Write a small and safe Dockerfile, Write a Kubernetes NetworkPolicy.
- Learn it: CKS — Pod Security levels, CKS — Labels, CKS — A restricted Pod, CKS — Fix a rejected Pod.
- Practise: Lab: enforce the restricted Pod Security Standard.
1How do you see which running Pods would break, before enforcing restricted?
2Which settings does a container need to pass restricted?
Run as non-root, allowPrivilegeEscalation: false, drop ALL capabilities, and a seccomp profile (RuntimeDefault).