Hello friends,
Today I wanna share a quick tip on how to set up the auto-completion feature for
kubectl
in our local machine. It will help us type the command faster and also reduce typo mistakes.
Don’t know how to set up a Kubernetes cluster locally? Check out my post here: How to setup a single node Kubernetes cluster on local environment for testing purpose?
In case you’re using macOS and zsh.
The kubectl completion script for Zsh can be generated with the command kubectl completion zsh
.
trungtran@Trungs-MacBook-Air ~ % vi ~/.zshrc
trungtran@Trungs-MacBook-Air ~ % cat ~/.zshrc
autoload -Uz compinit
compinit
source <(kubectl completion zsh)
Then exit the terminal and start it again, try the kubectl
command and enjoy the auto-completion feature. For example, when I type the below command (1) and press the tab
key, it will auto-complete the command like the (2) line.
(1) trungtran@Trungs-MacBook-Air ~ % kubectl get dep
(2) trungtran@Trungs-MacBook-Air ~ % kubectl get deployments.apps
Or the suggestions like the below case:
trungtran@Trungs-MacBook-Air ~ % kubectl get p
persistentvolumeclaims
persistentvolumes
poddisruptionbudgets.policy
pods
podtemplates
priorityclasses.scheduling.k8s.io
prioritylevelconfigurations.flowcontrol.apiserver.k8s.io
In case you use PowerShell on Windows.
Open your Powershell run the following command
kubectl completion powershell >> $PROFILE
Then restart your Powershell and enjoy the auto-completion feature.
In case you faced the below issue, make sure you have kubectl version >= 1.23.
PS C:\Users\trung> kubectl completion powershell >> $PROFILE
error: Unsupported shell type "powershell".
See 'kubectl completion -h' for help and examples
Happy learning,
Trung.