0
0
Kubernetesdevops~10 mins

kubectl logs for debugging in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - kubectl logs for debugging
Start: Identify Pod
Run kubectl logs <pod-name>
Fetch logs from Pod's container
Display logs in terminal
Analyze logs for errors or info
Decide next step: Fix issue or get more logs
Use options like -c, --since, --tail
End
This flow shows how to use kubectl logs command to fetch and analyze logs from a Kubernetes pod for debugging.
Execution Sample
Kubernetes
kubectl logs my-pod
kubectl logs my-pod -c my-container
kubectl logs my-pod --since=1h
kubectl logs my-pod --tail=50
These commands fetch logs from a pod, optionally specifying container, time range, or number of lines.
Process Table
StepCommand RunActionResult/Output
1kubectl logs my-podFetch logs from default container in pod 'my-pod'Logs displayed in terminal from pod's default container
2kubectl logs my-pod -c my-containerFetch logs from container 'my-container' in pod 'my-pod'Logs displayed for specified container
3kubectl logs my-pod --since=1hFetch logs from last 1 hourLogs from last 1 hour shown
4kubectl logs my-pod --tail=50Fetch last 50 lines of logsOnly last 50 log lines displayed
5kubectl logs unknown-podTry fetching logs from non-existent podError: pod not found
6kubectl logs my-pod --followStream logs liveLogs stream continuously until stopped
7ExitNo more commandsEnd of debugging session
💡 User stops after viewing logs or encounters error like pod not found
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
pod-namemy-podmy-podmy-podmy-podmy-podunknown-podmy-podmy-pod
container-namedefaultdefaultmy-containermy-containerdefaultdefaultdefaultdefault
logs-outputemptylogs from default containerlogs from my-containerlogs last 1 hourlast 50 lines logserror: pod not foundstreaming logsstreaming logs or stopped
Key Moments - 3 Insights
Why do I get an error when running kubectl logs on a pod?
If the pod name is wrong or the pod is not running, kubectl logs shows an error. See step 5 in execution_table where 'unknown-pod' causes 'pod not found' error.
How do I get logs from a specific container in a pod with multiple containers?
Use the -c option with the container name. Step 2 shows 'kubectl logs my-pod -c my-container' fetching logs from the specified container.
How can I see only recent logs or limit the amount of logs?
Use options like --since to get logs from a time range or --tail to limit lines. Steps 3 and 4 demonstrate these options.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what output do you get when running 'kubectl logs my-pod --tail=50'?
ALogs from the last 1 hour
BError: pod not found
CLogs from the last 50 lines
DStreaming logs continuously
💡 Hint
Check step 4 in the execution_table for the command with --tail=50
At which step does the command fail due to a non-existent pod?
AStep 5
BStep 3
CStep 2
DStep 6
💡 Hint
Look for the error message 'pod not found' in the execution_table
If you want to see logs from a specific container, which command would you use?
Akubectl logs my-pod --since=1h
Bkubectl logs my-pod -c my-container
Ckubectl logs my-pod --tail=50
Dkubectl logs unknown-pod
💡 Hint
Refer to step 2 in the execution_table where container is specified with -c
Concept Snapshot
kubectl logs <pod-name> - Fetch logs from a pod's default container
Use -c <container-name> to specify container in multi-container pods
Options --since and --tail limit logs by time or lines
--follow streams logs live for real-time debugging
Errors occur if pod name is wrong or pod is not running
Full Transcript
This visual execution shows how to use the kubectl logs command to debug Kubernetes pods. First, identify the pod name. Then run 'kubectl logs <pod-name>' to fetch logs from the default container. Use -c to specify a container if there are multiple. Options like --since and --tail help limit logs by time or number of lines. If the pod name is incorrect, an error appears. You can also stream logs live with --follow. This step-by-step trace helps beginners see how commands fetch logs and handle errors.