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

← All kubectl commands · Docker vs Kubernetes