0Pricing
DevOps Bootcamp · Lesson

Logging with kubectl logs

Access and analyze container logs directly from your Kubernetes cluster using kubectl.

Logging with kubectl logs is a free DevOps Bootcamp lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Logs Matter in Kubernetes

In Kubernetes, understanding what your applications are doing is key. Logs are like your application's diary, recording events, errors, and status updates.

They are essential for:

  • Debugging: Pinpointing issues when something goes wrong.
  • Monitoring: Keeping an eye on application health and performance.
  • Auditing: Tracking actions and changes over time.

Container Logs Explained

When a containerized application runs, it typically writes output to its standard output (stdout) and standard error (stderr) streams. Kubernetes collects these streams for each container.

These streams are then made available through the Kubernetes API, allowing you to easily access them without having to SSH into nodes or individual containers.

Introducing kubectl logs

The kubectl logs command is your primary tool for accessing logs from containers running in your Kubernetes Pods. It's straightforward and offers several powerful options.

The most basic usage involves simply specifying the name of the Pod you want to inspect.

Creating a Log-Generating Pod

To see kubectl logs in action, let's imagine we've deployed a simple Pod that continuously outputs messages to its standard output. This will simulate a real application generating logs.

Here's the YAML for such a Pod (you would apply this using kubectl apply -f):

apiVersion: v1
kind: Pod
metadata:
  name: busybox-logger
spec:
  containers:
  - name: busybox-container
    image: busybox
    command: ['sh', '-c', 'while true; do echo "$(date) - Hello from BusyBox"; sleep 5; done']
  restartPolicy: Always

Viewing Basic Pod Logs

Once your busybox-logger Pod is running (check with kubectl get pods), you can fetch its logs using the basic kubectl logs command.

This will show you all the log entries recorded up to that point.

kubectl logs busybox-logger

Streaming Real-time Logs

For live debugging, you often need to see logs as they happen, similar to tail -f on Linux. The -f or --follow flag does exactly this.

It continuously streams new log entries from the container.

kubectl logs -f busybox-logger

Logs from Previous Containers

If a container within your Pod crashes and restarts, kubectl logs by default shows logs from the *current* running instance.

To view logs from a *previous*, terminated instance of a container, use the --previous or -p flag. This is invaluable for debugging why a container might have failed.

kubectl logs -p busybox-logger

Logs from Specific Containers

Some Pods can run multiple containers (e.g., a main application and a sidecar for logging or monitoring). If your Pod has more than one container, you must specify which container's logs you want to see using the -c or --container flag.

kubectl logs my-multi-container-pod -c my-sidecar-container

Filtering & Limiting Log Output

Sometimes you only need a specific part of the logs or just the latest entries. You can combine kubectl logs with standard Linux commands or use built-in flags:

  • Filter with grep: Pipe the output to grep to find specific keywords.
  • Limit lines with --tail: Display only the last N lines of the logs.
kubectl logs busybox-logger | grep "Hello"
kubectl logs busybox-logger --tail=5

Log Command Challenge

You have a Pod named my-app-pod with two containers: frontend and backend. The backend container recently crashed and restarted. You want to view the logs from the previous instance of the backend container.

Logs & Debugging Summary

Congratulations! You've learned the essentials of accessing and analyzing container logs using kubectl logs. This powerful command is indispensable for understanding and troubleshooting your applications running in Kubernetes.

Key commands to remember:

  • kubectl logs <pod-name>: Basic log retrieval.
  • kubectl logs -f <pod-name>: Stream logs in real-time.
  • kubectl logs -p <pod-name>: View logs from a previous container instance.
  • kubectl logs -c <container> <pod-name>: Target logs from a specific container.

Frequently asked questions

Is the “Logging with kubectl logs” lesson free?

Yes — the full text of “Logging with kubectl logs” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Logging with kubectl logs”?

Access and analyze container logs directly from your Kubernetes cluster using kubectl. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Logging with kubectl logs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Logging with kubectl logs
  2. Metrics with Prometheus & Grafana
  3. Health Checks: Liveness & Readiness Probes
  4. Distributed Tracing and Events
← Back to DevOps Bootcamp