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:
1 2 3 |
$ kubectl desc<TAB> p<TAB> my-nginx<TAB> |
This will result to the following:
1 2 3 |
$ kubectl describe pod mynginx-6867b5b964-599ln |
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
1 2 3 |
$ sudo yum install bash-completion -y |
Then, run the following command to activate kubectl autocompletion to your current shell.
1 2 3 |
$ source <(kubectl completion bash) |
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:
1 2 3 |
$ echo "source <(kubectl completion bash)" >> ~/.bashrc |
The last step is to source your .bahsrc file to make the changes effective instantly without having to logout and login again.
1 2 3 |
$ source .bashrc |
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.
1 2 3 |
$ kubectl completion -h |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
Output shell completion code for the specified shell (bash or zsh). The shell code must be evaluated to provide interactive completion of kubectl commands. This can be done by sourcing it from the .bash _profile. Detailed instructions on how to do this are available here: https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion Note for zsh users: [1] zsh completions are only supported in versions of zsh >= 5.2 Examples: # Installing bash completion on macOS using homebrew ## If running Bash 3.2 included with macOS brew install bash-completion ## or, if running Bash 4.1+ brew install bash-completion@2 ## If kubectl is installed via homebrew, this should start working immediately. ## If you've installed via other means, you may need add the completion to your completion directory kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl # Installing bash completion on Linux ## If bash-completion is not installed on Linux, please install the 'bash-completion' package ## via your distribution's package manager. ## Load the kubectl completion code for bash into the current shell source <(kubectl completion bash) ## Write bash completion code to a file and source if from .bash_profile kubectl completion bash > ~/.kube/completion.bash.inc printf " # Kubectl shell completion source '$HOME/.kube/completion.bash.inc' " >> $HOME/.bash_profile source $HOME/.bash_profile # Load the kubectl completion code for zsh[1] into the current shell source <(kubectl completion zsh) # Set the kubectl completion code for zsh[1] to autoload on startup kubectl completion zsh > "${fpath[1]}/_kubectl" Usage: kubectl completion SHELL [options] Use "kubectl options" for a list of global command-line options (applies to all commands). |