Kubectl Logs and Exec

Debug live workloads: stream logs and run commands in pods.

  • kubectl logs
  • kubectl exec
  • crashed container
  • follow logs
  • kubectl cp

Kubectl Logs and Exec

Debug live workloads: stream logs and run commands in pods.

Commands

CommandWhat it does
kubectl logs -f deploy/apiFollow aggregated deployment logs
kubectl logs -p <pod>Previous (crashed) container logs
kubectl logs <pod> --since=10m --tail=200Bounded recent logs
kubectl exec -it <pod> -- shInteractive shell
kubectl cp <pod>:/app/out ./outCopy files from a pod

Example

kubectl logs -f deploy/api -n shop
kubectl logs -p api-7d9c5f6b9-abcde -n shop
kubectl exec -it api-7d9c5f6b9-abcde -n shop -- sh

FAQ

Logs are gone after restart — why?

kubelet keeps few rotated files; ship stdout to Loki/ES for anything beyond quick debugging.

exec fails with no shell?

Distroless images have none — use kubectl debug with an ephemeral debug container instead.

Preguntas frecuentes

Logs are gone after restart — why?
kubelet keeps few rotated files; ship stdout to Loki/ES for anything beyond quick debugging.
exec fails with no shell?
Distroless images have none — use kubectl debug with an ephemeral debug container instead.

Kubernetes