Zum Inhalt springen
Trainingslager IT-Trainingslager

Tag 5: Abstürze und Neustarts

Ziel: Du trennst Abstürze des Prozesses von Neustarts durch das kubelet und reparierst Befehl, Speicherlimit, Probes und schreibgeschütztes Dateisystem. · Dauer: ca. 75 min · Voraussetzung: Tag 1

Briefing

  • Ein Container startet neu, weil der Prozess endet (Exit-Code, Log) oder weil das kubelet ihn beendet (Liveness, Event Killing). Die Events sagen, welcher Fall vorliegt.
  • OOMKilled mit 137 heißt: Der Kernel hat im Speicherlimit des Containers getötet.
  • Readiness startet nichts neu, sie nimmt den Pod nur aus dem Service.
  • Der Backoff zwischen Neustarts verdoppelt sich ab 10 s bis höchstens 5 min.
Fehlerbild Symptom Wo die Antwort steht
Befehl falsch Exit 127 Log: not found
App-Fehler Exit 1 logs --previous
Speicher OOMKilled, 137 Last State, Kernel-Log
Liveness zu streng Neustarts, Killing Events Unhealthy
Readiness falsch 0/1, keine Neustarts Events, EndpointSlice

Drill 1: Falscher Befehl und der Backoff

Einbauen:

# auf cp1
mkdir -p ~/debug/tag05 && cd ~/debug/tag05
kubectl create namespace tag05
cat > tippfehler.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata: {name: tippfehler, namespace: tag05}
spec:
  replicas: 1
  selector: {matchLabels: {app: tippfehler}}
  template:
    metadata: {labels: {app: tippfehler}}
    spec:
      containers:
      - name: app
        image: busybox:1.37
        command: [sh, -c, 'echo "starte"; slep infinity']
EOF
kubectl apply -f tippfehler.yaml

Symptom: Beobachte zwei Minuten lang Neustarts und Wartezeit:

# auf cp1
for i in $(seq 1 12); do sleep 10
  kubectl -n tag05 get pod -l app=tippfehler --no-headers | awk '{print $3, $4, $5, $6}'
done
kubectl -n tag05 get pod -l app=tippfehler \
  -o jsonpath='{.items[0].status.containerStatuses[0].state.waiting.message}{"\n"}' \
  | cut -c1-40

Erwartet (gekürzt):

CrashLoopBackOff 1 (8s ago)
Error 2 (18s ago)
Error 3 (36s ago)
CrashLoopBackOff 3 (38s ago)
Error 4 (49s ago)
Error 4 (81s ago)
back-off 1m20s restarting failed

Untersuchen:

# auf cp1
kubectl -n tag05 logs deploy/tippfehler
kubectl -n tag05 get pod -l app=tippfehler -o jsonpath=\
'{.items[0].status.containerStatuses[0].lastState.terminated.exitCode}{"\n"}'

Erwartet:

starte
sh: slep: not found
127

Ursache: Tippfehler im Befehl. Warum? Die Wartezeit wächst 10 s, 20 s, 40 s, 80 s bis 5 min und setzt sich erst zurück, wenn der Container zehn Minuten fehlerfrei lief. Ein Fix wirkt also nicht sofort, wenn der Pod gerade im langen Backoff hängt – ein Rollout erzeugt einen frischen Pod.

Fix:

# auf cp1
kubectl -n tag05 patch deploy tippfehler --type=json -p '[{"op": "replace",
  "path": "/spec/template/spec/containers/0/command/2",
  "value": "echo \"starte\"; sleep infinity"}]'
kubectl -n tag05 rollout status deploy/tippfehler

Nachweis:

# auf cp1
kubectl -n tag05 logs deploy/tippfehler

Erwartet (gekürzt):

starte

Fortschritt speichern? Kostenlos registrieren

Drill 2: Die Anwendung gibt auf

Einbauen:

