Interview's MateKubernetes
Interview's Mate

Kubernetes

K8s · Workloads · GitOps · Cloud

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Kubernetes topics
Lesson 13/ 160
01. Kubernetes Fundamentals & Architecture

Viewing Logs, Executing Commands & Port-Forwarding (logs, exec, port-forward)

Debug running pods with logs, exec shells, and local port-forward tunnels.

Beginnerlogs · exec · port-forward3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Logs are the flight recorder, exec is stepping onto the cockpit floor, and port-forward is a temporary service tunnel into the aircraft. The simple Kubernetes rule is: Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource.

Think of it like…

Logs are the flight recorder, exec is stepping onto the cockpit floor, and port-forward is a temporary service tunnel into the aircraft.

Where you’ve seen it

A cluster operator validates a small change and runs kubectl logs deploy/api --all-containers --tail=100; kubectl exec -it api-0 -- sh; kubectl port-forward svc/api 8080:80. Expected evidence: Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource. If the run instead shows this symptom, stop and restore the last known state: Logs can rotate away and exec fails if the process image lacks a shell, while port-forward can tunnel only traffic supported by the target workload.

Words you’ll hear

logs
For this lesson, logs is the controlling idea: Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource.
exec
exec is the checkpoint; run kubectl logs deploy/api --all-containers --tail=100; kubectl exec -it api-0 -- sh; kubectl port-forward svc/api 8080:80 and inspect the owner chain. Logs can rotate away and exec fails if the process image lacks a shell, while port-forward can tunnel only traffic supported by the target workload.
port-forward
port-forward is the outcome to preserve; the cost is explicit: These are fast diagnostic paths, but they are interactive, terminate with the client session, and should not replace telemetry or a supported debug interface.
Verification evidence
The evidence command is kubectl logs deploy/api --all-containers --tail=100; kubectl exec -it api-0 -- sh; kubectl port-forward svc/api 8080:80. It connects the declaration to this behavior: Debug running pods with logs, exec shells, and local port-forward tunnels.

Visual diagram

Loading interactive diagram

Theory & deep dive

How logs reaches the intended state

Debug running pods with logs, exec shells, and local port-forward tunnels. Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource.

The concrete checkpoint is kubectl logs deploy/api --all-containers --tail=100; kubectl exec -it api-0 -- sh; kubectl port-forward svc/api 8080:80; compare the logs owner, exec state, and port-forward outcome instead of treating YAML acceptance as proof.

Verify exec, not only object creation

Run kubectl logs deploy/api --all-containers --tail=100; kubectl exec -it api-0 -- sh; kubectl port-forward svc/api 8080:80 after the change and correlate the result with Events and the owning object. Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource.

Key takeaway: logs is implemented only when this invariant remains observable: Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource.

exec: failure boundary and production cost

Logs can rotate away and exec fails if the process image lacks a shell, while port-forward can tunnel only traffic supported by the target workload. The prerequisite contract is concrete: Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource.

These are fast diagnostic paths, but they are interactive, terminate with the client session, and should not replace telemetry or a supported debug interface. A safe design limits blast radius, records the last good configuration, and uses kubectl logs deploy/api --all-containers --tail=100; kubectl exec -it api-0 -- sh; kubectl port-forward svc/api 8080:80 as a deterministic rollback checkpoint.

Recover from exec drift

Capture the current state with kubectl logs deploy/api --all-containers --tail=100; kubectl exec -it api-0 -- sh; kubectl port-forward svc/api 8080:80, then correct the controlling manifest rather than patching a child object; the relevant divergence signal is specific: Logs can rotate away and exec fails if the process image lacks a shell, while port-forward can tunnel only traffic supported by the target workload.

Key takeaway: Accept the trade-off consciously: These are fast diagnostic paths, but they are interactive, terminate with the client session, and should not replace telemetry or a supported debug interface.

How it works, step by step

Step 1 of 4
Operator intent

1. Model the cluster contract

Start from a minimal API, context, or node condition and make this contract explicit: Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource.

logs contract → owned state
Key numbers at this step
Goal
Debug running pods with logs, exec shells, and local port-forward tunnels.
Owner
logs

When to use it & trade-offs

What it is

Debug running pods with logs, exec shells, and local port-forward tunnels. Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource.

Why it matters: Logs can rotate away and exec fails if the process image lacks a shell, while port-forward can tunnel only traffic supported by the target workload. The invariant at risk is specific: Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource. The quickest fact check before changing the design is kubectl logs deploy/api --all-containers --tail=100; kubectl exec -it api-0 -- sh; kubectl port-forward svc/api 8080:80.

When to reach for it

  • Use logs to enforce this invariant: Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource.
  • Use exec evidence from kubectl logs deploy/api --all-containers --tail=100; kubectl exec -it api-0 -- sh; kubectl port-forward svc/api 8080:80 in reviews and incident timelines. Debug running pods with logs, exec shells, and local port-forward tunnels.
  • Choose the platform-level abstraction route only when this cost is justified by governance or scale: These are fast diagnostic paths, but they are interactive, terminate with the client session, and should not replace telemetry or a supported debug interface.

