In container orchestration, Kubernetes stands tall as the go-to platform, with its command-line tool, kubectl
, acting as the powerful wand in the hands of its wizards. While undeniably potent, its vast array of commands and options can sometimes make even seasoned professionals fumble.
After all, it is hard to remember all the countless options and combinations between them that the kubectl
tool offers.
But even if you are among the most seasoned professionals who know them by heart, there will always be that one moment with their tedious input, sometimes bordering on writing entire sentences.
What if there was a way to make this tool more intuitive and efficient with the ability to automatically complete (hint) the available options, similar to the auto-complete you are used to with other Linux commands?
Weโve got good news โ you can get this functionality quickly and easily, and weโll show you how immediately. So, are you ready to accelerate your Kubernetes tasks with kubectl
auto-completion? Letโs dive in!
Enable Kubernetes’ Kubectl Auto-Completion
First, make sure your Linux system has the “bash-completion” package installed. Most distributions come with it installed by default, but if not, you can easily add it depending on the package manager you use, APT, DNF, Pacman, etc.
And now to the fun part. The kubectl command has built-in functionality to ะพutput shell completion code for specified shell – Bash or Zsh. Since Bash is the most widely used and almost all Linux distributions come with it as a default, we will use it.
So, open the terminal, navigate to your home directory (if you are not in it), and generate the kubectl
auto-completion script for Bash with the following command:
kubectl completion bash > .kubectl_completion_bash
Code language: CSS (css)
The command will create a “.kubectl_completion_bash” file in your home directory containing everything you need for the auto-completion functionality of the kubectl
command.
The next step is to tell our system to use it. To do this, first open the “.bashrc” file with the terminal text editor you are using.
nano ~/.bashrc
Then go to the very end of it and add the following line:
source ~/.kubectl_completion_bash
Save the file and exit. Finally, to enable kubectl
auto-completion immediately in the current shell session, source the file:
source ~/.bashrc
Now try typing a kubectl
command, using the familiar repeated press of “Tab” to autocomplete the command or display the available options. The magic is done – everything should now work as expected.
Conclusion
As we’ve explored, enabling kubectl
auto-completion on Linux isn’t just a matter of convenience; it is an enhancement that can dramatically streamline your Kubernetes operations.
By integrating this feature, we not only minimize the risk of command errors but also foster a more efficient and enjoyable Kubernetes command-line experience.
For detailed information about the kubectl
command line tool and all the options it offers, refer to its official documentation.