# auf cp1
cat > web.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata: {name: web, namespace: tag05}
spec:
  replicas: 1
  selector: {matchLabels: {app: web}}
  template:
    metadata: {labels: {app: web}}
    spec:
      containers:
      - name: web
        image: nginxinc/nginx-unprivileged:1.29-alpine
        command: [nginx, -g, 'daemon off;', -c, /etc/nginx/conf.d/app.conf]
EOF
kubectl apply -f web.yaml

Symptom und Untersuchen:

# auf cp1
kubectl -n tag05 get pods -l app=web
kubectl -n tag05 logs deploy/web

Erwartet (Zeile umbrochen):

NAME                   READY   STATUS             RESTARTS      AGE
web-7c8cd947d6-zstdf   0/1     CrashLoopBackOff   4 (55s ago)   2m26s
2026/11/20 07:25:01 [emerg] 1#1: open() "/etc/nginx/conf.d/app.conf" failed
  (2: No such file or directory)
nginx: [emerg] open() "/etc/nginx/conf.d/app.conf" failed (2: No such file or directory)

Ursache: Der Befehl verweist auf eine Konfiguration, die es im Image nicht gibt; nginx endet mit Exit-Code 1. Die Datei gehört als ConfigMap in den Pod.

Fix:

# auf cp1
cat > web-conf.yaml <<'EOF'
apiVersion: v1
kind: ConfigMap
metadata: {name: web-conf, namespace: tag05}
data:
  app.conf: |
    pid /tmp/nginx.pid;
    events {}
    http {
      server {
        listen 8080;
        location / { return 200 "ok\n"; }
      }
    }
EOF
kubectl apply -f web-conf.yaml
kubectl -n tag05 patch deploy web -p '
spec:
  template:
    spec:
      containers:
      - name: web
        volumeMounts: [{name: conf, mountPath: /etc/nginx/conf.d}]
      volumes:
      - name: conf
        configMap: {name: web-conf}'
kubectl -n tag05 rollout status deploy/web

Nachweis:

# auf cp1
kubectl -n tag05 exec deploy/web -- wget -qO- 127.0.0.1:8080

Erwartet:

ok

Tipp: wget localhost:8080 meldet hier Connection refused: localhost löst im Pod zuerst auf ::1 auf, nginx lauscht aber nur auf IPv4.

Warum patch ohne --type=merge? Der Standard (Strategic Merge) führt Listen wie containers über den Namen zusammen. Ein JSON-Merge-Patch würde die ganze Liste ersetzen und an fehlendem image scheitern.

Fortschritt speichern? Kostenlos registrieren

Drill 3: OOMKilled

Einbauen: Ein Prozess lädt 100 MB in den Speicher, das Limit ist 64Mi.

# auf cp1
cat > speicher.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata: {name: speicher, namespace: tag05}
spec:
  replicas: 1
  selector: {matchLabels: {app: speicher}}
  template:
    metadata: {labels: {app: speicher}}
    spec:
      containers:
      - name: app
        image: busybox:1.37
        command:
        - sh
        - -c
        - echo "lade Cache"; head -c 100m /dev/zero | tail >/dev/null; sleep infinity
        resources:
          requests: {cpu: 50m, memory: 64Mi}
          limits: {memory: 64Mi}
EOF
kubectl apply -f speicher.yaml

Symptom:

# auf cp1
kubectl -n tag05 get pods -l app=speicher -o wide | cut -c1-88

Erwartet:

NAME                        READY   STATUS      RESTARTS      AGE   IP           NODE
speicher-7489b76474-qncx6   0/1     OOMKilled   1 (17s ago)   20s   10.42.2.16   worker2

Untersuchen:

# auf cp1
kubectl -n tag05 describe pod -l app=speicher \
  | sed -n '/Last State/,/Exit Code/p;/Limits/,/memory/p'

Erwartet:

    Last State:     Terminated
      Reason:       OOMKilled
      Exit Code:    137
    Limits:
      memory:  64Mi

