Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
Step by step
- One command is often enough (no interactive shell needed):
kubectl exec api-6d8f9c7b5d-x2kq9 -n shop -- cat /app/config/app.yaml
kubectl exec api-6d8f9c7b5d-x2kq9 -n shop -- ls -la /app
kubectl exec deploy/api -n shop -- printenv DATABASE_HOSTdeploy/api picks one Pod of the Deployment for you.
- Interactive shell — try
shifbashis not there:
kubectl exec -it api-6d8f9c7b5d-x2kq9 -n shop -c api -- sh
/app $ wget -qO- http://localhost:8080/healthz
/app $ exit- No shell in the image? (
exec: "sh": executable file not found) — add an ephemeral debug container that shares the app's process and network:
kubectl debug -it api-6d8f9c7b5d-x2kq9 -n shop --image=busybox:1.37 --target=api -- sh
/ # ps
/ # nslookup postgres.shop.svc.cluster.local
/ # wget -qO- http://127.0.0.1:8080/healthz--target=api lets you see the app's processes; the files of the app are under /proc/1/root/.
- Network tests from inside the cluster without touching the app — a throwaway Pod:
kubectl run nettest -n shop --rm -it --image=nicolaka/netshoot:v0.13 --restart=Never -- bash- A shell on a node (the node's file system is under
/host):
kubectl debug node/worker1 -it --image=busybox:1.37
/ # chroot /host
journalctl -u kubelet -n 50Delete the debug Pod after: kubectl get pods shows a node-debugger-… Pod.
- Copy files in and out:
kubectl cp shop/api-6d8f9c7b5d-x2kq9:/app/logs/app.log ./app.log -c api
kubectl cp ./fix.sql shop/postgres-0:/tmp/fix.sqlkubectl cp needs tar inside the container.
Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
executable file not found for sh/bash | Distroless or scratch image. | kubectl debug … --image=busybox:1.37 --target=NAME. |
Defaulted container "api" out of: api, istio-proxy | Several containers; it chose one. | Add -c NAME. |
Forbidden … pods/exec | No RBAC right to exec. | Ask for create on pods/exec (it is a powerful right — keep it for admins). |
unable to upgrade connection | The API server can not reach the kubelet (firewall, proxy). | Check port 10250 between control plane and nodes. |
| Ephemeral container not allowed | Old cluster or a policy blocks it. | Kubernetes 1.25+; or use a copy: kubectl debug POD --copy-to=NAME. |
| Changes made in the shell are gone | Containers are rebuilt from the image on every restart. | Fix the image or the manifest, never the running Pod. |
Go further
- Command pages: kubectl, crictl.
- Next how-tos: Get the logs of a crashed Pod, Port-forward to a service, Test if a remote port is open, Detect a shell in a container with Falco.
- Learn it: kubectl debug in daily work, CKA — Troubleshoot the network, CKS — Investigate at runtime.
- Security: exec into Pods is logged by the audit log and caught by Falco — Install Falco. Practise: Lab: catch a shell in a container with Falco.
1The image has no shell. How do you still get one next to the app?
kubectl debug -it POD --image=busybox:1.37 --target=CONTAINER -- sh (an ephemeral container).
2Why should you never fix production by editing files inside a running Pod?
The change is lost at the next restart and nobody can review it; fix the image or manifest instead.