0
0
Kubernetesdevops~20 mins

kubectl logs for debugging in Kubernetes - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kubectl Logs Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Viewing logs of a specific container in a pod

You have a pod named webapp-1234 with two containers: frontend and backend. Which kubectl logs command will show logs only from the backend container?

Akubectl logs webapp-1234
Bkubectl logs webapp-1234 --container=frontend
Ckubectl logs webapp-1234 -c backend
Dkubectl logs backend
Attempts:
2 left
💡 Hint

Use the -c or --container flag to specify the container name.

💻 Command Output
intermediate
2:00remaining
Getting logs from previous container instance

A pod restarted due to a crash. You want to see the logs from the previous container instance to understand what caused the crash. Which command will show those logs?

Akubectl logs pod-name --previous
Bkubectl logs pod-name --all-containers
Ckubectl logs pod-name --follow
Dkubectl logs pod-name --timestamps
Attempts:
2 left
💡 Hint

Look for a flag that fetches logs from the last terminated container.

Configuration
advanced
2:00remaining
Filtering logs by timestamps using kubectl

You want to fetch logs from a pod named api-server but only want to see logs starting from a specific time, for example, logs after 10 minutes ago. Which approach is correct?

Akubectl logs api-server --from-time=10m
Bkubectl logs api-server --since-time='2024-01-01T10:00:00Z'
Ckubectl logs api-server --tail=10m
Dkubectl logs api-server --since=10m
Attempts:
2 left
💡 Hint

Check the flag that filters logs by duration from now.

Troubleshoot
advanced
2:00remaining
Error when fetching logs from a pod

You run kubectl logs mypod but get the error: Error from server (BadRequest): a container name must be specified for pod "mypod". What is the most likely cause?

AThe pod <code>mypod</code> does not exist in the current namespace.
BYou forgot to specify the container name with <code>-c</code> because the pod has multiple containers.
CYou need to add <code>--all-containers</code> to get logs from all containers.
DThe pod is not running, so logs cannot be fetched.
Attempts:
2 left
💡 Hint

Check if the pod has more than one container and if the command specifies which container.

🔀 Workflow
expert
3:00remaining
Debugging a failing pod with multiple containers and restarts

You have a pod db-pod with two containers: db and sidecar. The pod keeps restarting. You want to check the logs of the previous instance of the db container only. What is the correct command sequence?

Akubectl logs db-pod -c db --previous
Bkubectl logs db-pod -c sidecar --previous
Ckubectl logs db-pod --previous
Dkubectl logs db-pod -c db
Attempts:
2 left
💡 Hint

Combine container selection and previous logs flags.