kubectl logs: Follow Logs from Multiple Pods (With Examples)

kubectl logs: Follow Logs from Multiple Pods (With Examples)

kubectl logs reads container output from a pod, and with the right flags it streams live logs from several pods at once. The catch is that kubectl logs has no --all-namespaces flag, and following multiple pods only works through a label selector or a workload resource. This guide covers the commands for single pods, label-selected sets, workload resources, init containers, and crashed pods, plus when a dedicated tool like stern is the better choice.

TL;DR: Use kubectl logs <pod> -f to follow one pod, kubectl logs -l app=api -f to follow a label-selected set, and --all-pods=true on a workload resource (kubectl 1.31+). There is no --all-namespaces flag; for cross-namespace tailing use a namespace loop or stern.

kubectl logs Basics: One Pod, One Container

kubectl logs prints the logs a container wrote to stdout and stderr. The simplest call targets a pod in the current namespace:

kubectl logs web-1Code language: Bash (bash)

For a pod with several containers, add -c. Without it, kubectl selects the first container and prints which one it defaulted to:

kubectl logs web-1 -c app
# Defaulted container "app" out of: [app sidecar]Code language: Bash (bash)

Common single-pod flags:

FlagEffect
--tail=NLast N lines. Default -1 (all lines) for a pod; 10 lines per pod with -l
--since=1hLines written in the last hour
--timestampsPrefix each line with a timestamp
--limit-bytes=NMax bytes to read
-n, --namespaceNamespace of the pod
--max-log-requests=NMax concurrent log streams, default 5

Follow Live Output with -f and –tail

-f (or --follow) streams new lines as the container writes them. Combine it with --tail so you see recent history first:

kubectl logs -f --tail=100 web-1Code language: Bash (bash)

For a workload resource, kubectl resolves it to one pod. A Deployment follows its current pod:

kubectl logs -f --tail=100 deployment/apiCode language: Bash (bash)

Follow Multiple Pods with a Label Selector

To stream logs from every pod that matches a label, use -l. kubectl then opens one stream per pod, up to --max-log-requests (5 by default):

kubectl logs -l app=api -fCode language: Bash (bash)

When several pods share a stream, add --prefix so each line shows which pod it came from:

kubectl logs -l app=api -f --prefixCode language: Bash (bash)

If you follow more pods than the concurrency limit allows, kubectl errors out:

Error from server: you are attempting to follow 8 log streams, but maximum allowed concurrency is 5, use –max-log-requests to increase the limit

Raise the limit explicitly:

kubectl logs -l app=api -f --max-log-requests=20Code language: Bash (bash)

The -l selector cannot be combined with a pod or resource name. Trying it produces:

error: only a selector (-l) or a POD name is allowed

Workload Resources and –all-pods

By default, a workload resource resolves to a single pod. From kubectl 1.31 you can stream all pods of a Deployment with --all-pods=true; this forces --prefix on:

kubectl logs deployment/api --all-pods=true -fCode language: Bash (bash)

The –all-namespaces Flag Does Not Exist

There is no -A or --all-namespaces on kubectl logs. The proposal for it was never merged, and the flag is absent from kubectl 1.10 through 1.35 and current master. A selector still runs in one namespace, so the working pattern is a per-namespace loop:

for ns in $(kubectl get ns -o name | cut -d/ -f2); do
  kubectl logs -n "$ns" -l app=api -f --prefix --max-log-requests=10
doneCode language: Bash (bash)

Multi-Container Pods and Init Containers

For a pod with sidecars, select the container explicitly:

kubectl logs web-1 -c sidecar -fCode language: Bash (bash)

To include all containers, init containers, and ephemeral containers in one call:

kubectl logs web-1 --all-containers=true -fCode language: Bash (bash)

Init containers are debugged with the same syntax. Add --previous to read the run that failed before a restart:

kubectl logs web-1 -c init-migrate --previousCode language: Bash (bash)

Read the Last Run with –previous

--previous reads the logs of the previous, terminated container instance. This is the fastest way to see why a pod keeps crashing:

kubectl logs web-1 --previousCode language: Bash (bash)

If there is no previous instance, kubectl says so plainly:

Error from server (BadRequest): previous terminated container “app” in pod “web-1” not found

stern and kail for Production Log Tailing

For interactive debugging across many pods, kubectl one-liners get long. Two dedicated tools do this better:

ToolApproachMulti-namespaceInstall
sternRegex or label query, color-coded, auto-restarts on pod churnYes (stern . -A)brew install stern or kubectl krew install stern
kailMatch by label, service, deployment, or pod; no args means whole clusterYesbrew install kail

Stream everything in a namespace with stern:

stern . -n productionCode language: Bash (bash)

Common Errors

ErrorCauseFix
only a selector (-l) or a POD name is allowedCombined -l with a pod/resource namePick one: selector or name
you are attempting to follow N log streams...More pods than --max-log-requests (5)Add --max-log-requests
previous terminated container ... not foundNo previous instance existsCheck the pod was actually restarted
Error from server (NotFound): pods "x" not foundWrong namespace or pod nameAdd -n or list pods first
Defaulted container "app" out of: [app sidecar]No -c on a multi-container podPass -c explicitly

FAQ

Can kubectl logs follow multiple pods at once?

Yes, with a label selector. `kubectl logs -l app=api -f` opens one stream per matching pod, up to the concurrency limit set by `–max-log-requests` (5 by default), and `–prefix` labels each line with its pod so you can tell the streams apart.

Does kubectl logs have –all-namespaces?

No. The `–all-namespaces` flag was proposed for kubectl logs but never merged, so it is absent from kubectl 1.10 through 1.35 and current master. Use a `-l` selector per namespace, a namespace loop, or stern with its `-A` flag for cross-namespace tailing.

Why does kubectl logs show only one pod for a Deployment?

By default a workload resource such as a Deployment resolves to a single pod, and kubectl picks it with a preference for running, ready pods. From kubectl 1.31 you can stream every pod of the resource with `–all-pods=true`, which forces `–prefix` on so each line shows its pod.

How do I see the logs of a crashed container?

Use `kubectl logs –previous`. It reads the previous, terminated container instance, which is where the crash output lives. If no previous instance exists, kubectl returns `previous terminated container “app” in pod “web-1” not found`.

What is the difference between kubectl logs and stern?

kubectl logs is the built-in command for one pod, container, or label-selected set, with a 5-stream concurrency default. stern is a dedicated tailing tool with regex matching, color-coded output, multi-namespace `-A`, and automatic reconnection when pods restart, which makes it better for production debugging.

Related Reading

Sergio Bremming Avatar