Den Beweis liefert der Kernel des Knotens aus der Spalte NODE:

# auf worker2
sudo journalctl -k --since -5min | grep 'Killed process' | cut -c1-88

Erwartet:

Nov 20 07:27:20 worker2 kernel: Memory cgroup out of memory: Killed process 29137 (sh) t
Nov 20 07:27:20 worker2 kernel: Memory cgroup out of memory: Killed process 29157 (head)
Nov 20 07:27:20 worker2 kernel: Memory cgroup out of memory: Killed process 29158 (tail)

Ursache: tail puffert alles bis zum ersten Zeilenende – bei /dev/zero also 100 MB. Warum alle drei Prozesse? Unter cgroup v2 setzt das kubelet memory.oom.group; der Kernel beendet dann den ganzen Container, nicht nur den größten Prozess.

Fix: Limit nach Bedarf plus Reserve:

# auf cp1
kubectl -n tag05 set resources deploy/speicher \
  --requests=memory=160Mi --limits=memory=160Mi
kubectl -n tag05 rollout status deploy/speicher

Nachweis:

# auf cp1
kubectl -n tag05 get pods -l app=speicher
kubectl -n tag05 logs deploy/speicher

Erwartet:

NAME                        READY   STATUS    RESTARTS   AGE
speicher-67f57fff74-l4z8r   1/1     Running   0          42s
lade Cache

Fortschritt speichern? Kostenlos registrieren

Drill 4: Liveness zu streng

Einbauen: Die Anwendung braucht 40 Sekunden zum Start, die Liveness-Probe gibt ihr 10 s plus drei Fehlversuche à 5 s.

# auf cp1
cat > langsam.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata: {name: langsam, namespace: tag05}
spec:
  replicas: 1
  selector: {matchLabels: {app: langsam}}
  template:
    metadata: {labels: {app: langsam}}
    spec:
      terminationGracePeriodSeconds: 5
      containers:
      - name: app
        image: busybox:1.37
        command:
        - sh
        - -c
        - |
          echo "Lade Daten (dauert 40 Sekunden) ..."
          sleep 40
          mkdir -p /www && echo ok > /www/healthz
          exec httpd -f -p 8080 -h /www
        livenessProbe:
          httpGet: {path: /healthz, port: 8080}
          initialDelaySeconds: 10
          periodSeconds: 5
          failureThreshold: 3
EOF
kubectl apply -f langsam.yaml

Symptom (nach gut einer Minute):

# auf cp1
kubectl -n tag05 get pods -l app=langsam

Erwartet:

NAME                       READY   STATUS    RESTARTS      AGE
langsam-6b8fb65674-95vvb   1/1     Running   2 (15s ago)   75s

1/1 und trotzdem Neustarts: Ohne Readiness-Probe gilt der Container sofort als bereit, auch wenn er noch gar nicht antwortet.

Untersuchen:

# auf cp1
kubectl -n tag05 events --for $(kubectl -n tag05 get pod -l app=langsam -o name) \
  | grep -E 'Unhealthy|Killing' | cut -c1-88
kubectl -n tag05 logs deploy/langsam --previous

Erwartet (Zeilen umbrochen):

21s (x2 over 51s)   Normal    Killing     Pod/langsam-6b8fb65674-95vvb   Container app
  failed liveness probe, will be restarted
1s (x7 over 61s)    Warning   Unhealthy   Pod/langsam-6b8fb65674-95vvb   Liveness probe
  failed: Get "http://10.42.2.19:8080/healthz": dial tcp ...
Lade Daten (dauert 40 Sekunden) ...

Ursache: Nach 10 + 3 × 5 = 25 Sekunden beendet das kubelet den Container, lange vor den 40 Sekunden Startzeit. Das Log endet mitten im Start, ohne Fehlermeldung – der Prozess wurde von außen beendet. sh als PID 1 ignoriert SIGTERM, nach 5 s Gnadenfrist folgt SIGKILL: Exit-Code 137 ohne OOMKilled.

