Skip to content

violates PodSecurity: Pod Security Admission rejects the pod

Securitytested on Kubernetes v1.37.1

Pod Security Admission checks pods at creation time against the level set by the pod-security.kubernetes.io/enforce label on the namespace. If a pod violates it, the API server rejects it. The message is the checklist: every parenthesis names field and required value.

Symptom

Error from server (Forbidden): error when creating "STDIN": pods "toolbox2" is
forbidden: violates PodSecurity "baseline:v1.37": host namespaces (hostPID=true),
privileged (container "c" must not set securityContext.privileged=true)
Error from server (Forbidden): pods "hn" is forbidden: violates PodSecurity
"baseline:v1.37": host namespaces (hostNetwork=true)

A deployment only gets a warning and is created. Only the ReplicaSet controller fails on the pod – the symptom is then "0/1 and no pod".

Diagnosis

For deployments, the rejection is in the events.

k -n tag09-strict get deploy notes
k -n tag09-strict get events --field-selector reason=FailedCreate \
  -o jsonpath='{.items[0].message}{"\n"}'
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
notes   0/1     0            0           3s
Error creating: pods "notes-7d6b8b9c78-wfvf8" is forbidden: violates PodSecurity
"restricted:v1.37": allowPrivilegeEscalation != false (container "notes-api" must set
securityContext.allowPrivilegeEscalation=false), ...

Causes

baselineHost access or privileged

baseline forbids host namespaces (hostPID, hostNetwork), privileged and hostPath volumes. A hostPath volume fails with "hostPath volumes (volume …)".

restrictedThe four restricted settings are missing

restricted additionally requires allowPrivilegeEscalation false, drop ALL, runAsNonRoot and a seccompProfile of RuntimeDefault or Localhost.

Warning: would violate PodSecurity "restricted:v1.37": allowPrivilegeEscalation != false
(container "probe" must set securityContext.allowPrivilegeEscalation=false),
unrestricted capabilities (container "probe" must set
securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container
"probe" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container
"probe" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost")

Fix

Apply the items from the message as a patch to the pod template. The strategic merge patch merges the fields by container name.
cat > fix.yaml <<'EOF'
spec:
  template:
    spec:
      securityContext:
        runAsNonRoot: true
        seccompProfile:
          type: RuntimeDefault
      containers:
      - name: notes-api
        securityContext:
          allowPrivilegeEscalation: false
          capabilities:
            drop: ["ALL"]
EOF
k -n tag09-strict patch deploy notes --patch-file fix.yaml
k -n tag09-strict rollout status deploy/notes
deployment.apps/notes patched
deployment "notes" successfully rolled out

Don't: Setting enforce without checking first. With k label --dry-run=server you ask the API server which running pods would violate the new level.

▶
Break it yourself and find it – free
CKS hands-on, day 9: Pod Security Admission with warn, audit and enforce, fixing violations and runAsNonRoot.
Go to day 9 →

FAQ

Are running pods stopped when I set enforce?

No. PSA only checks at creation time. A pod that violates the level keeps running – you have to remove it yourself.

Is runAsNonRoot true enough for restricted?

For admission, yes. Whether the image really starts as non-root is only noticed by the kubelet – otherwise the pod ends up in CreateContainerConfigError.

Related error patterns

More