Bird
Raised Fist0
Kubernetesdevops~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the kubectl logs command do in Kubernetes?
easy
A. Deletes a pod from the cluster
B. Lists all pods running in the cluster
C. Creates a new pod with specified configuration
D. Shows the output logs of a container inside a pod

Solution

  1. Step 1: Understand the purpose of kubectl logs

    This command is used to view the logs generated by containers running inside pods in Kubernetes.
  2. Step 2: Differentiate from other commands

    Deleting pods uses kubectl delete, creating pods uses kubectl create, and listing pods uses kubectl get pods. None of these show logs.
  3. Final Answer:

    Shows the output logs of a container inside a pod -> Option D
  4. Quick Check:

    Logs = Output from container [OK]
Hint: Logs show container output, not pod creation or deletion [OK]
Common Mistakes:
  • Confusing logs with pod creation or deletion
  • Thinking logs list pods instead of showing output
  • Assuming logs show cluster-wide events
2. Which of the following is the correct syntax to follow logs live from a pod named webapp-123?
easy
A. kubectl logs webapp-123 --follow
B. kubectl logs --tail webapp-123
C. kubectl logs webapp-123 -t
D. kubectl logs -t webapp-123

Solution

  1. Step 1: Recall correct flag placement

    The --follow flag comes after the pod name in the kubectl logs command syntax.
  2. Step 2: Check distractor flags

    Distractors use --tail (shows last lines, no streaming) or -t (timestamps, no follow). Only --follow streams live.
  3. Final Answer:

    kubectl logs webapp-123 --follow -> Option A
  4. Quick Check:

    Pod name first, then flags [OK]
Hint: Put pod name before flags like --follow [OK]
Common Mistakes:
  • Placing flags before pod name
  • Using --tail or -t instead of --follow
  • Mixing order of arguments
3. Given the command kubectl logs mypod --tail=5, what will be the output?
medium
A. Shows all logs except the last 5 lines from the pod named mypod
B. Shows the first 5 lines of logs from the pod named mypod
C. Shows the last 5 lines of logs from the pod named mypod
D. Shows logs only if the pod has more than 5 containers

Solution

  1. Step 1: Understand the --tail option

    The --tail=5 option limits the output to the last 5 lines of the logs.
  2. Step 2: Confirm it applies to the specified pod

    The command targets the pod named mypod, so logs shown are from that pod only.
  3. Final Answer:

    Shows the last 5 lines of logs from the pod named mypod -> Option C
  4. Quick Check:

    --tail = last lines [OK]
Hint: --tail shows last lines, not first [OK]
Common Mistakes:
  • Thinking --tail shows first lines
  • Confusing pod logs with container logs
  • Assuming it filters by container count
4. You run kubectl logs mypod -c mycontainer but get an error saying container not found. What is the likely cause?
medium
A. The pod mypod is not running
B. The container name mycontainer does not exist in pod mypod
C. You forgot to add the --follow flag
D. The pod mypod has multiple containers but you did not specify one

Solution

  1. Step 1: Analyze the error message

    The error says container not found, which means the container name given is incorrect or missing in the pod.
  2. Step 2: Check command correctness

    You specified -c mycontainer, so if the container name is wrong, the error appears. Pod running state or missing --follow flag does not cause this error.
  3. Final Answer:

    The container name mycontainer does not exist in pod mypod -> Option B
  4. Quick Check:

    Container name must match exactly [OK]
Hint: Check container name spelling matches pod's containers [OK]
Common Mistakes:
  • Assuming pod state causes container not found error
  • Forgetting to specify container when multiple exist
  • Using wrong flag for container name
5. You want to debug a pod with two containers named frontend and backend. Which command correctly streams live logs from the backend container only?
hard
A. kubectl logs podname -c backend --follow
B. kubectl logs podname --tail -c backend
C. kubectl logs -c backend podname --tail
D. kubectl logs --tail podname -c backend

Solution

  1. Step 1: Recall correct argument order

    The pod name must come immediately after kubectl logs, followed by flags like -c and --follow.
  2. Step 2: Confirm container and follow flag usage

    Using -c backend specifies the backend container, and --follow streams logs live.
  3. Final Answer:

    kubectl logs podname -c backend --follow -> Option A
  4. Quick Check:

    Pod name first, then container and flags [OK]
Hint: Pod name first, then container (-c) and --follow [OK]
Common Mistakes:
  • Placing flags before pod name
  • Mixing order of container and follow flags
  • Using unsupported shorthand flags
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard