Tag 6: kubectl debug
Ziel: Du untersuchst Container ohne Shell, kopierst abstürzende Pods mit anderem Image und gehst ohne SSH auf einen Knoten – und kennst die Grenzen der Profile. · Dauer: ca. 60 min · Voraussetzung: Tag 5
Briefing
- Distroless-Images haben keine Shell.
kubectl exechilft dann nicht,kubectl debugbringt das Werkzeug als eigenes Image mit. - Drei Varianten: Ephemeral Container im laufenden Pod, Kopie des Pods
(
--copy-to), Pod auf einem Knoten (node/…). - Das Profil (
--profile) bestimmt densecurityContextdes Debug-Containers und damit, was du siehst – und ob Pod Security ihn überhaupt zulässt.
| Lage | Variante | Wichtigste Option |
|---|---|---|
| Pod läuft, keine Shell | Ephemeral Container | --target=<container> |
| Pod stürzt sofort ab | Kopie | --copy-to, --set-image,
--container |
| Problem auf dem Knoten | Knoten-Pod | node/<name>, chroot /host |
Drill 1: Distroless, und es stürzt ab
Einbauen:
# auf cp1
mkdir -p ~/debug/tag06 && cd ~/debug/tag06
kubectl create namespace tag06
cat > dns-test.yaml <<'EOF'
apiVersion: v1
kind: ConfigMap
metadata: {name: dns-test, namespace: tag06}
data:
corefile: |
.:1053 {
whoami
health :8080
log
}
---
apiVersion: apps/v1
kind: Deployment
metadata: {name: dns-test, namespace: tag06}
spec:
replicas: 1
selector: {matchLabels: {app: dns-test}}
template:
metadata: {labels: {app: dns-test}}
spec:
containers:
- name: coredns
image: registry.k8s.io/coredns/coredns:v1.14.7
args: [-conf, /etc/coredns/Corefile]
volumeMounts: [{name: config, mountPath: /etc/coredns}]
volumes:
- name: config
configMap: {name: dns-test}
EOF
kubectl apply -f dns-test.yamlSymptom:
# auf cp1
kubectl -n tag06 get pods
kubectl -n tag06 logs deploy/dns-testErwartet:
NAME READY STATUS RESTARTS AGE
dns-test-6f6cb5574b-v5blz 0/1 CrashLoopBackOff 1 (12s ago) 21s
loading Caddyfile via flag: open /etc/coredns/Corefile: no such file or directory
maxprocs: Leaving GOMAXPROCS=2: CPU quota undefined
Der naheliegende nächste Schritt scheitert doppelt:
# auf cp1
kubectl -n tag06 exec deploy/dns-test -- ls /etc/corednsErwartet (Zeile umbrochen, gekürzt):
error: Internal error occurred: ... OCI runtime exec failed: exec failed: cannot exec
in a stopped container
Der Container läuft nicht, und selbst wenn: Das Image hat weder
sh noch ls.
Fortschritt speichern? Kostenlos registrieren
Drill 2: Kopie mit anderem Image
Untersuchen: Eine Kopie des Pods, in der
coredns durch busybox ersetzt wird und nur schläft.
Volumes, Env und Scheduling-Vorgaben bleiben gleich; auf denselben
Knoten kommt die Kopie nur mit --same-node.
# auf cp1
P=$(kubectl -n tag06 get pod -l app=dns-test -o jsonpath='{.items[0].metadata.name}')
kubectl -n tag06 debug $P --copy-to=dns-kopie --set-image=coredns=busybox:1.37 \
--container=coredns -- sleep 3600
kubectl -n tag06 wait pod/dns-kopie --for=condition=Ready
kubectl -n tag06 exec dns-kopie -- ls -l /etc/coredns/
kubectl -n tag06 get pod dns-kopie --show-labelsErwartet:
pod/dns-kopie condition met
total 0
lrwxrwxrwx 1 root root 15 Nov 20 07:37 corefile -> ..data/corefile
NAME READY STATUS RESTARTS AGE LABELS
dns-kopie 1/1 Running 0 3s <none>
Ohne --container=coredns lehnt kubectl ab:
you must specify an existing container or a new image when specifying args.
Die Kopie hat keine Labels – sie gehört zu keinem ReplicaSet und bekommt
keinen Service-Verkehr (außer mit --keep-labels).
Ursache: Der Schlüssel der ConfigMap heißt
corefile, CoreDNS erwartet die Datei Corefile.
Linux unterscheidet Groß- und Kleinschreibung.
Fix:
# auf cp1
kubectl -n tag06 delete pod dns-kopie
sed -i 's/^ corefile: |/ Corefile: |/' dns-test.yaml
kubectl apply -f dns-test.yaml
kubectl -n tag06 rollout restart deploy/dns-test
kubectl -n tag06 rollout status deploy/dns-testNachweis:
# auf cp1
kubectl -n tag06 logs deploy/dns-test | head -3Erwartet (gekürzt):
maxprocs: Leaving GOMAXPROCS=2: CPU quota undefined
.:1053
CoreDNS-1.14.7
Fortschritt speichern? Kostenlos registrieren
Drill 3: Ephemeral Container mit –target
Jetzt läuft CoreDNS. Ein Ephemeral Container mit
--target teilt den Prozess-Namespace des Ziel-Containers
und sieht dessen Prozesse und Dateisystem (/proc/1/root).
Du startest ihn zweimal, mit zwei Profilen:
# auf cp1
P=$(kubectl -n tag06 get pod -l app=dns-test --field-selector=status.phase=Running \
-o jsonpath='{.items[0].metadata.name}')
for prof in baseline general; do
kubectl -n tag06 debug $P --image=nicolaka/netshoot:v0.15 --target=coredns \
--profile=$prof --container=dbg-$prof \
-- sh -c 'ps -o pid,user,args; ls /proc/1/root/etc/coredns'
done
kubectl -n tag06 logs $P -c dbg-baseline
kubectl -n tag06 logs $P -c dbg-generalErwartet (gekürzt):
PID USER COMMAND
1 65532 /coredns -conf /etc/coredns/Corefile
13 root sh -c ps -o pid,user,args; ls /proc/1/root/etc/coredns
ls: /proc/1/root/etc/coredns: Permission denied
PID USER COMMAND
1 65532 /coredns -conf /etc/coredns/Corefile
21 root sh -c ps -o pid,user,args; ls /proc/1/root/etc/coredns
Corefile
Warum? CoreDNS läuft als Benutzer 65532. Ins Dateisystem eines
fremden Prozesses schauen darf root nur mit CAP_SYS_PTRACE
– und die gibt nur general:
# auf cp1
kubectl -n tag06 get pod $P -o jsonpath=\
'{range .spec.ephemeralContainers[*]}{.name}={.securityContext}{"\n"}{end}'Erwartet:
dbg-baseline=
dbg-general={"capabilities":{"add":["SYS_PTRACE"]}}
Fortschritt speichern? Kostenlos registrieren
Drill 4: Die Profile im Vergleich
# auf cp1
for prof in restricted netadmin sysadmin; do
kubectl -n tag06 debug $P --image=nicolaka/netshoot:v0.15 --profile=$prof \
--container=dbg-$prof -- true
done
kubectl -n tag06 get pod $P -o jsonpath=\
'{range .spec.ephemeralContainers[*]}{.name}={.securityContext}{"\n"}{end}' | cut -c1-88Erwartet (gekürzt):
dbg-restricted={"allowPrivilegeEscalation":false,"capabilities":{"drop":["ALL"]},"runAs
dbg-netadmin={"capabilities":{"add":["NET_ADMIN","NET_RAW"]}}
dbg-sysadmin={"privileged":true}
# auf cp1
kubectl -n tag06 get pod $P -o json | jq -r '.status.ephemeralContainerStatuses[]
| select(.name=="dbg-restricted") | .state.waiting.message' | cut -c1-88Erwartet:
container has runAsNonRoot and image has non-numeric user (root), cannot verify user is
| Profil | securityContext | Wofür |
|---|---|---|
general (Standard) |
+SYS_PTRACE |
Prozesse und Dateien des Ziels |
baseline |
nichts | Netzwerk ansehen, PSA baseline |
restricted |
non-root, drop ALL, seccomp |
PSA restricted |
netadmin |
+NET_ADMIN,NET_RAW |
tcpdump, iptables |
sysadmin |
privileged: true |
alles, auch auf dem Knoten |
restricted scheitert hier am Image: netshoot läuft als
root, das Profil verlangt non-root. Das löst Drill 5.
Fortschritt speichern? Kostenlos registrieren
Drill 5: Ein eigenes Profil mit –custom
--custom ergänzt das gewählte Profil um eigene Felder
des Containers, etwa Benutzer und Volumes des Ziel-Pods.
# auf cp1
cat > debug-custom.yaml <<'EOF'
securityContext:
runAsUser: 65532
runAsGroup: 65532
volumeMounts:
- {name: config, mountPath: /etc/coredns}
EOF
kubectl -n tag06 debug $P --image=nicolaka/netshoot:v0.15 --profile=restricted \
--custom=debug-custom.yaml --container=dbg-custom \
-- sh -c 'id; cat /etc/coredns/Corefile'
kubectl -n tag06 logs $P -c dbg-customErwartet:
uid=65532 gid=65532 groups=65532
.:1053 {
whoami
health :8080
log
}
Der Debug-Container läuft als derselbe Benutzer wie CoreDNS, ohne
Capabilities, und sieht die ConfigMap über das Volume des Pods – ganz
ohne SYS_PTRACE.
Fortschritt speichern? Kostenlos registrieren
Drill 6: Auf den Knoten ohne SSH
kubectl debug node/… startet einen Pod mit Host-Netz und
Host-PIDs; das Dateisystem des Knotens liegt unter
/host.
# auf cp1
kubectl -n tag06 debug node/worker1 --image=busybox:1.37 --profile=sysadmin \
-- chroot /host sh -c 'hostname; systemctl is-active rke2-agent;
journalctl -u rke2-agent -n 2 --no-pager -o cat | cut -c1-80'
kubectl -n tag06 logs $(kubectl -n tag06 get pods -o name | grep node-debugger)Erwartet (erste Zeile umbrochen):
Creating debugging pod node-debugger-worker1-ltg4g with container debugger on node
worker1.
worker1
active
time="2026-11-20T07:15:00Z" level=info msg="rke2 agent is up and running"
Started rke2-agent.service - Rancher Kubernetes Engine v2 (agent).
Warum sysadmin? Lesende Befehle wie
systemctl is-active und journalctl laufen auch
mit dem Standardprofil general. Alles, was Kernel-Rechte
braucht, scheitert dort aber:
# auf cp1
kubectl -n tag06 debug node/worker1 --image=busybox:1.37 -- chroot /host sh -c \
'dmesg | tail -1; ip link add dummy9 type dummy && ip link del dummy9 && echo netz-ok'
kubectl -n tag06 logs $(kubectl -n tag06 get pods -o name \
--sort-by=.metadata.creationTimestamp | tail -1)Erwartet (nach einigen Sekunden; mit
--profile=sysadmin stattdessen die letzte Kernel-Meldung
und netz-ok):
dmesg: read kernel buffer failed: Operation not permitted
RTNETLINK answers: Operation not permitted
Erst sysadmin (privileged: true) hat alle
Capabilities, etwa CAP_SYSLOG für dmesg und
CAP_NET_ADMIN für Netzwerkänderungen.
Achtung: Ein Knoten-Pod mit
sysadminist root auf dem Knoten. Er bleibt nach dem Ende alsCompletedstehen; lösch ihn, sobald du fertig bist.
# auf cp1
kubectl -n tag06 delete $(kubectl -n tag06 get pods -o name | grep node-debugger)Fortschritt speichern? Kostenlos registrieren
Drill 7: Grenzen
Ephemeral Container lassen sich nicht entfernen:
# auf cp1
kubectl -n tag06 patch pod $P --subresource=ephemeralcontainers --type=json \
-p '[{"op":"remove","path":"/spec/ephemeralContainers/0"}]'Erwartet (Zeile umbrochen):
The Pod "dns-test-df85b59d6-2r8dd" is invalid: spec.ephemeralContainers: Forbidden:
existing ephemeral containers "dbg-baseline" may not be removed
Sie verschwinden erst mit dem Pod. Und Pod Security prüft beim
Hinzufügen den ganzen Pod. Ein sauberer Pod in einem
Namespace mit restricted:
# auf cp1
cat > sauber.yaml <<'EOF'
apiVersion: v1
kind: Pod
metadata: {name: sauber, namespace: tag06}
spec:
securityContext:
runAsNonRoot: true
runAsUser: 65534
seccompProfile: {type: RuntimeDefault}
containers:
- name: app
image: busybox:1.37
command: [sleep, infinity]
securityContext:
allowPrivilegeEscalation: false
capabilities: {drop: [ALL]}
EOF
kubectl apply -f sauber.yaml
kubectl label namespace tag06 pod-security.kubernetes.io/enforce=restricted
kubectl -n tag06 debug sauber --image=busybox:1.37 --profile=general --container=g1 \
-- id
kubectl -n tag06 debug sauber --image=busybox:1.37 --profile=restricted --container=r2 \
-- id
kubectl -n tag06 logs sauber -c r2Erwartet (Zeilen umbrochen, gekürzt):
Warning: existing pods in namespace "tag06" violate the new PodSecurity enforce level
"restricted:latest"
namespace/tag06 labeled
Error from server (Forbidden): pods "sauber" is forbidden: violates PodSecurity
"restricted:latest": allowPrivilegeEscalation != false (container "g1" must set
securityContext.allowPrivilegeEscalation=false), unrestricted capabilities
(container "g1" must set securityContext.capabilities.drop=["ALL"]; container "g1"
must not include "SYS_PTRACE" in securityContext.capabilities.add)
uid=65534(nobody) gid=65534(nobody) groups=65534(nobody)
In den nicht konformen Pod dns-test kommt unter
restricted gar kein Debug-Container mehr hinein, egal mit
welchem Profil: Die Ablehnung zählt dann auch coredns und
alle alten Ephemeral Container auf. Das Label nimmst du wieder ab:
# auf cp1
kubectl label namespace tag06 pod-security.kubernetes.io/enforce-Talos: Dort gilt cluster-weit
baseline–--profile=generalscheitert anSYS_PTRACE(Tag 18).
Fortschritt speichern? Kostenlos registrieren
Übung: Speed-Drills
- Liste die Namen aller Ephemeral Container im
dns-test-Pod. Zeitziel: 20 s - Zeig für jeden Ephemeral Container den Exit-Code. Zeitziel: 45 s
- Zeig, welche Profile dein
kubectl debugkennt. Zeitziel: 15 s - Lies
/etc/rancher/rke2/config.yamlvonworker2, ohne SSH. Zeitziel: 45 s
Lösung
# auf cp1
kubectl -n tag06 get pod $P -o jsonpath='{.spec.ephemeralContainers[*].name}{"\n"}'
kubectl -n tag06 get pod $P -o json | jq -r '.status.ephemeralContainerStatuses[]
| "\(.name) \(.state.terminated.exitCode)"'
kubectl debug --help | grep -A1 -- "--profile="
kubectl -n tag06 debug node/worker2 --image=busybox:1.37 \
-- cat /host/etc/rancher/rke2/config.yamlErwartet (gekürzt):
dbg-baseline dbg-general dbg-restricted dbg-netadmin dbg-sysadmin dbg-custom
dbg-baseline 1
dbg-general 0
dbg-restricted null
dbg-netadmin 0
dbg-sysadmin 0
dbg-custom 0
--profile='general':
Options are "general", "baseline", "restricted", "netadmin" or "sysadmin".
dbg-baseline endet mit 1 (das gescheiterte
ls), dbg-restricted ist nie gestartet
(null). Für Aufgabe 4 holst du die Ausgabe mit
kubectl -n tag06 logs vom
node-debugger-worker2-…-Pod – sie enthält das Token des
Clusters, also nicht in Tickets kopieren.
Fortschritt speichern? Kostenlos registrieren
Kontrollpunkt
# auf cp1
kubectl -n tag06 get pods -l app=dns-testErwartet: ein Pod 1/1 Running mit
0 Neustarts.
# auf cp1
kubectl get namespace tag06 --show-labels | cut -c1-88Erwartet: kein
pod-security.kubernetes.io/enforce unter den Labels.
Challenge
Finde heraus, auf welchen Ports CoreDNS im dns-test-Pod
lauscht und welcher Prozess dahintersteht – ohne die Pod-Definition
anzusehen.
Hinweis: ss -p zeigt Prozesse nur, wenn
es in /proc des Ziels schauen darf.
Lösung
# auf cp1
kubectl -n tag06 debug $P --image=nicolaka/netshoot:v0.15 --target=coredns \
--profile=general --container=ports -- ss -lntup
kubectl -n tag06 logs $P -c ports | cut -c1-88Erwartet:
Netid State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
udp UNCONN 0 0 *:1053 *:* users:(("coredns",pid=1,
tcp LISTEN 0 4096 *:8080 *:* users:(("coredns",pid=1,
tcp LISTEN 0 4096 *:1053 *:* users:(("coredns",pid=1,
DNS auf 1053 (UDP und TCP), Health auf 8080. Mit
--profile=baseline bleibt die Spalte Process
leer, weil SYS_PTRACE fehlt.
Fortschritt speichern? Kostenlos registrieren
Aufräumen
# auf cp1
kubectl delete namespace tag06
kubectl get pods -A | grep -c node-debuggerErwartet:
namespace "tag06" deleted
0
Interview-Notiz
- Wie debuggst du ein distroless-Image?
kubectl debugmit Ephemeral Container und--target, für Dateien des Ziels--profile=generaloder--custommit gleichem Benutzer und Volume. - Der Pod stürzt sofort ab – was nun?
--copy-tomit--set-imageund eigenem Befehl; die Kopie behält Volumes und Env, verliert aber Labels. - Knoten ohne SSH?
kubectl debug node/<name>, Dateisystem unter/host,chroot /hostfür die Werkzeuge des Knotens.
Enterprise-Notiz
- RBAC: Wer
pods/ephemeralcontainerspatchen darf, kann in fremde Container schauen;nodes/proxyundkubectl debug nodesind faktisch root auf dem Knoten. - Debug-Images zentral freigeben und spiegeln (netshoot, busybox) – unter Druck will niemand auf Docker Hub warten.
- Eingaben in Debug-Sitzungen landen im Container-Log und damit im zentralen Logging.
- Unter PSA
restrictedfunktioniert nur--profile=restricted, und auch das nur in konformen Pods; plane das in Notfall-Runbooks ein. - Ephemeral Container bleiben sichtbar, bis der Pod gelöscht oder ersetzt wird – in Audits erklärt das manch fremden Containernamen.