Kubernetes Command Line Tooling | Shorts

Vishnu Deva
4 min readAug 31, 2022

The Kubernetes API has a wide range of options, and it gets tiresome to work with through kubectl on the command line for extended periods of time. And regardless of how much effort you put into aliases and making your custom tooling look good, it’ll never be as good as tools that were expressly developed for this specific purpose.

So this article goes over some necessary tools for your Terminal to make life easier when working with Kubernetes.

Kube PS1

kube-ps1

Displays your current Kubernetes cluster and namespace information in the terminal prompt. It’s a script that can be installed with the Brew package manager if you already have it:

$ brew update
$ brew install kube-ps1

If you don’t have brew, don’t bother with it, just install kube-ps1 from source, it’s nothing but a script that needs to be sourced.

When you use managed clusters, your cluster names tend to be unwieldy, something like arn:aws:eks:us-east-1:<aws-account-id:cluster/<cluster-name>. So you can shorten it with a function before sourcing the kube-ps1 script. For example, here’s what my .bashrc contains:

# kube-ps1
function get_cluster_short() {
echo "$1" | cut -d : -f6 | cut -d / -f2
}
KUBE_PS1_CLUSTER_FUNCTION=get_cluster_short
. $HOME/.kube-ps1/kube-ps1.sh
PS1='$(kube_ps1) ${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[\033[00m\]\[\033[01;34m\]\W\[\033[00m\]\$ '

This gives me a concise cluster name, namespace, and local folder name. For brevity, my PS1 displays only the current directory’s name.

(⎈|<cluster-name>:kube-system) directory$
From https://github.com/jonmosco/kube-ps1

Note

PS1 is the primary prompt variable on Linux. It’s what defines the contents of the prompt you see whenever you open the terminal.

Aliases for kubectl

kubectl-aliases

This project contains aliases for more than 1200 useful kubectl commands in a consistent manner. The consistency guarantee is key because it makes it easy to guess commands without referring to the aliases or memorizing them. For example, kubectl get pods is kgpo

and kubectl get ingress -o yaml is kgingoyaml

Thus, kubectl get pods -o yaml is kgpooyaml

I was expecting that it’d take me a month to get used to it, but I became quite comfortable with it in just a day.

Installation

A straightforward process since it just needs the ~/.kubectl_aliases file to be sourced during shell start.

Kubectl Tree

kubectl-tree

There are a lot of interrelated resources in Kubernetes and kubectl doesn’t do the relationships justice when you look at your resources category by category. Using ownerReferences, kubectl-tree shows you the relevant resources in one view. Here’s an example, when we take a look at the ‘karpenter’ deployment with: kubectl tree deploy karpenter -n karpenter

A minor convenience, but a welcome one.

Installation

Uses the krew kubectl plugin manager. Once you have that, installation is just: kubectl krew install tree

Kubectx and Kubens

Link

Context management is a pain. Namespace management is a pain. These tools take care of that in a visually appealing and efficient manner.

Instead of going through kubectl config get-contexts and then use kubectl config use-context <context>. We just do kubectx <context>.

Similarly, just using kubens <namespace> will also work.

But the main value-add here is not the alias, but rather the autocompletion and fuzzy finder for both kubectx and kubens. This makes dealing with multiple clusters and endless namespaces so much easier.

GIF from https://github.com/ahmetb/kubectx#interactive-mode

Installation

Both can be installed with the ‘krew’ kubectl plugin manager.

kubectl krew install ctx
kubectl krew install ns

If you want the interactive mode shown in the gif above, you only need to install fzf in addition to the two plugins above.

K9s Terminal UI

K9s

I was previously a heavy user of the desktop UI Lens, but more recently, I’ve found that I much prefer K9s for its snappiness and keyboard shortcuts. You do miss the IDE feel that Lens gives you, but overall, I think K9s is the best Kubernetes graphical interface out there. And the minimal resource usage is the cherry on top.

It excels at giving you a bird’s eye view of your cluster from the command line. It also lets you edit resources, view logs, attach to a pod, etc. I’m surprised by how intuitive it was; where I expected a learning curve, I found none.

From https://k9scli.io/
If nothing else, you need to use K9s just to show appreciation for this logo

Managing multiple Kubectl Versions

I’ve covered this in this article. I highly recommend this if you routinely deal with multiple Kubernetes clusters running different K8s versions.

--

--