Viewing Logs, Executing Commands & Port-Forwarding (logs, exec, port-forward)
Debug running pods with logs, exec shells, and local port-forward tunnels.
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:80and 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
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.
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.
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.
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.
How it works, step by step
Step 1 of 41. 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.
- 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:80in 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:80first: 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
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.
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.
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:80without 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?
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
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.