Sub topic 8
Deployments, Jobs, and pod templates
This chapter assumes you know Deployments, Jobs, and Namespaces, and that you have seen the pod-security.kubernetes.io namespace labels from the previous chapter. It covers a behavior that often surprises people. Pod Security Admission (PSA) checks Pods, but you usually create Deployments and Jobs.
Enforce checks Pods only
In enforce mode, PSA evaluates Pod objects. It does not evaluate a Deployment, Job, or StatefulSet. The API server accepts a workload object even if its pod template violates the namespace policy. The controller for that workload then tries to create Pods, and PSA rejects them.
This is the failure sequence:
- You apply a Deployment. The API server accepts it.
- The Deployment controller creates a ReplicaSet.
- The ReplicaSet controller tries to create a Pod.
- PSA rejects the Pod with a
Forbiddenerror. - The ReplicaSet records the error and keeps retrying.
Seeing the failure
Take the payments namespace from earlier, which enforces baseline. This Deployment asks for a privileged container.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: payments
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: app
image: busybox:1.36
command: ["sleep", "3600"]
securityContext:
privileged: trueApply it and check the rollout:
kubectl apply -f web.yaml
kubectl get deployment web -n payments
kubectl describe replicaset -n payments -l app=webkubectl apply succeeds, and the Deployment stays at 0/1 ready. The describe output shows an event like this:
Error creating: pods "web-5d8f7c9b6-" is forbidden: violates PodSecurity "baseline:latest": privileged (container "app" must not set securityContext.privileged=true)The ReplicaSet also gets a ReplicaFailure condition. Always look at the ReplicaSet events, not only the Deployment, when a rollout stalls in a PSA-enforcing namespace.
Warn and audit see pod templates
The warn and audit modes do look at workload objects. They evaluate the pod template inside these resources:
- Deployment
- ReplicaSet and ReplicationController
- StatefulSet and DaemonSet
- Job and CronJob
When you apply the Deployment above to a namespace with warn: restricted, kubectl prints a warning right away:
Warning: would violate PodSecurity "restricted:latest": privileged (container "app" must not set securityContext.privileged=true), allowPrivilegeEscalation != false (container "app" must set securityContext.allowPrivilegeEscalation=false), ...The audit mode adds a pod-security.kubernetes.io/audit-violations annotation to the audit event. This gives you early feedback at apply time, before any Pod is rejected. It is one reason the Topic's example namespace sets warn and audit to a stricter level than enforce.
Jobs and CronJobs
A Job behaves like a Deployment here. The Job object is accepted, the Job controller fails to create Pods, and the error appears as events on the Job. The Job keeps retrying, so it does not fail fast. A CronJob creates a Job on each schedule, so a violating CronJob can look healthy for hours and fail only when it first runs.
Fix the template, not the Pod
Pods created by controllers are owned by the workload. Editing a running Pod does not help, because the controller recreates it from the template. Put the Restricted fields in spec.template.spec:
spec:
securityContext:
runAsNonRoot: true
runAsUser: 1000
seccompProfile:
type: RuntimeDefault
containers:
- name: app
image: busybox:1.36
command: ["sleep", "3600"]
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop: ["ALL"]Once you apply the corrected Deployment, the ReplicaSet retries and the Pod is admitted.
Common errors
| Symptom | Cause | Where to look |
|---|---|---|
| Deployment shows `0/N` ready, no error from `kubectl apply` | Enforce rejected the Pods | ReplicaSet events |
| Job never completes, no Pods exist | Pod creation is forbidden | Job events |
| Warnings on apply, but Pods run | Only `warn` or `audit` is set to the stricter level | Namespace labels |
Exercise
A namespace has the label pod-security.kubernetes.io/enforce: restricted. You apply a Deployment that has no securityContext. kubectl apply reports deployment.apps/api created, but kubectl get pods shows nothing. Why, and which command shows the reason?
Show answer
Enforce mode checks Pods, not Deployments, so the API server accepted the Deployment. The ReplicaSet controller then failed to create Pods because they violate the Restricted profile. Run kubectl describe replicaset -n <namespace> (or kubectl get events -n <namespace>) to see the Forbidden error that names the violated controls.