0
0
Kubernetesdevops~5 mins

Viewing Pod details and logs in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your app inside Kubernetes pods may not work as expected. You need to look inside the pod to see what is happening and check its logs to find errors or messages.
When you want to check if a pod is running correctly after deployment
When your application inside the pod crashes or behaves unexpectedly
When you want to see real-time output or error messages from your app
When you need to understand why a pod is restarting frequently
When debugging network or configuration issues inside a pod
Commands
This command lists all pods in the current namespace so you can find the pod name you want to inspect.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE my-app-pod 1/1 Running 0 3m
This command shows detailed information about the pod, including its status, events, and resource usage. It helps you understand the pod's current state and any recent issues.
Terminal
kubectl describe pod my-app-pod
Expected OutputExpected
Name: my-app-pod Namespace: default Node: node-1 Start Time: Thu, 01 Jun 2024 10:00:00 +0000 Labels: app=my-app Status: Running IP: 10.244.1.5 Containers: my-app-container: Container ID: docker://abcdef123456 Image: my-app-image:1.0 State: Running Ready: True Restart Count: 0 Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 5m default-scheduler Successfully assigned default/my-app-pod to node-1
This command fetches the logs from the main container inside the pod. Logs show what the app is printing, including errors and info messages.
Terminal
kubectl logs my-app-pod
Expected OutputExpected
2024-06-01T10:01:00Z Starting application 2024-06-01T10:01:05Z Application ready to accept requests
This command streams the logs live, so you can watch new log messages as they appear. Useful for real-time debugging.
Terminal
kubectl logs -f my-app-pod
Expected OutputExpected
2024-06-01T10:01:00Z Starting application 2024-06-01T10:01:05Z Application ready to accept requests 2024-06-01T10:02:00Z Received request from user
-f - Follow logs live as new lines appear
Key Concept

If you remember nothing else from this pattern, remember: use 'kubectl describe pod' for detailed pod info and 'kubectl logs' to see what your app is saying.

Common Mistakes
Trying to get logs from a pod name that does not exist
kubectl will return an error because it cannot find the pod to get logs from
Run 'kubectl get pods' first to find the correct pod name before fetching logs
Not specifying the container name when a pod has multiple containers
kubectl logs will fail or show logs from the wrong container
Use 'kubectl logs my-app-pod -c container-name' to specify which container's logs to view
Not using the '-f' flag when wanting to see live logs
You only get static logs up to the command run time, missing new log entries
Add '-f' to stream logs live for real-time monitoring
Summary
Use 'kubectl get pods' to list pods and find the pod name.
Use 'kubectl describe pod <pod-name>' to see detailed pod information and events.
Use 'kubectl logs <pod-name>' to view the application logs inside the pod.
Use 'kubectl logs -f <pod-name>' to stream logs live for real-time debugging.