When you have to manage one or several Kubernetes clusters it is very likely that you'll spend a good amount of time typing kubectl commands. Kubectl has a lot of commands and each command can have many flags and options so, apart from the three or four most used commands, it's going to be impossible to work with kubectl without a cheatsheet. Also, the more specific you are with your command the longer your command will be.
To help us with this, there are a couple of features that we can enable in our shell sessions to work more efficiently with kubectl:
- Autocomplete - Bash autocomplete, also known as tab completion, is a feature of the Bash shell that helps users quickly complete commands, file names, directories, and other entities by pressing the Tab key. When you type part of a command or file name and press Tab, Bash will either complete the command if it's unambiguous or provide a list of possible commands if there are multiple matches.
- Alias - The alias feature in Bash allows you to create shortcuts or alternative names for commands or series of commands. This can be useful for shortening frequently used commands, simplifying complex command sequences, or even creating custom command behaviors.
Before diving into these two features, ensure your kubectl can connect to your cluster(s). For that, make sure you’ve set up correctly your kubeconfig file(s). Check out this post if you want to know more how to manage your kubeconfig.
Autocomplete for kubectl
Follow the next steps:
- Check if bash-completion is installed
type _init_completion
- If you get an output like the picture above that means bash_completion is installed. If not, install it with the following command
apt-get install bash-completion
kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl > /dev/null
- Next you need to reload your bash shell. For that you can close your current session and open a new one or run the command
source ~/.bashrc
Setting up an alias for kubectl
- To create an alias, you use the
alias
command followed by the name of the alias and the command it should execute. The basic syntax is:
alias alias_name='command'
k=kubectl
, run the command: echo 'alias k=kubectl' >>~/.bashrc
source ~/.bashrc
k get nodes
Enable autocompletion for your kubectl alias
- Lastly, if you noticed, when you set up your kubectl alias, autocomplete does not work. That’s because we need to tell our Operating System that we want to use automcomplete also for our alias.
- To do that, run the following
echo 'complete -o default -F __start_kubectl k' >>~/.bashrc
More alias for kubectl
As we mentioned above, an alias can be a shortcut for a command but also for more complex expressions. In our case, with kubectl, this means we can set up an alias for a kubectl command followed by options and flags which would reduce a lot those long specific commands when we want to get something specific.There are no standards for this, every K8s admin has their particular preferences. In my case this is the list of alias I like to use that reduce a lot my work with kubectl
alias k=kubectl
alias kg='kubectl get'
alias kd='kubectl describe'
alias kgn='k get nodes'
alias kgp='k get pods'
alias kgpa='kgp -A'
alias nano='KUBE_EDITOR=nano'
alias dr='--dry-run=client -o yaml'
alias kaf='k apply -f'