Skip to content

How to View and Tail Kubernetes Pod Logs with kubectl

DodaTech 2 min read

In this tutorial, you'll learn about How to View and Tail Kubernetes Pod Logs with kubectl. We cover key concepts, practical examples, and best practices.

The Problem

Your pod is running but behaving unexpectedly — returning errors, crashing intermittently, or producing wrong output. You need to see what the application inside the pod is logging. kubectl logs retrieves stdout and stderr output from containers running in Kubernetes pods. Unlike docker logs, kubectl logs works across your entire cluster and supports multi-pod selection, time-based filtering, and viewing logs from crashed container instances.

Quick Fix

1. View logs from a pod

kubectl logs my-pod

Expected output:

Starting server on port 8080
GET /api/users 200 12ms
POST /api/login 401 "Invalid credentials"

This shows the last few hundred lines from the pod's default container.

2. Tail logs in real time

kubectl logs -f my-pod

The -f flag follows the log output, similar to tail -f. Press Ctrl+C to stop.

3. Specify a container (for multi-container pods)

kubectl logs my-pod -c sidecar-proxy

The -c flag selects a specific container by name. Without it, kubectl returns an error for multi-container pods.

4. View logs from a previous crashed instance

kubectl logs my-pod --previous

When a pod restarts (CrashLoopBackOff), this shows the logs from the terminated container. Combine with -c for multi-container pods: kubectl logs my-pod -c app --previous.

5. Filter by time range

# Since a specific time
kubectl logs my-pod --since=2h

# Since a relative duration
kubectl logs my-pod --since-time="2025-06-23T10:00:00Z"

# Last N lines
kubectl logs my-pod --tail=50

6. Add labels to select pods

kubectl logs -l app=my-app,env=staging

The -l flag selects all pods matching the label selector. Outputs are interleaved with pod name prefixes.

7. Stream logs from multiple pods with stern (alternative)

# Install stern (recommended for multi-pod log viewing)
stern my-app --since=5m

Expected output:

my-app-6f8d4c7b-9k2lm app GET /api/health 200 2ms
my-app-6f8d4c7b-3h7jq app GET /api/users 200 15ms

Stern prefixes each line with the pod name and container name, making it easier to follow distributed logs.

8. Follow logs from all containers in a deployment

kubectl logs -f deployment/my-app --all-containers

The --all-containers flag streams logs from every container in the deployment's pods. Combine with --max-log-requests to control concurrency.

9. Limit log output to avoid overwhelming output

kubectl logs my-pod --tail=20
kubectl logs my-pod --max-log-requests=3

Use --tail to show only the last N lines. Use --max-log-requests to limit concurrent requests when following logs from multiple pods.

Prevention

  • Use kubectl logs --previous for debugging crash loops
  • Set structured logging (JSON) in your application for easier filtering with tools like jq
  • Use --since=1h instead of --tail=100 for time-bounded debugging
  • Use stern or kubetail for multi-pod log aggregation
  • Avoid tailing logs in production for extended periods — use a log aggregator like Loki or ELK instead
  • Label pods consistently to enable -l app=my-app log selection

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro