DEV Community

Cover image for Quick Guide: Kubens, Kubectx, & Stern
🐱
🐱

Posted on

Quick Guide: Kubens, Kubectx, & Stern

These easily accessible Command Line Tools are fantastic for debugging and utilizing Kubernetes (k8s) cloud resources from the command line.

Who is this for?

This guide is coming from the perspective of someone trying to operate, develop and debug applications that are hosted via k8s. As someone who does this on a daily basis, I have come to appreciate a few wonderful tools that make the process much easier.

The usefulness of each tool scales nicely with the level of infrastructure present within your organization. Those that are without multiple namespaces and/or contexts might not be served as well, but might still benefit from the knowledge of their existence.

We would like to reduce

$ kubectl config set-context <insert-context-here> --namespace=<insert-namespace-name-here>
$ kubectl get pods | <tool-of-choice-to-extract-pod-list>
$ kubectl log -f <pod-1> & kubectl log -f <pod-2> & ...kubectl log -f <pod-N>
Enter fullscreen mode Exit fullscreen mode

down to something much more manageable, with low cognitive performance.

Aside: Context vs namespaces.

Getting Started

Prerequisites:

Installation

  1. Install Kubectx + Kubens.
  2. Install Stern.

Usage

Switch k8s contexts.

$ kubectx <context_name>
Enter fullscreen mode Exit fullscreen mode

Switch k8s namespace.

$ kubens <namespace_name>
Enter fullscreen mode Exit fullscreen mode

Tail pods.

stern <pod_name>
Enter fullscreen mode Exit fullscreen mode

Often we'll find ourselves trying to debug a specific container within our pods. This might be due to a split in services across the pod. We can do

$ stern --container <container-name> <pod-name>
Enter fullscreen mode Exit fullscreen mode

Voilà, we have significantly reduced our cognitive load and made switching contexts in our shell of choice much easier. We could take this a step further and alias out these commands, but I'll leave that to the reader. As they stand these are suitable for much quicker debugging sessions.

Aside: Making life even easier with Kubectl-aliases

Thank you for reading, and have a wonderful day 😊 💕

Top comments (0)