Enabling Kubectl Autocompletion

After installing kubectl and get it configured to connect to your Kubernetes cluster, you might want to configure shell autocompletion. This would save you a lot of time while running kubectl commands to manage your cluster. By doing so, you’ll then just use the tab completion for command names and actual object names. For instance, if you had to display detailed information about a pod named mynginx-6867b5b964-599ln, instead of having to write the whole command and the pod name, you can just use tab keys to get the command completion:

This will result to the following:

Luckily, the kubectl command completion is very easy to enable. Here’s how to do it for bash on a CentOS 7 box.

Enabling shell autocompletion

If not installed, you’ll first need to install a package called bash-completion

Then, run the following command to activate kubectl autocompletion to your current shell.

The above command will enable kubectl bash completion for the current shell only, so if you logout from your session or close your shell, you’ll have to to run this command again after you login. If instead, you want to add kubectl autocompletion to your profile (in the .bahsrc file), so that it is automatically loaded in future shells run, run the following command:

The last step is to source your .bahsrc file to make the changes effective instantly without having to logout and login again.

That’s it. Now you’re all set to start interacting with your cluster without having to type long commands.

This was about how to enable autocompletion on Linux, if you are managing a Kubernetes cluster from your macOS, just type kubectl completion -h on any Linux shell and you’ll get more details to install kubectl autocompletion on Mac.




Find this post interesting. Share it!

Leave a Comment

Your email address will not be published. Required fields are marked *