kubectl logs

How to view pod logs in Kubernetes

kubectl logs reads what the container writes to standard output (stdout and stderr). These are the commands for everyday cases: a specific container, the run that died, live, only the last hour or several pods at once.

A pod’s log

kubectl logs <pod> -n <namespace>
kubectl logs <pod> -n <namespace> -c <container>
kubectl logs <pod> -n <namespace> --all-containers

Live, with timestamps

kubectl logs -f <pod> -n <namespace> --timestamps
kubectl logs <pod> -n <namespace> --tail=200 --since=1h

The run that died

After a restart (CrashLoopBackOff, OOMKilled), the current log belongs to the new run. --previous shows the previous one:

kubectl logs <pod> -n <namespace> --previous

Several pods at once

By label, or through the Deployment (which picks one of its pods). With a label, kubectl opens at most 5 live connections by default; raise it with --max-log-requests:

kubectl logs -l app=<app> -n <namespace> --prefix --tail=50
kubectl logs deployment/<name> -n <namespace>

Init containers and Jobs

kubectl logs <pod> -n <namespace> -c <init-container>
kubectl logs job/<job> -n <namespace>

What kubectl logs does not show

  • Log files written inside the container: only what goes to stdout and stderr.
  • Logs of a deleted pod: when the pod goes, its log goes with it. For history, use a centralized solution (Loki, Elastic, AKS Log Analytics or EKS CloudWatch).
  • Logs of runs older than the previous one: the kubelet keeps only the current and the last.

Without a terminal, in Kubepier

On Kubepier’s Pro plan, live logs open straight from the pod, with a timestamp on each line and a container picker, from any cluster, in the browser or on your phone, with no kubectl or kubeconfig on the machine.

Start for free