⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Kubernetes Interview Questions Scenario 89 of 207 in Kubernetes
Junior / Associate DevOps [L1] Kubernetes Advanced Scenarios Core Fundamentals [L1]
🎯 Target Role / Context: Kubernetes Scenario #78 (L1) · Advanced Scenarios

Q: How do you get logs from all pods of a deployment?

kubectl logs -l app=<label> or use label selector.

#Kubernetes #Advanced Scenarios #L1 #Container Orchestration #K8s
🎙️ Candidate Opening & Architectural Context
"To <strong>get logs from all pods of a deployment</strong>, select them by label: <code>kubectl logs -l app=my-app --all-containers --prefix --tail=100</code>. Running <code>kubectl logs deployment/my-app</code> reads from only one pod, which surprises many people. Interviewers ask this to check that you know labels and selectors, the default limits of the selector mode, and when kubectl is the wrong tool and a log pipeline is needed instead."
Advertisement
⚡ Recommended Practice Lab

Want to master this scenario in a live sandbox? KodeKloud's CKA & CKAD Hands-On Certification Track covers this exact problem with hands-on terminal drills.

🛠️ Production Runbook & Step-by-Step Resolution

Direct Answer: To <strong>get logs from all pods of a deployment</strong>, select them by label: <code>kubectl logs -l app=my-app --all-containers --prefix --tail=100</code>. Running <code>kubectl logs deployment/my-app</code> reads from only one pod, which surprises many people. Interviewers ask this to check that you know labels and selectors, the default limits of the selector mode, and when kubectl is the wrong tool and a log pipeline is needed instead.

1️⃣

Why kubectl logs deployment/name shows only one pod

A Deployment owns ReplicaSets, which own Pods. kubectl logs deploy/x resolves to a single pod and streams that one.

  • Single pod: kubectl picks one pod of the Deployment and prints a hint line about the chosen container.
  • Label selector: -l key=value fetches logs from every matching pod, so use the Deployment's spec.selector.matchLabels.
  • Find the labels: kubectl get deploy my-app -n prod -o jsonpath='{.spec.selector.matchLabels}'.
  • Defaults: in selector mode kubectl shows only the last 10 lines per pod unless you pass --tail.
2️⃣

Hands-on: logs from all pods of a Deployment

Combine selector, prefix and tail flags so you can tell the pods apart.

kubectl get deploy my-app -n prod -o jsonpath='{.spec.selector.matchLabels}'
# {"app":"my-app"}

kubectl logs -l app=my-app -n prod --all-containers --prefix --tail=100 --since=15m
kubectl logs -l app=my-app -n prod --prefix -f --max-log-requests=20
  • Prefix: --prefix adds [pod/name/container] to each line.
  • All containers: --all-containers includes sidecars and init containers' output where available.
  • Follow: -f streams live. kubectl opens at most 5 concurrent streams by default; raise it with --max-log-requests.
  • Time window: --since=15m or --since-time limits output.
Advertisement
3️⃣

Verify coverage and narrow down the noise

Make sure you actually saw every replica, then filter.

kubectl get pods -l app=my-app -n prod -o name
kubectl logs -l app=my-app -n prod --prefix --tail=500 | grep -i "error"
kubectl logs my-app-7d9c8b6f5-abcde -n prod -c app --tail=50
  • Count pods: compare the pod list with the prefixes you see: kubectl get pods -l app=my-app -n prod.
  • Filter: pipe to grep -i error or use jq if logs are JSON.
  • Single pod deep dive: use kubectl logs <pod> -c <container> for one container.
  • Quiet pods: a pod with no lines inside the window may simply be idle; widen --since.
4️⃣

Failure modes and edge cases

Selector-based logs are a convenience, not a complete record.

  • Crashed containers: add --previous to read the last terminated instance; selector mode supports it per pod that has one.
  • Deleted pods: logs vanish with the pod and are also rotated by the kubelet on the node, so completed history is lost.
  • Stream cap: with more than 5 replicas and -f you get an error unless --max-log-requests is increased.
  • Overlapping labels: a broad selector such as app=web may pull in pods from other Deployments; add more labels.
  • RBAC: you need get on pods/log.
Pro Tip: <code>kubectl logs</code> reads from the node's container log files via the kubelet. If the pod or node is gone, so are the logs.
5️⃣

Production practice: ship logs off the node

For anything beyond a quick check, use a log pipeline.

stern my-app -n prod --since 15m --tail 50
# or, using only kubectl:
kubectl logs -l app=my-app -n prod --prefix --tail=-1 > /tmp/my-app-all.log
  • Stern: the open-source stern my-app -n prod tails all matching pods with colors and picks up new pods automatically.
  • Central logging: ship stdout to Loki, Elasticsearch or a cloud logging service with a DaemonSet agent such as Fluent Bit.
  • Structure: log JSON with a request ID so you can correlate across replicas.
  • Retention: central storage keeps logs after pod rescheduling and node replacement.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: Always pair <code>-l</code> with <code>--prefix</code> and an explicit <code>--tail</code>, since selector mode defaults to 10 lines per pod and hides which replica spoke."
⚡ 60-Second Elevator Pitch Talking Points
  • <code>kubectl logs deploy/x</code> only reads one pod, so for all replicas I use <code>kubectl logs -l app=x --all-containers --prefix --tail=100</code>.
  • I confirm coverage by comparing prefixes with <code>kubectl get pods -l</code> and raise <code>--max-log-requests</code> when following many pods.
  • For real operations I ship logs to a central store and use stern for live tailing.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. Assuming kubectl logs deployment/name covers every replica It selects a single pod, so you may debug the one healthy replica while the failing one stays unread.
  2. Forgetting --tail in selector mode kubectl returns only the last 10 lines per pod by default, so earlier errors are missed.
  3. Following more than 5 pods without --max-log-requests kubectl refuses concurrent streams beyond the default limit of 5 and errors out.
  4. Relying on kubectl logs as the only log retention Logs are tied to pod and node lifecycle and are rotated by the kubelet, so evidence disappears after restarts or evictions.
Advertisement
📥 FREE DOWNLOAD · 101-PAGE COMPANION HANDBOOK
Studying for Kubernetes & SRE Technical Rounds?
Download the complete 100-question PDF field guide covering all 11 core modules with offline diagnostic runbooks.
📥 Download PDF (Free) Read Online Guide →
Want more Kubernetes scenarios?
Explore our complete collection of scenario-based Kubernetes interview runbooks.
Browse All Kubernetes Questions →