When to avoid or be careful

  • Avoid assuming logs is complete after YAML submission; the required behavior is concrete: Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource.
  • Avoid treating the next symptom as random; check the owner and state with kubectl logs deploy/api --all-containers --tail=100; kubectl exec -it api-0 -- sh; kubectl port-forward svc/api 8080:80 first: Logs can rotate away and exec fails if the process image lacks a shell, while port-forward can tunnel only traffic supported by the target workload.
  • Avoid adopting a heavier platform wrapper before measuring its cost: These are fast diagnostic paths, but they are interactive, terminate with the client session, and should not replace telemetry or a supported debug interface.

Native, manual, and managed paths for logs

Native cluster primitive
Best for: Debug running pods with logs, exec shells, and local port-forward tunnels. A concrete incident check is kubectl logs deploy/api --all-containers --tail=100; kubectl exec -it api-0 -- sh; kubectl port-forward svc/api 8080:80.

Strengths: Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource.

Watch out: These are fast diagnostic paths, but they are interactive, terminate with the client session, and should not replace telemetry or a supported debug interface. Recovery must also recognize this signal: Logs can rotate away and exec fails if the process image lacks a shell, while port-forward can tunnel only traffic supported by the target workload.

One-off administrative action
Best for: Quickly testing the exec assumption with kubectl logs deploy/api --all-containers --tail=100; kubectl exec -it api-0 -- sh; kubectl port-forward svc/api 8080:80; the expected invariant is: Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource.

Strengths: kubectl logs deploy/api --all-containers --tail=100; kubectl exec -it api-0 -- sh; kubectl port-forward svc/api 8080:80 gives immediate feedback without first building a platform layer.

Watch out: A manual change can hide drift, and unlike logs, it does not make this symptom self-documenting: Logs can rotate away and exec fails if the process image lacks a shell, while port-forward can tunnel only traffic supported by the target workload.

Platform-level abstraction
Best for: Teams that need port-forward integrated with guardrails; the evidence path is kubectl logs deploy/api --all-containers --tail=100; kubectl exec -it api-0 -- sh; kubectl port-forward svc/api 8080:80.

Strengths: Platform controls can wrap this exact behavior in policy or dashboards: Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource.

Watch out: The added platform ownership buys value at this cost: These are fast diagnostic paths, but they are interactive, terminate with the client session, and should not replace telemetry or a supported debug interface.

Interview signals & numbers to know

When this topic comes up

  • Interview anchor: Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource.
  • Trace logs to port-forward through exec; the invariant is: Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource.
  • Recognize the symptom: Logs can rotate away and exec fails if the process image lacks a shell, while port-forward can tunnel only traffic supported by the target workload.
  • Justify the cost: These are fast diagnostic paths, but they are interactive, terminate with the client session, and should not replace telemetry or a supported debug interface.
  • Verify with kubectl logs deploy/api --all-containers --tail=100; kubectl exec -it api-0 -- sh; kubectl port-forward svc/api 8080:80 instead of only inspecting YAML.

Benchmarks & rules of thumb

100 -
lines is a practical initial tail for incident triage
2 -
ports must be distinguished in a local:remote port-forward
0 -
port-forward should be mistaken for a production load balancer

Common mistakes & follow-ups

Common interview mistakes

  • Debugging only the child object misses the logs owner contract: Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource.
  • Ignoring this telltale condition leads to unrelated changes: Logs can rotate away and exec fails if the process image lacks a shell, while port-forward can tunnel only traffic supported by the target workload.
  • Repeating kubectl logs deploy/api --all-containers --tail=100; kubectl exec -it api-0 -- sh; kubectl port-forward svc/api 8080:80 without an owner for remediation wastes retry cost. These are fast diagnostic paths, but they are interactive, terminate with the client session, and should not replace telemetry or a supported debug interface.

Likely interviewer follow-ups

  • How would API output and runtime evidence prove this behavior: kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource?
  • Which owner object, logs, and Events would you inspect for this symptom-Logs can rotate away and exec fails if the process image lacks a shell, while port-forward can tunnel only traffic supported by the target workload-?
  • At what threshold would this cost become unacceptable: These are fast diagnostic paths, but they are interactive, terminate with the client session, and should not replace telemetry or a supported debug interface?
Interview tip: Interview sequence: state the invariant, run kubectl logs deploy/api --all-containers --tail=100; kubectl exec -it api-0 -- sh; kubectl port-forward svc/api 8080:80, name the failure, then justify the cost. Invariant: Kubectl logs reads container output, exec opens a command stream into a running container, and port-forward creates a local authenticated tunnel to a Pod or selected resource. Failure: Logs can rotate away and exec fails if the process image lacks a shell, while port-forward can tunnel only traffic supported by the target workload. Cost: These are fast diagnostic paths, but they are interactive, terminate with the client session, and should not replace telemetry or a supported debug interface.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Viewing Logs, Executing Commands & Port-Forwarding (logs, exec, port-forward) with generative AI interview Q&A and hands-on practice.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Labels, Selectors & Annotations
Next topic
Resource Requests & Limits (CPU millicores, Memory Mi/Gi, QoS Classes)