Tips to improve your productivity with kubectl

This article explains tips to increase your efficiency with kubectl. What is Kubectl?

image.pngThe new kubectl logo is a command-line utility that helps to interact with Kubernetes API. kubectl helps to create, update, delete, and get API objects for the declarative configuration schema for the system. For a complete list of kubectl operations, see Overview of Kubectl. For installation instructions see installing kubectl. Let's get started.

Tip #1 - Using the Tab key for Autocomplete

While everyone uses Tab key on their terminals to autocomplete commands, kubectl also provides the feature of using Tab key for autocompletion. To setup autocomplete on Bash, first, you need to ensure you have installed bash-completion using the following command: brew install bash-completion #Install bash-completion source <(kubectl completion bash) #Sets auto completion in Bash

To setup autocomplete on ZSH use the following command:

source <(kubectl completion zsh)

Auto completion works for verbs, resources as well as for flags.

Tip #2 You can use VS Code as your default editor

Many times we need to edit resources right from the cluster. While you can do so using your default editor (vim/nano) but you can also set VS Code as your default editor to edit resources. Simply set the environment variable KUBE_EDITOR to VS Code using the following command:

$ export KUBE_EDITOR = `code - wait`

While you can also use a flag - edit with kubectl create command to edit the resources before applying. eg. $ kubectl create -f life.yml - edit

Tip #3 You can access documentation inside your terminal

If you are feeling lazy and want to refer the documentation for any resources, you can do so by simply explaining it.

$kubectl explain pods

This command shows you all the information of definitions, fields on your terminal. It just makes an API request to Kubernetes cluster, grabs the current documentation of the API version running in the cluster, and outputs it on the terminal.

Extra Tip - Always refer kubectl cheatsheet for some great explanation of kubctl commands.

Reference Docs


References- I learned about these tips in the session ' Unleashing the Power of kubectl by Suraj Narwade, Platform Engineer, uSwitch' at Kubernetes Forum Delhi 2020. kubernetes.io/docs/reference/kubectl/cheats..

Suggestions and Feedback are always welcomed!