0
0
KubernetesHow-ToBeginner · 3 min read

How to Get Pod Logs in Kubernetes Quickly and Easily

Use the kubectl logs [pod-name] command to get logs from a Kubernetes pod. Add -c [container-name] if the pod has multiple containers. Use --follow to stream logs in real-time.
📐

Syntax

The basic command to get logs from a pod is kubectl logs [pod-name]. If the pod has multiple containers, specify the container with -c [container-name]. To watch logs live, add --follow.

  • kubectl logs: Command to fetch logs.
  • [pod-name]: Name of the pod you want logs from.
  • -c [container-name]: Optional, specify container if multiple exist.
  • --follow: Optional, streams logs live like tail -f.
bash
kubectl logs [pod-name] [-c container-name] [--follow]
💻

Example

This example shows how to get logs from a pod named myapp-pod and stream logs from a container named myapp-container.

bash
kubectl logs myapp-pod -c myapp-container --follow
Output
2024-06-01T12:00:01.123Z Starting application 2024-06-01T12:00:02.456Z Application running 2024-06-01T12:00:03.789Z Received request from client ...
⚠️

Common Pitfalls

Common mistakes include:

  • Not specifying the container name when the pod has multiple containers, causing an error.
  • Trying to get logs from a pod that has already terminated without using --previous.
  • Using the wrong pod name or namespace.

Always check the pod status with kubectl get pods before fetching logs.

bash
kubectl logs myapp-pod
# Error if multiple containers exist

kubectl logs myapp-pod -c myapp-container
# Correct usage specifying container

kubectl logs myapp-pod --previous
# Get logs from previous pod instance if it crashed
📊

Quick Reference

CommandDescription
kubectl logs [pod-name]Get logs from a single-container pod
kubectl logs [pod-name] -c [container-name]Get logs from a specific container in a multi-container pod
kubectl logs [pod-name] --followStream logs live
kubectl logs [pod-name] --previousGet logs from a crashed or previous pod instance
kubectl logs -n [namespace] [pod-name]Get logs from a pod in a specific namespace

Key Takeaways

Use kubectl logs [pod-name] to fetch pod logs quickly.
Specify container with -c if the pod has multiple containers.
Use --follow to stream logs live like tailing a file.
Check pod status before fetching logs to avoid errors.
Use --previous to see logs from crashed pods.