Fix: Eine Startup-Probe schützt den Start, die Liveness greift erst danach:

# auf cp1
kubectl -n tag05 patch deploy langsam -p '
spec:
  template:
    spec:
      containers:
      - name: app
        startupProbe:
          httpGet: {path: /healthz, port: 8080}
          periodSeconds: 5
          failureThreshold: 18'
kubectl -n tag05 rollout status deploy/langsam

Nachweis:

# auf cp1
kubectl -n tag05 get pods -l app=langsam

Erwartet (gekürzt):

NAME                       READY   STATUS    RESTARTS   AGE
langsam-79b659cc47-d22tt   1/1     Running   0          46s

18 × 5 s = 90 s Startzeit erlaubt; der Rollout wartet jetzt, bis die Anwendung wirklich antwortet.

Fortschritt speichern? Kostenlos registrieren

Drill 5: Readiness falsch gezielt

Einbauen:

# auf cp1
cat > nieready.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata: {name: nieready, namespace: tag05}
spec:
  replicas: 1
  selector: {matchLabels: {app: nieready}}
  template:
    metadata: {labels: {app: nieready}}
    spec:
      containers:
      - name: app
        image: busybox:1.37
        command: [sh, -c, 'mkdir -p /www && echo ok > /www/healthz &&
          exec httpd -f -p 8080 -h /www']
        readinessProbe:
          httpGet: {path: /ready, port: 8080}
          periodSeconds: 5
---
apiVersion: v1
kind: Service
metadata: {name: nieready, namespace: tag05}
spec:
  selector: {app: nieready}
  ports: [{port: 80, targetPort: 8080}]
EOF
kubectl apply -f nieready.yaml

Symptom:

# auf cp1
kubectl -n tag05 get pods -l app=nieready

Erwartet:

NAME                        READY   STATUS    RESTARTS   AGE
nieready-85b946b75c-54r8c   0/1     Running   0          75s

Untersuchen:

# auf cp1
kubectl -n tag05 events --for $(kubectl -n tag05 get pod -l app=nieready -o name) \
  | grep Unhealthy | tail -1 | cut -c1-88
kubectl -n tag05 get endpointslices -l kubernetes.io/service-name=nieready
kubectl -n tag05 get endpointslices -l kubernetes.io/service-name=nieready \
  -o jsonpath='{.items[0].endpoints[0].conditions}{"\n"}'

Erwartet (Zeile umbrochen):

3s (x16 over 75s)   Warning   Unhealthy   Pod/nieready-85b946b75c-54r8c   Readiness
  probe failed: HTTP probe failed with statuscode: 404
NAME             ADDRESSTYPE   PORTS   ENDPOINTS    AGE
nieready-4j72l   IPv4          8080    10.42.2.20   77s
{"ready":false,"serving":false,"terminating":false}

Ursache: Die Probe fragt /ready, die Anwendung kennt nur /healthz – 404. Warum steht die IP trotzdem in der EndpointSlice? Auch nicht bereite Pods stehen darin, mit ready: false (im alten Endpoints-Objekt unter notReadyAddresses). kube-proxy schickt keinen Verkehr hin.

Fix:

# auf cp1
kubectl -n tag05 patch deploy nieready --type=json -p '[{"op": "replace",
  "path": "/spec/template/spec/containers/0/readinessProbe/httpGet/path",
  "value": "/healthz"}]'
kubectl -n tag05 rollout status deploy/nieready

Nachweis:

# auf cp1
kubectl -n tag05 get endpointslices -l kubernetes.io/service-name=nieready \
  -o jsonpath='{.items[0].endpoints[0].conditions}{"\n"}'

Erwartet:

{"ready":true,"serving":true,"terminating":false}

Fortschritt speichern? Kostenlos registrieren

Drill 6: readOnlyRootFilesystem

Einbauen:

