In this post, we will demonstrate how to install kubectl, set up auto-completion, and set an alias for kubectl in your Linux Bash Shell, Zsh, and PowerShell.
What we will cover:
- What is kubectl autocomplete?
- Benefits of using kubectl autocomplete
- How to set up kubectl autocomplete in a Linux Bash Shell
- How to set up kubectl autocomplete in Mac Zsh
- How to set up kubectl autocomplete in PowerShell
- Kubectl autocomplete in action – example
- Why is kubectl autocomplete not working?
Kubectl autocompletion is a feature that lets you set up an alias for kubectl on the command line (commonly as k
). Auto-completion can be very useful to avoid having to reference a cheat sheet constantly when using kubectl commands to interact with your Kubernetes cluster. It also helps save a lot of time when managing your cluster, and is recommended for the CKA administrator exam as it is very time limited.
There are many benefits to using any autocomplete tool, but when it comes to Kubernetes, this is even more helpful, as kubectl commands can take a very long time. Let’s explore some of the benefits:
- Efficiency – reduce the amount of typing required, especially in very long commands
- Minimize errors – by suggesting command completions, you minimize the chances for mistyping, thus reducing overall frustration
- Enhanced learning – this is beneficial, especially for people just starting out with K8s, as auto-completion can serve as an education tool by showing available commands and options users may not be familiar with
- Increased productivity – streamline workflows by allowing users to execute commands more quickly and accurately
First, let’s set up kubectl autocomplete for a Linux Bash Shell
1) Install Kubectl on Linux
Open your bash shell. Type kubectl
. If you see the message ‘kubectl: command not found’, follow these steps to install kubectl
. If it is installed, skip to 2) Set up Auto-completion.
Download the latest kubectl
release with the command:
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
Download the checksum file:
curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
Validate the kubectl binary against the checksum file:
echo "$(cat kubectl.sha256) kubectl" | sha256sum --check
Install kubectl:
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
Type kubectl
. You should now see the tool is installed.
2) Set up Auto-completion
Check if bash-completion
is already installed:
type _init_completion
If it is already installed, you will see something like the following:
If it is not installed, install using apt or yum, depending on which package manager you are using (usually apt for Ubuntu):
apt-get install bash-completion
or
yum install bash-completion
Set the kubectl completion script source for your shell sessions:
For all users on the system:
kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl > /dev/null
Reload your bash shell. Type kubectl -
followed by pressing tab twice to see the available options and verify auto-complete is working:
3) Set up an alias for kubectl and enable auto-completion
Set an alias for kubectl as k
.
echo 'alias k=kubectl' >>~/.bashrc
Enable the alias for auto-completion.
echo 'complete -o default -F __start_kubectl k' >>~/.bashrc
Reload your bash shell. Type k -
followed by pressing tab twice to see the available options and verify auto-complete is working with the alias.
k -
To setup kubectl autocomplete, you will need to initially run the following commands:
mkdir -p ~/.oh-my-zsh/custom/plugins/kubectl-autocomplete/
kubectl completion zsh > ~/.oh-my-zsh/custom/plugins/kubectl-autocomplete/kubectl-autocomplete.plugin.zsh
Then, you should edit your zsh profile (~/.zshrc) and add the kubectl autocomplete inside the plugins:
plugins=(kubectl-autocomplete)
To set up kubectl autocomplete in Windows, you just need to run:
kubectl completion powershell >> $PROFILE
To use the kubectl autocomplete, you just need to start typing a command you want to use, and by pressing tab, you will get suggestions for how to autocomplete.
There are several reasons why kubectl autocomplete may not be working:
- Shell Compatibility – kubectl autocomplete is supported in zsh, bash, and powershell.
- Installation Path – you should ensure that your kubectl executable is accessible in the system PATH.
- Shell aliases – if you are using an alias for kubectl, you might need to also set up autocompletion for that alias.
- Profile not sourced – if you’ve just changed your profile, you need to source it or restart the terminal to ensure autocompletion is working properly.
- Incomplete installation – sometimes autocompletion dependencies may be a part of a different package, so depending on how you’ve installed kubectl, you may need to install something else.
Enabling auto-completion for kubectl
and setting an alias will save you time!
Additionally, Spacelift can help you manage the complexities and compliance challenges of using Kubernetes. Anything that can be run via kubectl can be run within a Spacelift stack. Read about how Spacelift works with Kubernetes. If you want to learn more, create a free account today or book a demo with one of our engineers.
Cheers!
Continuous Integration and Deployment for your IaC
Spacelift allows you to automate, audit, secure, and continuously deliver your infrastructure. It helps overcome common state management issues and adds several must-have features for infrastructure management.