Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 33Kubernetes · kubectl

How to open a shell in a running Pod

Sometimes you must look inside: is the config file there, can the Pod reach the database, what does DNS answer? kubectl exec opens a shell in a running container. Many modern images have no shell at all (distroless, scratch) — then kubectl debug adds a temporary tool container next to the app. This how-to shows both, plus a shell on a node, copying files, and the safety rules for production.

  • 5 Lessen
  • 6 Commando's

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

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

33.1

The quick answer

kubectl exec -it api-6d8f9c7b5d-x2kq9 -n shop -- sh
kubectl exec -it deploy/api -n shop -c api -- bash
kubectl exec api-6d8f9c7b5d-x2kq9 -n shop -- env
kubectl debug -it api-6d8f9c7b5d-x2kq9 -n shop --image=busybox:1.37 --target=api -- sh

Everything after -- runs inside the container.

33.2

Step by step

  1. 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_HOST

deploy/api picks one Pod of the Deployment for you.

  1. Interactive shell — try sh if bash is not there:
kubectl exec -it api-6d8f9c7b5d-x2kq9 -n shop -c api -- sh
/app $ wget -qO- http://localhost:8080/healthz
/app $ exit
  1. 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/.

  1. 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
  1. 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 50

Delete the debug Pod after: kubectl get pods shows a node-debugger-… Pod.

  1. 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.sql

kubectl cp needs tar inside the container.

33.3

Check that it worked

You got your answer (the file, the env value, the DNS name, the HTTP reply). Then leave the shell, and delete debug Pods:

kubectl get pods -A | grep -E 'debugger|nettest|debug'

33.4

Common problems

ProblemCauseFix
executable file not found for sh/bashDistroless or scratch image.kubectl debug … --image=busybox:1.37 --target=NAME.
Defaulted container "api" out of: api, istio-proxySeveral containers; it chose one.Add -c NAME.
Forbidden … pods/execNo RBAC right to exec.Ask for create on pods/exec (it is a powerful right — keep it for admins).
unable to upgrade connectionThe API server can not reach the kubelet (firewall, proxy).Check port 10250 between control plane and nodes.
Ephemeral container not allowedOld 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 goneContainers are rebuilt from the image on every restart.Fix the image or the manifest, never the running Pod.

33.5

Go further

1The image has no shell. How do you still get one next to the app?
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.

Alle commando's in dit hoofdstuk