Your ability to debug quickly is of great value to your team. If you hone this skill, team members and managers start to rely on you. You become an anchor for your team.
I'm going to teach you how to view pod logs quickly without friction or fuss.
Often when trying to view logs in production, it's urgent. Something is wrong and you need to find out why.
Viewing logs with kubectl is clunky and unintuitive.
You need the exact pod name, which often have random suffixes from Deployments.
kubectl logs my-pod-6dc5887b4f-vzbrz
Oh, and don't forget `-f` to tail the logs, which is what you want most of the time. 🤦♂️
kubectl logs my-pod-6dc5887b4f-vzbrz -f
Want to tail multiple logs? Gotta figure out the labeling scheme. 😳
kubectl get pod --show-labels # Find labels you care about, then:
kubectl logs --selector app.kubernetes.io/name=prometheus -f # <- Better not have a typo here
But it doesn't have to be this way. You don't need to be a perfect typist!
Here's how, step by step:
Step 1: Install stern
Stern is an open source logging tool built for Kuberentes.
I like installing it via `krew`: the kubectl plugin manager.
Install krew. You only need to do this once!
Then:
kubectl krew install stern
View Stern's README for other ways to install. You can run it as a standalone binary.
Don't be scared when you see this warning. It means this is a 3rd party plugin and the Krew team can't guarantee it.
WARNING: You installed plugin "stern" from the krew-index plugin repository.
These plugins are not audited for security by the Krew maintainers.
Run them at your own risk.
Upgrading is as simple as:
kubectl krew upgrade stern
Step 2: Tail logs by pod query
You no longer have to worry about typing the pod's exact name or figuring out label schemes.
Stern matches on the pod name.
kubectl stern pod-query [flags]
And stern follows the logs by default which is what you want 99% of the time.
The pod-query takes 2 forms:
1. Regex match
The pod-query is a regular expression that matches over pod names.
Examples:
# Tail any pod with "grafana" in its name in your active namespace:
kubectl stern grafana
# Tail all pods in your active namespace:
kubectl stern '.*'
2. Exact resource match
The pod-query is in the form `resource/name`. These are for pods manage by a controller such as a Deployment or StatefulSet. The `name` is the name of the controller resource.
Examples:
# Tail all pods belonging to a StatefulSet:
kubectl stern statefulset/kube-prometheus
# Or a deployment
kubectl stern deployment/nginx
Without any flags, the output looks like:
Let's break this down:
Step 3: Use flags for better filtering
Stern only searches in your active namespace. If you think your pod-query is right, but you're not seeing logs, try searching all namespaces. Or switch your active namespace.
# Search all namespaces with -A
kubectl stern grafana -A
Stern now adds the namespace as part of its output.
You may not want stern to prefix extra the namespace, pod, and container. You may want to pipe the logs to a file for analysis later. No problem:
kubectl stern grafana -oraw > path/to/file.log
Stern also provides similar flags to `kubectl logs`.
# Find pods matching "prometheus". And, tail logs only for container "grafana" within the last 5 minutes.
kubectl stern prometheus --since 5m -c grafana
See the official documentation for more or run `kubectl stern -h`.
Conclusion
If you're like me and think `kubectl logs` is clunky, give stern a try. You won't regret it. I never reach for `kubectl logs` anymore. In fact, writing this post, I had to re-teach myself that part of kubectl. Stern will save you time and frustration!
P.S.
If you enjoyed this post, follow me on Twitter/X or Linkedin for more.