liveBook · Manning

Kubectl logs

As explained in Kubernetes in Action, Second Edition

kubectl logs is a Kubernetes command-line operation that retrieves output streams captured from a container.

kubectl logs retrieves the output captured from a Kubernetes container. Containerized applications commonly write messages to stdout and stderr rather than application-specific files; the container runtime captures these streams and stores them consistently, usually under /var/log/containers. Kubernetes therefore lets you inspect logs without accessing the hosting node or knowing the application’s file layout. section 5.3.2

View and stream a pod’s logs

Run `kubectl logs kiada` to print the container log. Add `-f` or `--follow` to stream new lines as requests arrive, and press Ctrl-C to stop. Add `--timestamps` to show the timestamp attached to each line. Limit output with `--since=2m`, `--since-time=2020-02-01T09:50:00Z`, or `--tail=10`. section 5.3.2

$ kubectl logs kiada
Kiada - Kubernetes in Action Demo Application
---------------------------------------------
Kiada 0.1 starting...
Local hostname is kiada
Listening on port 8080
Received request for / from ::ffff:10.244.2.1     #1
Received request for / from ::ffff:10.244.2.5     #2
Received request for / from ::ffff:127.0.0.1      #3

Use the sample output to recognize startup messages, listening status, and request lines returned by `kubectl logs`. section 5.3.2

The command can target a ReplicaSet instead of a specific pod. Use `kubectl logs rs/kiada -c kiada` when a pod has multiple containers. Add `--all-pods` to retrieve logs from every pod in the ReplicaSet, or `--all-containers` to include every container. Combining `--all-pods`, a container selection, and `-f` helps follow traffic distributed across replicas. section 14.1.2

Inspect logs across replicas

`kubectl logs rs/kiada --all-pods -c envoy` streams the Envoy container logs from all ReplicaSet pods. Use `kubectl logs rs/kiada --all-pods --all-containers` when every container’s output is needed. section 14.1.2

How Kubernetes in Action, Second Edition covers it

How deep it goes
Explains captured output, streaming, timestamps, time filters, and multi-pod or multi-container selection, but not log retention.
Examples
Demonstrates Kiada startup and request logs, real-time following, timestamps, time windows, tails, and ReplicaSet-wide retrieval.
What you can do after
Operate kubectl logs to inspect, follow, filter, and aggregate application output from Kubernetes pods and containers.
How it connects
Connects container stdout and stderr with pods, containers, ReplicaSets, container runtimes, and distributed application traffic.

Also explained in

Compare how 3 books explain kubectl logs →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.