Solving the DevOps Infrastructure Dilemma: Enabling developer velocity with control 💡

Register for the webinar here →

Kubernetes

Enable Auto-Completion for Kubectl [Bash, Zsh, PowerShell]

Enable Auto-completion for Kubectl in a Linux Bash Shell

What is kubectl autocomplete?

Benefits of using kubectl autocomplete

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

How to set up kubectl autocomplete in a Linux Bash Shell

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.

kubectl autocompletion

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.

kubectl autocompletion - installation

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:

kubectl autocompletion - bash-completion

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
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:

kubectl autocompletion - Reload your bash shell

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 -
kubectl autocompletion - Reload your bash shell again

How to set up kubectl autocomplete in Mac Zsh

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)

How to set up kubectl autocomplete in PowerShell

To set up kubectl autocomplete in Windows, you just need to run:

kubectl completion powershell >> $PROFILE

Kubectl autocomplete in action - examples

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.

kubectl autocomplete example

Why is kubectl autocomplete not working?

There are several reasons why kubectl autocomplete may not be working:

  1. Shell Compatibility – kubectl autocomplete is supported in zsh, bash, and powershell.
  2. Installation Path – you should ensure that your kubectl executable is accessible in the system PATH.
  3. Shell aliases – if you are using an alias for kubectl, you might need to also set up autocompletion for that alias.
  4. 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.
  5. 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.

Key points

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.

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.

Start free trial

The Practitioner’s Guide to Scaling Infrastructure as Code

Transform your IaC management to scale

securely, efficiently, and productively

into the future.

ebook global banner
Share your data and download the guide