Kubernetes
Logs
Pod Management
Kubectl
DevOps Tips

How to get logs of two pods without opening two terminals and using kubectl tail command?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

You do not need two terminals to watch two Kubernetes pods at once. In many cases, plain kubectl logs can stream multiple pods in one command if you target them through a label selector or a workload resource. The cleanest solution is usually to select the pods by label instead of hard-coding their names.

Use a label selector with kubectl logs

If the two pods share a label, this is the simplest built-in approach:

bash
1kubectl logs -f -n production \
2  -l app=my-api \
3  --all-containers=true \
4  --prefix

This gives you one combined stream.

The useful pieces are:

  • '-f to keep following the logs'
  • '-l app=my-api to select multiple matching pods'
  • '--all-containers=true if the pods have sidecars or multiple containers'
  • '--prefix to show which pod and container produced each line'

Without --prefix, a combined stream becomes much harder to read.

Target the workload instead of pod names

If the pods belong to the same Deployment, you can often use the workload name rather than individual pod names.

bash
1kubectl logs -f deployment/my-api \
2  -n production \
3  --all-pods=true \
4  --all-containers=true

This is useful because pod names are ephemeral. A rollout or restart changes the pod names, but the Deployment name stays stable.

Improve readability with time filters

Mixed logs get noisy quickly, so narrow the output when debugging.

bash
1kubectl logs -f -n production \
2  -l app=my-api \
3  --all-containers=true \
4  --prefix \
5  --since=10m \
6  --timestamps=true

This limits the stream to recent lines and adds timestamps, which makes correlating events between pods much easier.

When the pods are unrelated

If the two pods do not share a useful label and you really need exactly those two names in one terminal, kubectl logs becomes awkward. That is the point where a tool such as Stern is often a better fit.

bash
stern 'api-7c8d9f6b5f-abc12|worker-5db6f8f79d-xy987' -n production

Stern is popular because it handles multiple pods naturally and keeps following logs as pods restart.

Still, before adding another tool, ask whether the labels should be improved. In a well-labeled cluster, related pods are usually easy to select together.

There is no standard kubectl tail command

A common source of confusion is the phrase kubectl tail. Standard kubectl has kubectl logs. If you see tail in a workflow, it is usually:

  • a shell alias
  • a plugin
  • a wrapper command

That matters because the built-in logging behavior is documented under kubectl logs, not under a separate standard tail subcommand.

Common Pitfalls

The biggest pitfall is selecting too many pods with a broad label. Always verify the selector with kubectl get pods -l ... before assuming the combined stream only covers the two pods you care about.

Another issue is forgetting --prefix. Once multiple streams are merged, source labels are essential.

It is also easy to hard-code pod names from a Deployment and then wonder why the command breaks after a restart or rollout.

Finally, if the pods are truly unrelated, do not force a fragile one-liner when a dedicated multi-pod log tool or better labeling would be clearer.

Summary

  • Use kubectl logs -f -l ... --prefix when the pods share labels.
  • Use a workload target such as a Deployment when pod names are ephemeral.
  • Add --timestamps and --since to keep mixed logs readable.
  • Use Stern or a similar tool only when you need multiple arbitrary pod names in one stream.
  • Good labels are usually the cleanest long-term solution.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design