Jake Trent

Read Logs for a Kubernetes Pod

Here’s how to read the logs for a kubernetes (k8s) pod.

Orient your read

First, we want to orient to where we’re reading from. We’ll need to know context and application name. A k8s “context” is a set of access parameters, including k8s cluster, user and namespace. The “application name” is a common label that we’d put on a pod as an identifier.

To get available contexts, run:

kubectl config get-contexts

To know the current context, run:

kubectl config current-context

If you’re running k8s locally, you likely running minikube and are in that context.

To see the application names of your pods, list the pods:

kubectl get pods

Reading kubernetes Logs

Once you know what to ask for, read the logs with:

kubectl --context <contextname> logs -lapp.kubernetes.io/name=<appname> --all-containers=true --prefix --tail=1 --follow

-l is to pass that label parameter – in this case, the application name. This is not the instance name, which has the unique hash suffix, thankfully. That makes this much easier to script.

--all-containers - will match on any pod with the given name.

--prefix shows the instance identifier prepended to each log line.

--tail=-1 will show the full log, from beginning to end.

--follow - will give you a live process that will update as the log is appended.

For example:

kubectl --context minikube logs -lapp.kubernetes.io/name=web --all-containers=true --prefix --tail=1 --follow