Course contentsShow
← Cloud and Infrastructure
Lesson 1432 of 2,98929. Kubernetes ObservabilityFree lesson

kubectl logs Command

Using kubectl logs to view container logs, with options for following, timestamps, and previous containers.

kubectl logs Command

What you'll learn: How to view and follow container logs in real-time using the kubectl logs command with various filtering and formatting options.

The Core Idea

The kubectl logs command is your window into what's happening inside your containers. It retrieves the stdout and stderr streams from running (or previously running) containers, allowing you to troubleshoot issues, monitor application behavior, and verify that your applications are functioning correctly.

Think of it like reading a diary your container keeps—every important event, error, or message your application writes gets recorded, and kubectl logs lets you read it.

Basic Usage

To view logs from a pod with a single container:

kubectl logs my-pod

For pods with multiple containers, specify which one:

kubectl logs my-pod -c my-container

Following Logs in Real-Time

The -f (follow) flag streams logs as they're written, similar to tail -f:

kubectl logs -f my-pod

This is invaluable when debugging live issues or watching deployments.

Adding Timestamps

Include timestamps to understand when events occurred:

kubectl logs my-pod --timestamps

Viewing Previous Container Logs

If a container crashed and restarted, view the previous instance's logs:

kubectl logs my-pod --previous

This is critical for debugging crash loops—the current container might have just started, but the previous one holds the crash evidence.

Combining Options

You can combine flags for powerful diagnostics:

kubectl logs my-pod -c app --previous --timestamps

Key Takeaway: The kubectl logs command is your primary tool for accessing container output, with options to follow live logs (-f), view timestamps (--timestamps), and inspect crashed containers (--previous)—essential capabilities for effective Kubernetes observability.