# auf cp1
cat > webro.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata: {name: webro, namespace: tag05}
spec:
  replicas: 1
  selector: {matchLabels: {app: webro}}
  template:
    metadata: {labels: {app: webro}}
    spec:
      containers:
      - name: web
        image: nginxinc/nginx-unprivileged:1.29-alpine
        securityContext: {readOnlyRootFilesystem: true}
EOF
kubectl apply -f webro.yaml

Symptom und Untersuchen:

# auf cp1
kubectl -n tag05 get pods -l app=webro
kubectl -n tag05 logs deploy/webro | grep -iE 'read-only|can not modify' | cut -c1-88

Erwartet (Zeilen umbrochen):

NAME                     READY   STATUS    RESTARTS      AGE
webro-668d7bff5f-xg6r8   1/1     Running   2 (13s ago)   16s
10-listen-on-ipv6-by-default.sh: info: can not modify /etc/nginx/conf.d/default.conf
  (read-only file system?)
2026/11/20 07:32:17 [emerg] 1#1: mkdir() "/tmp/proxy_temp" failed (30: Read-only
  file system)
nginx: [emerg] mkdir() "/tmp/proxy_temp" failed (30: Read-only file system)

Ursache: Das Root-Dateisystem ist schreibgeschützt, nginx braucht aber /tmp für Zwischendateien. Die erste Zeile ist nur eine Info des Entrypoints, die dritte der eigentliche Abbruch.

Fix: Nur das beschreibbar machen, was die Anwendung wirklich braucht:

# auf cp1
kubectl -n tag05 patch deploy webro -p '
spec:
  template:
    spec:
      containers:
      - name: web
        volumeMounts: [{name: tmp, mountPath: /tmp}]
      volumes:
      - name: tmp
        emptyDir: {sizeLimit: 64Mi}'
kubectl -n tag05 rollout status deploy/webro

Nachweis:

# auf cp1
kubectl -n tag05 exec deploy/webro -- wget -qO- 127.0.0.1:8080 | grep -o '<title>.*<'
kubectl -n tag05 exec deploy/webro -- touch /etc/nginx/test

Erwartet:

<title>Welcome to nginx!<
touch: /etc/nginx/test: Read-only file system
command terminated with exit code 1

Der Schutz bleibt für alles außer /tmp.

Fortschritt speichern? Kostenlos registrieren

Übung: Speed-Drills

  1. Liste die Pods in tag05 nach Neustarts sortiert. Zeitziel: 30 s
  2. Zeig für alle Pods den Reason des letzten beendeten Laufs. Zeitziel: 45 s
  3. Zeig die Startup-Probe von langsam als JSON. Zeitziel: 30 s
  4. Zeig die OOM-Meldungen des Kernels auf worker2. Zeitziel: 20 s

Lösung

# auf cp1
kubectl -n tag05 get pods --sort-by='.status.containerStatuses[0].restartCount'
kubectl -n tag05 get pods -o custom-columns=NAME:.metadata.name,\
LAST:.status.containerStatuses[0].lastState.terminated.reason
kubectl -n tag05 get deploy langsam \
  -o jsonpath='{.spec.template.spec.containers[0].startupProbe}{"\n"}'
ssh worker2 sudo journalctl -k | grep 'Killed process'

Erwartet (gekürzt, alle Fixes angewendet):

NAME                          READY   STATUS    RESTARTS   AGE
langsam-79b659cc47-d22tt      1/1     Running   0          3m8s
...
NAME                          LAST
langsam-79b659cc47-d22tt      <none>
...
{"failureThreshold":18,"httpGet":{"path":"/healthz","port":8080,"scheme":"HTTP"},...}
Nov 20 07:27:20 worker2 kernel: Memory cgroup out of memory: Killed process 29158 ...

Fortschritt speichern? Kostenlos registrieren

Kontrollpunkt

# auf cp1
kubectl -n tag05 get deploy

Erwartet:

NAME         READY   UP-TO-DATE   AVAILABLE   AGE
langsam      1/1     1            1           4m47s
nieready     1/1     1            1           4m47s
speicher     1/1     1            1           7m25s
tippfehler   1/1     1            1           10m
web          1/1     1            1           10m
webro        1/1     1            1           2m20s

Challenge

Das Deployment waechter startet anfangs alle 15 bis 20 Sekunden neu, danach mit wachsendem Backoff, obwohl httpd sauber läuft. Finde die Ursache und repariere sie.

# auf cp1
cat > waechter.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata: {name: waechter, namespace: tag05}
spec:
  replicas: 1
  selector: {matchLabels: {app: waechter}}
  template:
    metadata: {labels: {app: waechter}}
    spec:
      terminationGracePeriodSeconds: 5
      containers:
      - name: app
        image: busybox:1.37
        command: [sh, -c, 'mkdir -p /www && echo ok > /www/index.html &&
          exec httpd -f -p 8080 -h /www']
        livenessProbe:
          tcpSocket: {port: 80}
          periodSeconds: 5
          failureThreshold: 3
EOF
kubectl apply -f waechter.yaml

Hinweis: Wer beendet den Container – der Prozess selbst oder das kubelet?

Lösung

# auf cp1
kubectl -n tag05 events --for $(kubectl -n tag05 get pod -l app=waechter -o name) \
  | grep -E 'Unhealthy|Killing' | cut -c1-88

Erwartet (Zeilen umbrochen):

11s (x2 over 31s)   Normal    Killing     Pod/waechter-5bf8fb5d6d-89w29   Container app
  failed liveness probe, will be restarted
1s (x7 over 41s)    Warning   Unhealthy   Pod/waechter-5bf8fb5d6d-89w29   Liveness probe
  failed: dial tcp 10.42.2.23:80: ...

Die Probe prüft Port 80, httpd lauscht auf 8080.

# auf cp1
kubectl -n tag05 patch deploy waechter --type=json -p '[{"op": "replace",
  "path": "/spec/template/spec/containers/0/livenessProbe/tcpSocket/port",
  "value": 8080}]'
kubectl -n tag05 rollout status deploy/waechter
kubectl -n tag05 get pods -l app=waechter

Erwartet (nach 30 Sekunden):

NAME                        READY   STATUS    RESTARTS   AGE
waechter-76495bb95b-z7h2b   1/1     Running   0          33s

Fortschritt speichern? Kostenlos registrieren

Aufräumen

# auf cp1
kubectl delete namespace tag05

Erwartet:

namespace "tag05" deleted

Interview-Notiz

  • Liveness oder Readiness? Liveness startet neu (Prozess hängt), Readiness nimmt aus dem Service (vorübergehend nicht bereit). Eine Liveness, die von Abhängigkeiten wie der Datenbank abhängt, macht aus einem DB-Ausfall einen Neustart-Sturm.
  • 137 – OOM oder nicht? Reason: OOMKilled bzw. Kernel-Log; ohne beides war es ein SIGKILL von außen (Liveness, Gnadenfrist abgelaufen).
  • Langsamer Start? startupProbe statt riesigem initialDelaySeconds.

Enterprise-Notiz

  • Speicherlimits aus Messwerten ableiten (kubectl top, Metriken über Tage), nicht raten; JVM und Co. brauchen Heap plus Overhead.
  • Die Fehlerbilder hier tauchen oft erst unter Last auf: Probes mit timeoutSeconds: 1 scheitern, wenn die Anwendung ausgelastet ist.
  • readOnlyRootFilesystem als Standard in Pod-Security-Richtlinien; dazu gehört eine Liste der nötigen emptyDir-Pfade pro Anwendung.
  • Alarme auf kube_pod_container_status_last_terminated_reason{reason="OOMKilled"} und auf steigende Restart-Zähler sind billiger als jede Fehlersuche.