debug
kubectl logs
Prints container logs from a pod, deployment, or other workload. Supports streaming, previous-instance recovery after crashes, and selecting a specific container in multi-container pods.
kubectl logs [-f] [-p] [--container CONTAINER] POD [flags] Common flags
| Flag | Purpose |
|---|---|
| -f, --follow | Stream logs continuously (like tail -f). |
| -c, --container | Container name inside a multi-container pod. |
| -p, --previous | Show logs from the previous terminated container instance. |
| --tail | Lines of recent log file to display (default -1, all). |
| --since | Only logs newer than a relative duration (e.g. 5m, 1h). |
| --timestamps | Prefix each log line with an RFC3339 timestamp. |
| -l, --selector | Aggregate logs from all pods matching a label selector. |
| --all-containers | Print logs from every container in the pod. |
Examples
kubectl logs -f deploy/api Follow logs from a Deployment's currently active pod.
kubectl logs web-abc -c sidecar --tail=100 Last 100 lines from the sidecar container.
kubectl logs -p web-abc Retrieve logs from the crashed previous instance.
kubectl logs -l app=api --max-log-requests=10 --prefix Merge logs across pods sharing a label.
Gotcha
Without --tail on very chatty pods you flood the terminal. -p only works if the previous container actually terminated on that node.
Related commands
kubectl exec
Executes a command inside a running container. Use -it for an interactive TTY shell; omit them for a one-shot command whose stdout you want to capture for scripts.
kubectl describe
Shows a human-readable, aggregated view of a resource including its spec, status, related events, and controllers. It is the go-to command for debugging why a pod is Pending, CrashLoopBackOff, or otherwise misbehaving.
kubectl get
Lists one or more resources of a given type from the cluster. It is the primary read command for pods, services, deployments, and any other Kubernetes object, with flexible output formats for scripting and inspection.
kubectl port-forward
Forwards one or more local ports to a pod, service, or deployment inside the cluster via the API server. Ideal for reaching internal services (databases, admin UIs) from your laptop without exposing them publicly.
kubectl cp
Copies files and directories between your local filesystem and a container in a pod. Uses tar under the hood, so the target container must have a working tar binary on its PATH.