📖 Scenario: You are managing a Kubernetes cluster where multiple pods run your applications. To troubleshoot and monitor your applications, you need to view pod details and check their logs.
🎯 Goal: Learn how to use Kubernetes commands to view pod details and fetch logs from pods.
📋 What You'll Learn
Use kubectl commands to interact with pods
Identify pods by their exact names
Fetch detailed information about a pod
Retrieve logs from a specific pod
💡 Why This Matters
🌍 Real World
In real Kubernetes environments, viewing pod details and logs is essential for troubleshooting application issues and monitoring system health.
💼 Career
DevOps engineers and system administrators frequently use these commands to maintain and debug applications running in Kubernetes clusters.
Progress0 / 4 steps
1
List all pods in the default namespace
Run the command kubectl get pods to list all pods in the default namespace.
Kubernetes
Hint
Use kubectl get pods to see all pods in the current namespace.
2
View detailed information about the pod named nginx-pod
Run the command kubectl describe pod nginx-pod to see detailed information about the pod named nginx-pod.
Kubernetes
Hint
Use kubectl describe pod nginx-pod to get detailed info about that pod.
3
Fetch logs from the pod named nginx-pod
Run the command kubectl logs nginx-pod to fetch the logs from the pod named nginx-pod.
Kubernetes
Hint
Use kubectl logs nginx-pod to see the pod's logs.
4
Display the logs of the pod named nginx-pod
Run the command kubectl logs nginx-pod and ensure the logs are displayed in the terminal.
Kubernetes
Hint
The command kubectl logs nginx-pod will show the logs in your terminal.
Practice
(1/5)
1. What command shows detailed information and events about a specific Kubernetes pod?
easy
A. kubectl exec <pod-name> -- ls
B. kubectl get pods
C. kubectl logs <pod-name>
D. kubectl describe pod <pod-name>
Solution
Step 1: Understand the purpose of each command
kubectl describe pod <pod-name> shows detailed info and events; kubectl get pods lists pods; kubectl logs shows logs; kubectl exec runs commands inside pod.
Step 2: Identify the command for detailed pod info
The command that shows detailed pod information and events is kubectl describe pod <pod-name>.
Final Answer:
kubectl describe pod <pod-name> -> Option D
Quick Check:
Detailed pod info = kubectl describe pod [OK]
Hint: Use 'kubectl describe pod' for detailed pod info and events [OK]
Common Mistakes:
Using 'kubectl get pods' to see detailed info
Confusing 'kubectl logs' with pod description
Trying 'kubectl exec' to get pod events
2. Which of the following is the correct syntax to view logs of a pod named myapp-pod?
easy
A. kubectl get logs myapp-pod
B. kubectl logs myapp-pod
C. kubectl describe logs myapp-pod
D. kubectl show logs myapp-pod
Solution
Step 1: Recall the correct command for viewing pod logs
The command to view logs is kubectl logs <pod-name>.
Step 2: Match the correct syntax
Only kubectl logs myapp-pod matches the correct syntax; others are invalid commands.
Final Answer:
kubectl logs myapp-pod -> Option B
Quick Check:
View pod logs = kubectl logs [OK]
Hint: Use 'kubectl logs pod-name' to see pod logs quickly [OK]
Common Mistakes:
Adding extra words like 'get' or 'describe' with logs
Using 'kubectl show' which is not a valid command
Confusing logs command with describe
3. What will be the output of the command kubectl logs mypod --follow?
medium
A. It shows the logs of 'mypod' and continues to stream new log entries live.
B. It shows only the last 10 lines of logs from 'mypod'.
C. It describes the pod 'mypod' with detailed info and events.
D. It executes a shell inside the 'mypod' container.
Solution
Step 1: Understand the '--follow' flag with 'kubectl logs'
The '--follow' option streams logs live, showing new entries as they appear.
Step 2: Match the output description
The command outputs current logs and keeps streaming new logs live for 'mypod'.
Final Answer:
It shows the logs of 'mypod' and continues to stream new log entries live. -> Option A
Quick Check:
kubectl logs --follow = live log streaming [OK]
Hint: Add '--follow' to stream pod logs live as they appear [OK]
Common Mistakes:
Thinking '--follow' limits logs to last 10 lines
Confusing logs with pod description
Assuming it opens a shell inside the pod
4. You run kubectl logs mypod but get an error saying "a container name must be specified for pod mypod". What is the likely cause?
medium
A. The pod 'mypod' has multiple containers and you did not specify which container's logs to show.
B. The pod 'mypod' does not exist in the cluster.
C. You need to use 'kubectl describe pod mypod' instead of logs.
D. The pod 'mypod' is not running yet.
Solution
Step 1: Understand the error message
The error says a container name must be specified, which happens if the pod has multiple containers.
Step 2: Identify the cause
When a pod has more than one container, you must specify which container's logs you want using -c <container-name>.
Final Answer:
The pod 'mypod' has multiple containers and you did not specify which container's logs to show. -> Option A
Quick Check:
Multiple containers need container name for logs [OK]
Hint: Add '-c container-name' if pod has multiple containers [OK]
Common Mistakes:
Assuming pod doesn't exist without checking
Using describe instead of logs for viewing logs
Ignoring container name requirement for multi-container pods
5. You want to monitor logs of a pod named webserver continuously but only for the container named nginx. Which command should you use?
hard
A. kubectl logs webserver --container nginx
B. kubectl describe pod webserver -c nginx --follow
C. kubectl logs webserver -c nginx --follow
D. kubectl logs webserver --tail=0
Solution
Step 1: Identify the need to specify container and follow logs
To monitor logs continuously, use --follow. For multi-container pods, specify container with -c <container-name>.