๐Ÿ“„ kubectl logs Real-World Use Cases Explained

kubectl logs is used to view logs generated by containers in your Kubernetes pods. Logs help you troubleshoot issues, understand behavior, and monitor applications.


๐Ÿš€ Basic Use Cases

1. View logs from a pod with a single container

kubectl logs nginx
Case Study: You're running an NGINX web server in a pod. You want to check what requests it has handled. This command shows you its recent output.


2. Add pod/container name as prefix

kubectl logs nginx --prefix
Use: Useful in environments where you want to see the source of each log line.

Explanation: Adds a prefix to each line in logs showing [pod-name/container-name], helpful when aggregating logs from multiple containers.

Real-World Example: In CI/CD pipelines or ELK (Elasticsearch, Logstash, Kibana) stacks, prefixes make it easier to trace logs.


3. Limit log size (in bytes)

kubectl logs nginx --limit-bytes=500
Use Case: You're on a slow connection or just want a quick preview of logsโ€”get only 500 bytes of logs.

Explanation: Limits the amount of logs fetched from the pod. Useful for debugging without overloading the terminal.


4. Wait for pod to be ready

kubectl logs nginx --pod-running-timeout=20s
Example: Your app takes time to initialize. This command waits up to 20s for the pod to be ready before fetching logs.

Explanation: Useful in CI/CD pipelines or scripting scenarios where pods may still be starting.


๐ŸงŠ Multi-Container Use Cases

5. View logs from all containers in a pod

kubectl logs nginx --all-containers=true
Use Case: Your pod runs sidecar containers (e.g., logging, monitoring). This shows logs from all of them.


6. View logs from all pods in a deployment

kubectl logs deployment/nginx --all-pods=true
Case Study: You're debugging an NGINX deployment. This shows logs from all pods under that deployment.


7. Logs from labelled pods

kubectl logs -l app=nginx --all-containers=true
Use Case: You label all your app pods with app=nginx. This command gives you logs from all such pods, useful in large apps with multiple replicas.


8. Limit number of concurrent log requests

kubectl logs -l app=nginx --max-log-requests=10
Use: For performance control in high-scale environments, where pulling logs from 100+ pods may overload systems.

Explanation: Prevents overloading kubectl or Kubernetes API server.


๐Ÿ” Previous Logs

9. View logs from previously terminated container

kubectl logs -p -c ruby web-1
Case Study: Your Ruby app crashed. This command retrieves logs from the previous run, which helps debug the crash.


๐Ÿ“ก Streaming (Live Tail)

10. Stream logs and ignore errors

kubectl logs nginx -f --ignore-errors=true
Case: Youโ€™re watching logs live during a deployment, and want to ignore any transient errors during pod updates.


11. Stream logs from a specific container

kubectl logs -f -c ruby web-1
Use: Useful when debugging multi-container pods, where only one container (like ruby) is of interest.


12. Stream logs from all containers in all labelled pods

kubectl logs -f -l app=nginx --all-containers=true
Case Study: You're monitoring all logs live during a load test to see how your NGINX containers handle stress.


๐Ÿ• Time-Based Filtering

13. Show only the last 20 lines

kubectl logs --tail=20 nginx
Use Case: Quick look at recent activity.


14. Show logs written in last 1 hour

kubectl logs --since=1h nginx
Example: After a config change, you want to see logs only since the change.


15. Show logs from a specific timestamp

kubectl logs nginx --since-time=2024-08-30T06:00:00Z --timestamps=true
Use: Useful when correlating with an event from monitoring tools like Prometheus.


16. Skip TLS verification

kubectl logs --insecure-skip-tls-verify-backend nginx
Case: Youโ€™re working in a test cluster with expired/invalid kubelet certs. Use this only in non-production.


๐Ÿ’ผ Special Resource Types

17. Logs from a Job

kubectl logs job/hello
Use Case: One-time Job completed and you want to check the output.


18. Logs from a specific container of a deployment

kubectl logs deployment/nginx -c nginx-1
Case Study: Deployment has multiple containers (e.g., nginx-1, sidecar), and you want to isolate logs from nginx-1 only.


๐Ÿ“š Summary Table

Use Case Command
Single Container kubectl logs pod-name
Multi-Container --all-containers=true
Stream Logs -f
Previous Logs -p
Filter by Label -l app=name
Resource Type (Job/Deployment) job/name, deployment/name
Time-based --since=1h, --since-time=
TLS Skip --insecure-skip-tls-verify-backend
Limit Output --limit-bytes, --tail