Drill: Get Logs from Multi-Container Pods¶
Goal¶
Retrieve logs from specific containers in multi-container pods, including init containers and previous crashed instances.
Setup¶
- kubectl configured with cluster access
- A pod with multiple containers or init containers (or create one for testing)
Commands¶
Get logs from the default container:
Specify a container in a multi-container pod:
List containers in a pod:
List init containers:
Get logs from an init container:
Get logs from previous (crashed) instance:
Follow logs in real time:
Get logs from all containers in a pod:
Get logs with timestamps:
Get logs from the last hour:
Tail the last N lines:
What to Look For¶
- Init container logs reveal setup failures before the main containers start
--previousshows why a container crashed before it restarted- Sidecar container logs (e.g., envoy, fluentd) often hold routing or logging clues
- Timestamps help correlate events across containers
Common Mistakes¶
- Not specifying
-cand getting an error when a pod has multiple containers - Forgetting
--previousand only seeing the current (healthy) instance logs - Not checking init container logs when a pod is stuck in Init state
- Using
-fon a CrashLoopBackOff pod (the follow ends when the container exits)
Cleanup¶
No cleanup needed. These are read-only commands.