Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 4Naam in de cursus: Pod Security Admission and security contexts

Enforce Pod Security Standards

Pod Security Standards define three levels of pod safety. The built-in Pod Security Admission controller enforces them per namespace with labels — no extra software needed. A pod that breaks the level is rejected (or warned about).

  • 4 Lessen
  • 2 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

Vaardigheden in dit hoofdstuk

  • Pod Security Standards en security contexts

4.1

Know the three levels

Choose the strictest level each namespace can live with.

LevelAllowsFor
privilegedEverythingSystem namespaces (CNI, storage drivers)
baselineBlocks known escalations: host namespaces, privileged, hostPath, extra capabilitiesMost legacy apps
restrictedAlso: non-root, no privilege escalation, drop ALL capabilities, seccomp RuntimeDefaultNew apps — the target

4.2

Label a namespace

Three modes per namespace: enforce (reject), audit (log), warn (warning to the user).

kubectl label namespace shop pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/enforce-version=latest
kubectl label namespace shop pod-security.kubernetes.io/warn=restricted pod-security.kubernetes.io/audit=restricted --overwrite
kubectl label --dry-run=server --overwrite namespace legacy pod-security.kubernetes.io/enforce=restricted
kubectl get ns shop --show-labels

Op het werk

The --dry-run=server line lists which running pods would break the level before you enforce it.

4.3

Write a restricted pod

This pod passes the restricted level.

restricted-pod.yaml
apiVersion: v1
kind: Pod
metadata: { name: api, namespace: shop }
spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 1001
    runAsGroup: 1001
    fsGroup: 1001
    seccompProfile: { type: RuntimeDefault }
  containers:
    - name: api
      image: ghcr.io/s0sta/api:2.0
      securityContext:
        allowPrivilegeEscalation: false
        readOnlyRootFilesystem: true
        capabilities: { drop: ["ALL"] }
      volumeMounts:
        - { name: tmp, mountPath: /tmp }
  volumes:
    - name: tmp
      emptyDir: {}
kubectl apply -f restricted-pod.yaml
kubectl run bad --image=nginx -n shop
Error from server (Forbidden): pods "bad" is forbidden: violates PodSecurity "restricted:latest": allowPrivilegeEscalation != false ...

4.4

Fix a rejected workload

The error message lists every broken rule. Fix them one by one in the pod template (Deployments are not rejected — their pods are; check the ReplicaSet events).

kubectl get events -n shop --field-selector reason=FailedCreate
kubectl describe rs -n shop -l app=web | grep -A3 Warning
Message partFix
runAsNonRoot != truesecurityContext.runAsNonRoot: true (+ a non-root image or runAsUser)
allowPrivilegeEscalation != falseSet it to false per container
unrestricted capabilitiescapabilities.drop: ["ALL"]
seccompProfileseccompProfile.type: RuntimeDefault
hostPath volumesUse PVCs or emptyDir

Onthoud

Pod Security Admission checks only new pods. Existing pods keep running until they are recreated.

Alle commando's in dit hoofdstuk