DEV Community

Cover image for Maximize Kubectl Use with These Tips and Pointers
Ashok Sharma
Ashok Sharma

Posted on

Maximize Kubectl Use with These Tips and Pointers

Kubectl is the command-line tool of Kubernetes, which allows users to run commands against Kubernetes clusters. It enables the deployment of applications as well as the inspection and management of cluster resources. Also, it can be used in viewing Kubernetes logs.

While Kubectl is not as simple to use as WYSIWYG tools, it is an excellent tool for its intended purpose. It would be more convenient and easier to use when you become more familiar with it and with the help of the following tips.

1. Have a commands cheat sheet

There are many commands you can use with Kubectl, so it is understandable that remembering all of them is not going to be easy. It helps to have a guide or cheat sheet that shows the most important commands you need to remember, preferably arranged based on their uses and with information on how to use them.

Cheat sheets provide a convenient guide to basic syntax, keywords, contexts, and other crucial information that take some time to memorize. It makes it easy to remember commands such as getting the current contexts, switching context, getting the name of the containers of a running pod, and getting the value of a secret.

A good cheat sheet is one you prepared yourself, something you created after going over all the commands you are set to use. Preparing the cheat sheet is an excellent starting point to become familiar with the commands, especially when you take the time to separate the declarative and imperative commands, the deployment management commands, and those used to interact with pods.

2. Use the command completion feature

A key feature many tend to overlook, the command completion feature, allows Kubectl users to save the amount of time they spend typing. Using the Tab key, it auto-completes individual parts of Kubectl commands, including sub-commands, arguments, and options. It can even auto-complete resource names, which are rather difficult to type. This feature is available for both the Zsh and Bash shells.

To use the command completion feature, you need a completion script. This is a shell script that sets the parameters for how the auto-complete happens for specific commands. You can automatically generate scripts using the following commands: "kubectl completion bash" or "kubectl completion zsh".

3. Uses auto-generated aliases

Another way to reduce your typing job with Kubectl is by taking advantage of auto-generated aliases. There are hundreds of aliases involved in Kubectl commands. Remembering them is already a challenge; typing them in makes the job more tiresome.

To make things easier, Kubectl allows you to input aliases that are based on an ingenious scheme wherein an alias represents the base, system, operation, resource, and option elements of a command. The alias "ksysgsvcw", for example, stands for the base kubectl, the system n kube-system, the operation get, the resource svc (service), and the option w (watch).

The order of the components in the alias is negligible, and not all components of the alias are required. The alias "kgpooyamlall", which represents the command "kubectl get pods -o yaml - all-namespaces", for instance, can be written as "kgpoalloyaml". You just have to evaluate the resulting command that appears after inputting the alias. Tweak it to match the command you have in mind.

Kubectl aliases are not a native feature of Kubectl, though. This means you have to install it first to be able to use it. You can get the installer file from GitHub (the kubectl-aliases file)) and source it in the ~/.bashrc or ~/.zshrc file.

4. Take advantage of the custom columns output format

The custom columns output format expands the information that can be viewed with the "kubectl get" command. It allows you to configure the columns and data that will be displayed with the Get command. The standard format is not bad or patently insufficient, but it can be significantly better.

To use the custom columns output format, you have to set each output column as a ":" pair, wherein is the name of the column (can be anything you prefer) and is the detail of the resource field. The Kubernetes website has a guide on how to do this.

5. Use 'kubectl explain' to search for resource specifications quickly

If you are new to Kubectl, remembering everything about how to use it is not going to be a walk in the park. Just like in the case of the commands, it is not going to be a piece of cake remembering all the fields and the meanings of resources when generating YAML resource definitions. You may have to go to the API reference to find the information you want. Switching between web browsers or windows to work on your project and check the API reference containing the full specifications of all resources can be quite tedious.

Kubectl addresses this difficulty with the "kubectl explain" command. This allows you to produce a copy of resource specifications of all resources within your terminal. The command displays the specification of the resource or field you request based on the information contained in the API reference.

The default "kubectl explain" result shows only one level of fields, but you can pull out the entire tree of fields by using the " - recursive" flag. If you cannot remember, or you are unsure of the resource names to use with the "kubectl explain" command, you can display all the resource names by using the following command: "kubectl api-resources".

6. Use shortcuts to switch between clusters and namespaces easily

Switching between clusters and namespaces in Kubectl is a tedious process. You have to manually edit the kubeconfig file to do it. There are easier ways, though, as described below.

Use Kubectx - Kubectx is a popular Kubectl tool that enables the "kubectx" and "kubens" commands, which make it easy to switch between namespaces and clusters. These commands come with the command completion function that can be enabled through a completion script. You can get the instructions on how to use these commands and the completion script on the GitHub Kubectx page. Kubectx also comes with an "interactive" mode, which enables the selection of the target context or namespace via a search interface.

Install plugins - A number of Kubectl plugins are available to make the switching process faster. These include kubectl-foo, kubectl-ctx, and kubectl-ns. Once installed, they can be brought out similar to native commands and enable the fast switching between namespaces and aliases.

Use shell aliases - There are also aliases integrated within Kubectl to facilitate quick cluster and namespace switching. These aliases are used to change the current context and namespace when editing the kubeconfig files. This option is not as easy as the first two listed above, though.

Many may find Kubectl challenging, but it is not an extremely difficult challenge. You only need to spend some time, effort, and patience to get to know it better and take advantage of the different features and third-party tools you can use with it. It is one of the best Kubernetes tools in the hands of someone committed to learning and using it optimally.

Image: Pexels

Top comments (0)