featured.png

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

Table of Contents

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.

AliasCommandDescription
kkubectlThe kubectl command
kafkubectl apply -fApply a YML file
ketikubectl exec -tiDrop into an interactive terminal on a container
kdelkubectl deleteDelete resources by filenames, stdin, resources and names, or by resources and label selector
kgpkubectl get podsList all pods in ps output format
kdpkubectl describe podsDescribe all pods
kdelpkubectl delete podsDelete all pods matching passed arguments
kgdkubectl get deploymentGet 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

Bonus

@megabreit pointed out on Twitter that I missed the built-in auto-completion feature of kubectl, which allows you to run any kubectl command and then type resource names partially. For example, you could type in partial namespace names and wait for the full namespace name to be fetched from the kube-api-server.

For ZSH, install with:

source <(kubectl completion zsh)

If you've aliased your kubectl, you can extend autocompletion to work with it this way:

echo 'alias k=kubectl' >>~/.zshrc
echo 'complete -F __start_kubectl k' >>~/.zshrc

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

Did you find this article useful? If yes, follow me on Twitter. Feel free to suggest ideas, give feedback or discuss any Backend or DevOps topics with me.

Agrim Prasad avatar
Agrim Prasad
DevOps and Backend Development Enthusiast
comments powered by Disqus