Sub topic 9
Rolling out safely with server-side dry run
This chapter assumes you can use kubectl and write namespace and pod manifests. It also assumes you know the three Pod Security Standards profiles and the enforce, audit, and warn namespace labels from earlier chapters.
Why a rollout needs a plan
Setting pod-security.kubernetes.io/enforce=restricted on a busy namespace can break things. Existing pods keep running, because enforcement only checks pods when they are created or updated. Any pod that is later rescheduled, restarted by a controller, or scaled up is checked against the new level and may be rejected. The failure shows up hours or days after the label change, which makes it hard to trace.
The fix is to learn which workloads would fail before you enforce anything.
Step 1: Test with a server-side dry run
A server-side dry run sends the label change to the API server, which evaluates it and then discards it. Nothing is modified.
kubectl label --dry-run=server --overwrite ns --all \
pod-security.kubernetes.io/enforce=restrictedFor each namespace with pods that would violate the level, the API server returns a warning like this:
Warning: existing pods in namespace "payments" violate the new PodSecurity enforce level "restricted:latest"
Warning: web-7d9f8c5b6-x2k4p: allowPrivilegeEscalation != false, unrestricted capabilities, runAsNonRoot != true, seccompProfile
namespace/payments labeled (server dry run)Read each warning as a to-do list. It names the pod and the controls it fails.
To check one namespace against a different level, drop --all and name it:
kubectl label --dry-run=server --overwrite ns payments \
pod-security.kubernetes.io/enforce=baselineThe dry run only inspects pods that exist now. It does not read the pod templates inside Deployments or Jobs, so a Deployment scaled to zero will not appear. Treat the result as a lower bound.
Step 2: Turn on warn and audit first
Next, apply warn and audit at the target level while keeping enforce at a level that is already safe. Nothing is rejected, but problems become visible:
apiVersion: v1
kind: Namespace
metadata:
name: payments
labels:
pod-security.kubernetes.io/enforce: baseline
pod-security.kubernetes.io/enforce-version: latest
pod-security.kubernetes.io/audit: restricted
pod-security.kubernetes.io/warn: restrictedThe two modes report in different places:
| Mode | Where the violation appears | Who sees it |
|---|---|---|
| `warn` | A warning in the `kubectl` response when a pod, Deployment, or Job is created or updated | The person applying the manifest |
| `audit` | An annotation on the audit log event | Operators who read the API server audit log |
audit only helps if audit logging is enabled on the API server. On managed clusters, check that your provider exposes those logs.
Because warn also fires for pod templates in Deployments, it catches workloads that the dry run missed.
Step 3: Fix workloads, then enforce
Update each flagged workload with the securityContext fields you learned earlier. Re-apply it and confirm that the warn messages are gone. Then run the dry run again. When it prints no warnings, raise enforce:
kubectl label --overwrite ns payments \
pod-security.kubernetes.io/enforce=restrictedCommon errors
- Trusting a dry run on a namespace with scaled-down workloads. Their templates are not checked.
- Forgetting
--overwrite. Without it,kubectl labelfails on a namespace that already has the label. - Enabling
auditwithout audit logging, then seeing nothing. - Jumping from no policy straight to
restricted. Move throughbaselinefirst if many workloads fail.
Exercise
You run the server-side dry run for enforce=restricted on the shop namespace and see no warnings. A Deployment there is scaled to zero replicas. Is it safe to enforce Restricted? Why or why not?
Show answer
Not yet. The dry run checks only existing pods, and a Deployment scaled to zero has none. Its pod template may still violate Restricted and fail the first time it scales up. Set warn=restricted first and apply or update the Deployment to see warnings for its template. Alternatively, scale it up in a test environment.