If you are someone who frequently interacts with a Kubernetes cluster, you may find yourself frustrated by the time you spend typing out repetitive commands. One way to reduce this frustration is through the use of CLI tools for kubectl , the Kubernetes command line interface. This article will highlight several tools used to simplify usage of kubectl and save you time.

The following tools will be covered:

shell autocompletion: Autocompletion for kubectl

kubectx & kubens: Switch back and forth between Kubernetes contexts and namespaces

kube-ps1: Kubernetes prompt for bash and zsh: context/namespace info to your shell prompt

kubectl aliases: Create your own aliases for interacting with kubectl

Pre-requisites: make sure you have the kubectl command line utility installed along with access to a Kubernetes cluster. Check out this article for instructions on installing kubectl . Follow this friendly guide to get access to a Kubernetes cluster using Oracle Container Engine for Kubernetes (OKE).

Shell Autocompletion: Autocompletion for kubectl

If you find yourself spending too much time writing out long kubectl commands it would be worth your time to configure shell autocompletion. This is similar to shell completion in bash, zsh, etc. This tool is useful because it can be used to autocomplete all of the basic kubectl commands along with deployments, services, etc. from your cluster. Autocomplete pulls information from your current context, saving you from having to type out potentially lengthy deployment names. Check out this article for instructions on how to configure shell autocompletion for kubectl .

To use shell autocompletion with kubectl simply press tab while writing out a command. For example we can type g and then press tab to autocomplete to get .

$ kubectl g [TAB]

$ kubectl get

When we type s you may expect pressing tab would autocomplete to service, but in this case there are more than a few options for words starting with s .

$ kubectl get s [TAB]

secrets signalfxs.config.istio.io

serviceaccounts solarwindses.config.istio.io

servicecontrolreports.config.istio.io stackdrivers.config.istio.io

servicecontrols.config.istio.io statefulsets.apps

serviceentries.networking.istio.io statsds.config.istio.io

servicerolebindings.rbac.istio.io stdios.config.istio.io

serviceroles.rbac.istio.io storageclasses.storage.k8s.io

services

If you write out service you can tab to show all of the available services in your current namespace.

$ kubectl get service [TAB]

details helloworld kubernetes productpage ratings reviews

To see the whole list of available commands, type kubectl followed by two tab’s.

$ kubectl [TAB][TAB]

annotate certificate create explain plugin set

api-resources cluster-info delete expose port-forward taint

api-versions completion describe get proxy top

apply config diff label replace uncordon

attach convert drain logs rollout version

auth cordon edit options run wait

autoscale cp exec patch scale

kubectx & kubens: switch back and forth between Kubernetes contexts & namespaces

If you find yourself bouncing around between multiple Kubernetes contexts and/or multiple namespaces it can be helpful to have tools to shorten the process. Using kubectx you can easily list, switch, rename, and delete contexts. kubens similarly allows you to list and switch between name spaces. Both tools also support tab completion.

Check out this article for instructions about how to configure kubectx and kubens .

Additionally, you can make kubectx and kubens even more handy by adding an interactive menu, such as fzf . Follow these steps to install fzf . fzf is a command-line fuzzy finder that provides you with an interactive menu of contexts or namespaces to choose from.

Using kubectx

To list contexts:

$ kubectx

context-ctdazdbmi3t

docker-for-desktop

To switch contexts:

$ kubectx docker-for-desktop

Switched to context "docker-for-desktop".

To return to the previous context:

$ kubectx -

Switched to context "context-ctdazdbmi3t".

To rename a context:

$ kubectx oracle=context-ctdazdbmi3t

Context "context-ctdazdbmi3t" renamed to "oracle".

The same commands apply when kubectx is used with fzf . The only difference is the appearance of an interactive menu.

$ kubectx > docker-for-desktop

context-ctdazdbmi3t

2/2

Using kubens

To list namespaces:

$ kubens

default

istio-system

kube-public

kube-system

sample

spinnaker

To switch namespaces:

$ kubens spinnaker

Context "context-ctdazdbmi3t" modified.

Active namespace is "spinnaker".

To return to the previous namespace:

$ kubens -

Context "context-ctdazdbmi3t" modified.

Active namespace is "default"

Using kubectx with fzf

The same commands apply when kubens is used with fzf . As was the case with kubectx , the only difference is the appearance of an interactive menu.

$ kubens > default

istio-system

kube-public

kube-system

sample

spinnaker

6/6

kube-ps1: Kubernetes prompt for bash and zsh

kube-ps1 adds your current Kubernetes context and namespace to your prompt string. This tool saves you from having to type out kubectl config current-context every time you need to check your context or determine your current namespace. This is a simple change that can make interacting with your cluster a little easier.

Also, if you get tired of seeing your context and namespace in your prompt string, you can simply turn off kube-ps1 in your shell session with kubeoff . It can be turned back on with kubeon . To make this change take effect globally across shell sessions add the -g flag to the end of the command.

Check out this article for instructions about how to configure kube-ps1.

After it has been installed, your shell should look something like this:

(⎈ |[context]:[namespace]) $

For example my prompt string looks like this:

(⎈ |context-ctdazdbmi3t:default) $

kubectl Aliases

Shell aliases for bash, zsh, etc. can be used to shorten any and all shell commands, not just those related to kubectl . In this case there is nothing additional to install in your environment. It is a good practice to separate the aliases related to kubectl into their own file and source it in your shell runtime config (e.g. bashrc). To create your own set of kubectl aliases, begin by creating a file in your $HOME directory, for instance one named .kube_alias:

$ vi .kube_alias

Begin by adding in a simple alias such as alias k=’kubectl’ and save the file. Edit your .bashrc or .zshrc file with the following:

[ -f ~/.kubectl_alias ] && source ~/.kubectl_alias

This will tell your shell to reference your newly created alias file. The next time you enter k in your shell, it will be treated as if you typed kubectl instead.

Aliases can be created to simplify any number of commands. For instance, if you find yourself frequently typing out kubectl get pods --all-namespaces to get a list of pods running in every namespace in your cluster, you could write an alias, such as kgpall to shorten the command. In your .kube_alias file that alias would look like this: alias kpgall=’kubectl get pod --all-namespaces’ .

This page has a number of helpful aliases for use with Kubernetes. Even if you choose not to use this list, it can give you some helpful ideas for aliases to write yourself.

Conclusion

After reading this article I hope you have a better understanding of command line tools that can make it easier for you to interact with Kubernetes. I also hope you feel inspired to explore the ecosystem of available tools or even create ones of your own.