Zum Inhalt springen
IT-Trainingslagerhands-on · kostenlos

violates PodSecurity: Pod Security Admission lehnt den Pod ab

Securitygetestet auf Kubernetes v1.37.1

Pod Security Admission prüft Pods beim Anlegen gegen die Stufe, die das Label pod-security.kubernetes.io/enforce am Namespace festlegt. Verletzt ein Pod sie, lehnt der API-Server ihn ab. Die Meldung ist die Checkliste: Jede Klammer nennt Feld und Sollwert.

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)

Bei einem Deployment kommt nur eine Warnung, das Deployment wird angelegt. Erst der ReplicaSet-Controller scheitert am Pod – das Symptom heißt dann „0/1 und kein Pod“.

Diagnose

Bei Deployments steht die Ablehnung in den 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), ...

Ursachen

baselineHost-Zugriff oder privileged

baseline verbietet Host-Namespaces (hostPID, hostNetwork), privileged und hostPath-Volumes. Ein hostPath-Volume scheitert mit „hostPath volumes (volume …)“.

restrictedDie vier Punkte von restricted fehlen

restricted verlangt zusätzlich allowPrivilegeEscalation false, drop ALL, runAsNonRoot und ein seccompProfile RuntimeDefault oder 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")

Lösung

Die Punkte aus der Meldung als Patch auf das Pod-Template setzen. Der Strategic-Merge-Patch fügt die Felder über den Container-Namen zusammen.
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

Nicht tun: enforce setzen, ohne vorher zu prüfen. Mit k label --dry-run=server fragst du den API-Server, welche laufenden Pods die neue Stufe verletzen würden.

▶
Selbst einbauen und finden – kostenlos
CKS zum Anfassen, Tag 9: Pod Security Admission mit warn, audit und enforce, Verstöße beheben und runAsNonRoot.
Zu Tag 9 →

Häufige Fragen

Werden laufende Pods gestoppt, wenn ich enforce setze?

Nein. PSA prüft nur beim Anlegen. Ein Pod, der die Stufe verletzt, läuft weiter – du musst ihn selbst entfernen.

Reicht runAsNonRoot true für restricted?

Für die Admission ja. Ob das Image wirklich als Nicht-Root startet, merkt erst das kubelet – sonst endet der Pod in CreateContainerConfigError.

Verwandte Fehlerbilder

Mehr