DEV Community

Cover image for Supercharge your Kubernetes setup with OhMyZSH 🚀🚀🚀 + awesome command-line tools
Agrim Prasad
Agrim Prasad

Posted on • Originally published at agrimprasad.com on

Supercharge your Kubernetes setup with OhMyZSH 🚀🚀🚀 + awesome command-line tools

Follow me on Twitter. Feel free to suggest ideas, give feedback or discuss any Backend or DevOps topics with me.

Almost all of my colleagues who have worked with kubernetes have had similar complaints about the kubectl tool. It's so verbose! I need to remember such long commands! This lead me to search and discover a bunch of powerful cli tools, which combined with the OhMyZSH command-line framework can make your kubectl journey much smoother.

How you would usually use kubectl

kubectl by itself is a very powerful kubernetes client, and can be used for all kinds of purposes, but it really is too verbose, with a lot of gotchas in different commands.

  • Query the kubernetes pods running in specific namespaces of your kubernetes cluster.
kubectl get pod -n myZoo
  • Get the status of and details about different kubernetes entities, such as pods, configmaps, services etc.
kubectl describe deployment myCatDeploy -n myZoo
  • Tail the logs of a specific container. Firstly, find the list of all pods with
kubectl get pod -n myZoo

then find your specific pod and run kubectl logs (and hope you didn't make a typo anywhere)

kubectl logs -f myKittenPod -n myZoo

Tools to turbocharge kubectl

We can do better.

I give the installation instructions for these tools with macOS here. You can check the links to get installation instructions for other operating systems.

1. kubectx to switch cluster & namespaces quickly

kubectx can be installed on macOS with homebrew

brew install kubectx

This command actually installs two tools, kubectx and kubens.

You can use kubectx to switch between different clusters with kubectx my-cluster-name. Behind the scenes, this command will edit your ~/.kube/config file to mark a specific cluster as being the current-context. You would otherwise have to edit this file manually or use kubectl config use-context my-cluster-name.

kubectx demo to switch between clusters

kubens helps you switch between namespaces quickly. Whereas earlier you would have had to run kubectl config set-context --current --namespace=myZoo, now you can just run kubens myZoo.

kubens demo to switch between namespaces

As a bonus, I alias these tools to even shorter commands in my ZSH configuration file (~/.zshrc).

alias kctx="kubectx"
alias kns="kubens"

Then source the ZSH configuration file to load the changes.

source ~/.zshrc

Furthermore, if you use it with OhMyZsh you can get auto-completion of namespaces and cluster names with these completion scripts. No need to remember those long namespaces anymore!

mkdir -p ~/.oh-my-zsh/completions
chmod -R 755 ~/.oh-my-zsh/completions
ln -s /opt/kubectx/completion/kubectx.zsh ~/.oh-my-zsh/completions/_kubectx.zsh
ln -s /opt/kubectx/completion/kubens.zsh ~/.oh-my-zsh/completions/_kubens.zsh

2. kube-ps1 to display current cluster & namespace

kube-ps1 is a great companion tool to kubectx and kubens which displays the currently active cluster and namespace on your command line prompt. No more accidental deploys to the production cluster at 2AM.

Install with:

brew install kube-ps1

Here's how your kubernetes cluster-name (minikube) and namespace (default) appear in the command prompt now:

kube-ps1 screenshot

Here's how it looks when combined with the kubectx and kubens tools to switch clusters and namespaces.

kube-ps1 demo

3. OhMyZSH kubectl plugin for kubectl shortcuts

OhMyZSH actually has a plugin for kubectl which gives you nearly hundred shortcuts for commonly used commands. To use it, add kubectl to the plugins array in your ~/.zshrc file and then source the ~/.zshrc file like before:

plugins=(... kubectl)
source ~/.zshrc

Some shortcuts for commonly used commands provided by this plugin below.

Alias Command Description
k kubectl The kubectl command
kaf kubectl apply -f Apply a YML file
keti kubectl exec -ti Drop into an interactive terminal on a container
kdel kubectl delete Delete resources by filenames, stdin, resources and names, or by resources and label selector
kgp kubectl get pods List all pods in ps output format
kdp kubectl describe pods Describe all pods
kdelp kubectl delete pods Delete all pods matching passed arguments
kgd kubectl get deployment Get the deployment

… and more! Check out the full list on the github page.

4. stern for logs

stern has been designed to allow you to tail the logs of multiple pods and containers using regex. This means that you can search pods by partial name matches, rather than having to query the full names of pods and then running kubectl logs -f with the exact name of every single pod.

Install with:

brew install stern

To receive and then tail all the available logs of all pods whose names partially match myKitten (assuming default namespace of the minikube cluster used with kubectx, kubens and kube-ps1):

(⎈ |minikube:default)➜ ~ stern myKitten

To receive the last 10 lines per container and then tail:

(⎈ |minikube:default)➜ ~ stern myKittenPod --tail=10

To receive the last 2 minutes of logs per container and then tail:

(⎈ |minikube:default)➜ ~ stern myKittenPod --since=2m

You can also source stern autocompletion with zsh by adding this to your ~/.zshrc and then running source ~/.zshrc

source <(stern --completion=zsh)

5. kpoof to port-forward pods intuitively

kpoof is the easiest way to port-forward ports, which is often useful when you want to test an internal service which is not supposed to be publicly exposed outside the cluster.

Install with:

brew tap farmotive/k8s
brew install kpoof

With kpoof, you get an interactive way to firstly choose the namespace, and then the name of the pod which you would like to port-forward. The tool forwards the same local port number as the port exposed by the pod by default.

For example, in order to port-forward localhost port 3306 to port 3306 of the pod nyc below, you can run the following commands:

$ kpoof
Namespace? (default zoo):
1 zoo
2 town
3 city
3
Pod number? (default 1):
1 nyc
2 london
3 bangkok
1
Forwarding from 127.0.0.1:3306 -> 3306

See it in action below.

6. Popeye to scan your cluster for potential issues

Popeye is a tool which scans your cluster for misconfigured resources. Currently, nodes, namespaces, pods and services are supported for scanning.

When your cluster grows to 100s of different pods and to 10s of different namespaces, having such a tool is invaluable in detecting issues before they blow up, especially if you don't have monitoring on some of these resources.

Install with:

brew install derailed/popeye/popeye

Run with the command popeye to eye your entire cluster. Here's a screenshot of a well-maintained cluster which gets the cluster score 93 -- A from popeye

popeye screenshot

Know of any other useful CLI or GUI utilities for kubernetes? Let me know in the comments below!

Top comments (0)