List, inspect, exec into, tail logs from, copy files to, port-forward to and delete Pods without guessing flags.
The problem
Most day-to-day Kubernetes work is looking at Pods: what is running where, what it printed, how to get a shell in it, and how to reach it from your laptop. Knowing a dozen flags saves the trial and error during an incident.
How it works
- `kubectl get pods -o wide` adds the node and Pod IP; `-w` watches changes; `--show-labels` helps when a selector matches nothing.
- `kubectl logs <pod> -c <container>` picks a container; `--previous` shows the last terminated instance and is the first command for a restarting container; `-f`, `--tail=N` and `--since=10m` bound the output.
- `kubectl logs -l app=web --all-containers --prefix` reads many Pods at once; with a selector, the last 10 lines per container are shown unless you set `--tail`, and with `-f` it fails beyond `--max-log-requests` (default 5) Pods.
- `kubectl exec -it <pod> -c <container> -- sh` opens a shell; put `--` before the command so kubectl does not parse its flags. Images without a shell need `kubectl debug` instead.
- `kubectl cp <pod>:/path ./local -c <container>` needs `tar` inside the image, so it fails on distroless images; for big or repeated transfers use a volume or object storage.
- `kubectl port-forward pod/<pod> 8080:80` binds localhost by default and is a debugging tool, not an ingress path; it dies when the Pod is replaced.
- `kubectl delete pod <pod>` is safe for controller-managed Pods (a replacement is created), but it respects `terminationGracePeriodSeconds`; `--grace-period=0 --force` skips graceful shutdown and should be a last resort.
Examples
Find and read the logs of a misbehaving Pod.
kubectl get pods -n shop -l app=checkout -o wide
kubectl logs checkout-6c9f7d8b5-x2k4p -n shop --tail=100
# Container restarted? Read the previous instance
kubectl logs checkout-6c9f7d8b5-x2k4p -n shop --previous
# Pick a container in a multi-container Pod
kubectl logs checkout-6c9f7d8b5-x2k4p -n shop -c sidecar --since=15m
# All Pods of the app, prefixed with the Pod name
kubectl logs -n shop -l app=checkout --all-containers --prefix --tail=20
Get a shell, copy a file out and forward a port.
kubectl exec -it checkout-6c9f7d8b5-x2k4p -n shop -c app -- sh
# One-off command without a TTY
kubectl exec checkout-6c9f7d8b5-x2k4p -n shop -- env
# Copy a file out (needs tar in the image)
kubectl cp shop/checkout-6c9f7d8b5-x2k4p:/tmp/heap.hprof ./heap.hprof -c app
# Local 8080 to container port 80; Ctrl-C to stop
kubectl port-forward -n shop pod/checkout-6c9f7d8b5-x2k4p 8080:80
Delete Pods: gracefully, by selector, or forced as a last resort.
kubectl delete pod checkout-6c9f7d8b5-x2k4p -n shop
# Recreate every Pod of an app one by one: prefer rollout restart
kubectl rollout restart deploy/checkout -n shop
# Stuck Terminating (for example a lost node); skips graceful shutdown
kubectl delete pod checkout-6c9f7d8b5-x2k4p -n shop --grace-period=0 --force
In practice
- stern / kubetail — Community tools that tail logs from many Pods and containers with colour and regex filtering, a common upgrade over `kubectl logs -l`.
- kubectl port-forward — Handy for reaching an internal admin UI or database; it tunnels through the API server, so it is not for production traffic.
- In practice — Logs of a deleted Pod are gone with it unless a node agent (for example Fluent Bit, Vector or Promtail) ships them elsewhere; do not rely on `kubectl logs` for history.
Trade-offs
Pros
- Fast, no extra tooling or cluster setup
- Works the same on any conformant cluster
Cons
- Logs and exec sessions are ephemeral and not audited by default
- Single-Pod focus; fleet-wide questions need metrics or log aggregation