0
0
Kubernetesdevops~5 mins

kubectl logs for debugging in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your app inside Kubernetes does not work as expected. You need to see what it is doing inside. The kubectl logs command shows the messages your app writes, helping you find problems.
When your app crashes or restarts and you want to see error messages.
When your app is running but not behaving correctly and you want to check its output.
When you want to verify that your app started successfully inside the pod.
When you want to see recent activity or debug information from your app.
When you want to check logs from a specific container in a pod with multiple containers.
Commands
This command lists all pods in the current namespace so you can find the exact pod name to check logs from.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE my-app-pod 1/1 Running 0 10m
This command shows the logs from the main container in the pod named 'my-app-pod'. It helps you see what your app printed to its output.
Terminal
kubectl logs my-app-pod
Expected OutputExpected
Starting application... Listening on port 8080 Connection established Error: failed to connect to database Retrying connection...
This command shows logs from the previous instance of the container if it crashed and restarted. Useful to see what caused the crash.
Terminal
kubectl logs my-app-pod --previous
Expected OutputExpected
Starting application... Fatal error: out of memory Container terminated unexpectedly
--previous - Show logs from the last terminated container instance
If your pod has multiple containers, this command shows logs from the container named 'sidecar-container'.
Terminal
kubectl logs my-app-pod -c sidecar-container
Expected OutputExpected
Sidecar started Syncing data with main app Sync complete
-c - Specify which container's logs to show
Key Concept

If you remember nothing else from this pattern, remember: kubectl logs lets you see what your app inside a pod is saying, which is key to finding and fixing problems.

Common Mistakes
Trying to get logs from a pod name that does not exist or is misspelled.
kubectl will return an error because it cannot find the pod to show logs from.
Run 'kubectl get pods' first to find the exact pod name and use that name in the logs command.
Not specifying the container name in a pod with multiple containers.
kubectl logs will fail or show logs from the wrong container, missing the information you need.
Use the '-c container-name' flag to specify which container's logs you want.
Not using the '--previous' flag when checking logs after a crash.
You will see only the current container logs, which may be empty if the container restarted, missing the crash details.
Use 'kubectl logs pod-name --previous' to see logs from the crashed container instance.
Summary
Use 'kubectl get pods' to find the pod name before checking logs.
Use 'kubectl logs pod-name' to see the current logs from the main container.
Use '--previous' to see logs from a crashed container before it restarted.
Use '-c container-name' to get logs from a specific container in a multi-container pod.