Geavanceerd
Zoeken inLabs
Zo zoek je
  • Zoek een taak of een tool (lvm, selinux, @podman); filter op niveau en op boek of cursus.
  • Met een leeg zoekvak toont een filter alleen al de labs — bijvoorbeeld alle moeilijke labs van één cursus.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Lab 4.1CKS, Hoofdstuk 4

Enforce the restricted Pod Security Standard and fix a rejected pod

Niveau
Gemiddeld
35 min
Machines
a Kubernetes cluster 1.25 or newer (kind, minikube or kubeadm)
Status
Nog niet begonnen

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

Doel

Turn on Pod Security Admission for a namespace in three steps (warn, audit, enforce), watch a typical "works on my laptop" pod get rejected, and rewrite it until it passes the restricted level: non-root, no privilege escalation, all capabilities dropped, a seccomp profile and a read-only root file system.

Voordat je begint

  • kubectl version shows a server of 1.25 or newer (Pod Security Admission is built in and on).
  • Save this pod as bad-pod.yaml:
    bad-pod.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: web
    spec:
      containers:
        - name: web
          image: nginx:1.27
          ports: [{containerPort: 80}]
          securityContext:
            privileged: true

Taken

  1. Make the namespace secure-apps with the labels warn=restricted and audit=restricted only. Apply the bad pod and read the warnings.
  2. Delete the pod and set enforce=restricted with a pinned version (enforce-version=v1.30 or your version).
  3. Apply the bad pod again and read why it is rejected.
  4. Rewrite it as good-pod.yaml so it passes restricted. nginx must still serve pages (it needs a non-root image or port above 1024 and writable temp folders).
  5. Check with a server-side dry run that existing pods in the default namespace would pass baseline.

Controleer je werk

kubectl get ns secure-apps --show-labels
kubectl -n secure-apps apply -f good-pod.yaml
kubectl -n secure-apps get pod web
kubectl -n secure-apps exec web -- id
kubectl -n secure-apps port-forward pod/web 8080:8080 & sleep 2; curl -s localhost:8080 | head -4; kill %1
  • The namespace has pod-security.kubernetes.io/enforce=restricted.
  • bad-pod.yaml is rejected with violates PodSecurity "restricted:...".
  • good-pod.yaml is Running; id shows a user that is not 0; curl shows the nginx welcome page.

Hints

Toon de hints
  • Labels: pod-security.kubernetes.io/<mode>=<level> and pod-security.kubernetes.io/<mode>-version=<v>.
  • Restricted needs: runAsNonRoot: true, allowPrivilegeEscalation: false, capabilities: drop: [ALL], seccompProfile: type: RuntimeDefault, and no privileged, hostPath, hostNetwork or hostPort.
  • nginxinc/nginx-unprivileged listens on 8080 and runs as user 101.
  • With readOnlyRootFilesystem: true, give nginx emptyDir volumes for /tmp (and its cache folders).
  • kubectl label --dry-run=server --overwrite ns default pod-security.kubernetes.io/enforce=baseline shows warnings for pods that would fail.

Oplossing

Toon de oplossing
  1. Warn and audit first.
    kubectl create ns secure-apps
    kubectl label ns secure-apps pod-security.kubernetes.io/warn=restricted pod-security.kubernetes.io/audit=restricted
    kubectl -n secure-apps apply -f bad-pod.yaml
    Warning: would violate PodSecurity "restricted:latest": privileged (container "web" must not set securityContext.privileged=true), allowPrivilegeEscalation != false, unrestricted capabilities, runAsNonRoot != true, seccompProfile
    pod/web created
  2. Enforce.
    kubectl -n secure-apps delete pod web
    kubectl label ns secure-apps pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/enforce-version=v1.30
    kubectl -n secure-apps apply -f bad-pod.yaml
    Error from server (Forbidden): error when creating "bad-pod.yaml": pods "web" is forbidden: violates PodSecurity "restricted:v1.30": privileged ...
  3. The fixed pod.
    good-pod.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: web
    spec:
      securityContext:
        runAsNonRoot: true
        runAsUser: 101
        seccompProfile:
          type: RuntimeDefault
      containers:
        - name: web
          image: nginxinc/nginx-unprivileged:1.27-alpine
          ports: [{containerPort: 8080}]
          securityContext:
            allowPrivilegeEscalation: false
            readOnlyRootFilesystem: true
            capabilities:
              drop: [ALL]
          volumeMounts:
            - {name: tmp, mountPath: /tmp}
      volumes:
        - {name: tmp, emptyDir: {}}
    kubectl -n secure-apps apply -f good-pod.yaml
    kubectl -n secure-apps exec web -- id
    uid=101(nginx) gid=101(nginx) groups=101(nginx)
  4. Test another namespace without changing it.
    kubectl label --dry-run=server --overwrite ns default pod-security.kubernetes.io/enforce=baseline

Mijn notities

Nog geen notities.

Lessen voor dit lab

Commando's in dit lab