If you are someone who frequently interacts with a Kubernetes cluster, you may find yourself frustrated by the time spent 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.
Pre-requisites: The default prompt assumes you have the kubectl command line utility installed along with a Kubernetes cluster. Check out this article for instructions on installing kubectl. To get access to a Kubernetes cluster using Oracle Container Engine for Kubernetes (OKE) follow this friendly guide.
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 and also 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
Which means that when we type s you would 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
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 simply lists and switches 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. fzf is a command-line fuzzy finder that provides you with an interactive menu of contexts or namespaces to choose from.
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
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"
The same commands apply when kubens is used with fzf. The only difference is the appearance of an interactive menu.
$ kubens > default istio-system kube-public kube-system sample spinnaker 6/6
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, you can simply turn off kube-ps1 in your shell session with kubeoff. It can be turned back on with kubeon. To make it 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]) $ (⎈ |context-ctdazdbmi3t:default) $
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 sourcing 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 .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 would look like this: alias kpgall="kubectl get pods --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.
After reading this article I hope you have a better understanding of command line tools that 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.