kubectl autocomplete: Enabling and Using in Bash, Zsh and PowerShell

What Is kubectl autocomplete? 

kubectl autocomplete is a feature that allows for command-line autocompletion when working with the Kubernetes command-line interface (CLI), kubectl. kubectl autocompletion is an extension to the Tab key completion feature of shell environments like Bash and Zsh. This can make it easier to enter kubectl commands and interact with your Kubernetes cluster.

When you start typing a kubectl command, the autocomplete feature can predict and suggest the rest of the command for you. Not only does it save you keystrokes, but it also helps you remember the exact commands and their syntax. For instance, if you begin typing kubectl get, the autocomplete can suggest different objects that match this command, such as pods, saving you time and effort.

You can also set an alias for kubectl on the command line, commonly k, to save time when managing your cluster.

However, kubectl autocomplete is not enabled by default. To make use of this feature, you need to configure it manually. This can be done in several ways, depending on your shell and operating system—details in the following section.This is part of a series of articles about kubectl Cheat Sheet.

Setting Up Kubectl Autocomplete in Your Shell Environment 

Setting up kubectl autocomplete depends on the shell you’re using. Here are the options for the most common shells.

Remember to replace kubectl with your chosen alias if you’re using one. For instance, if you’ve aliased kubectl as k, replace kubectl with k in the scripts below.

Kubectl Autocomplete in Bash (Linux)

To set up kubectl autocomplete in Bash on Linux, you need to install bash-completion first. Once installed, you can add the completion script to your .bashrc file. This will enable kubectl autocomplete every time you start a new Bash session.

# install bash-completion
sudo apt-get install bash-completion

# Add the completion script to your .bashrc file
echo 'source <(kubectl completion bash)' >>~/.bashrc

# Apply changes
source ~/.bashrc

Kubectl Autocomplete in Bash (MacOS)

Setting up kubectl autocomplete in Bash on macOS is slightly different. First, you need to install bash-completion using Homebrew. Then, you need to add the completion script to your .bash_profile file. This will enable kubectl autocomplete for your macOS terminal sessions.

# Install bash-completion using Homebrew
brew install bash-completion

# Add the completion script to your .bash_profile
echo 'if [ -f /usr/local/share/bash-completion/bash_completion ]; then
. /usr/local/share/bash-completion/bash_completion
fi
source <(kubectl completion bash)' >>~/.bash_profile

# Apply changes
source ~/.bash_profile

Kubectl Autocomplete in Zsh

If you’re using Zsh, setting up kubectl autocomplete is straightforward. You simply need to add the completion script to your .zshrc file. This will enable kubectl autocomplete for all your Zsh sessions.

# Add the following to your .zshrc file
echo 'source <(kubectl completion zsh)' >>~/.zshrc

# Apply changes
source ~/.zshrc

Kubectl Autocomplete in PowerShell

For PowerShell users, setting up kubectl autocomplete involves adding the completion script to your PowerShell profile. This will enable kubectl autocomplete for all your PowerShell sessions.

# Add the completion script to your PowerShell profile
Add-Content -Path $PROFILE -Value 'source <(kubectl completion powershell)'

# Apply changes
. $PROFILE
expert-icon-header

Tips from the expert

Itiel Shwartz

Co-Founder & CTO

Itiel is the CTO and co-founder of Komodor. He’s a big believer in dev empowerment and moving fast, has worked at eBay, Forter and Rookout (as the founding engineer). Itiel is a backend and infra developer turned “DevOps”, an avid public speaker that loves talking about things such as cloud infrastructure, Kubernetes, Python, observability, and R&D culture.

In my experience, here are tips that can help you better utilize kubectl autocomplete:

Ensure Correct Shell Configuration

Verify that your shell (Bash, Zsh, or PowerShell) is properly configured for kubectl autocomplete. Misconfiguration can lead to incomplete or non-functional autocomplete.

Keep Your Shell and kubectl Updated

Regularly update your shell and kubectl to the latest versions to ensure compatibility and access to new features. Autocomplete functionality improves with updates.

Set Up Aliases for Common Commands

Create aliases for frequently used kubectl commands (e.g., alias k=kubectl) to save time. Combine these with autocomplete for faster and more efficient command execution.

Customize Autocomplete for Specific Needs

Tailor the autocomplete setup to your workflow by modifying your shell configuration files (.bashrc, .zshrc, or PowerShell profile). Enable autocompletion for custom commands and plugins.

Use Autocomplete for Resource Names and Contexts

Leverage autocomplete for resource names, namespaces, and contexts to avoid typos and ensure accurate command execution. This is especially useful in large clusters with numerous resources.

3 Reasons You Should Use kubectl autocomplete 

When working in a command-line environment, every keystroke counts. Here are some benefits of using autocomplete with kubectl:

  • Efficiency and speed: By predicting and filling in the rest of your commands, it saves you valuable time and keystrokes. You no longer need to type out the full commands, which can be quite lengthy in Kubernetes. 
  • Accuracy: With autocomplete, you minimize the risk of typos and syntax errors that could lead to failed commands or unintended actions. 
  • Learning and discovery: kubectl autocomplete is also a learning tool. If you’re new to Kubernetes, remembering all the commands and their syntax can be daunting. Autocomplete helps you learn these commands and their correct usage, and discover new kubectl commands that you might not have been aware of.

Examples of autocomplete with various kubectl Commands 

Basic Commands

The ‘kubectl autocomplete’ feature is incredibly useful when dealing with basic commands. For instance, if you type kubectl get p and press the tab key, the autocomplete feature will complete the command to kubectl get pods. This feature is not just limited to resource commands; it can autocomplete other commands like describe, delete, logs, and more.

Learn more in our detailed guide to kubectl logs

Resource Names

kubectl autocomplete can also help with resource names. Large clusters might have multiple resources, each with unique names that can be difficult to remember. 

For example, if you type kubectl describe pod web and press the tab key, the autocomplete feature will list all pods that start with web, such as web-production-7

This is a lifesaver when you have multiple resources with similar names. It allows you to select the correct resource without having to remember its exact name.

Namespaces

When dealing with namespaces, kubectl autocomplete can be a big help. Kubernetes uses namespaces to divide cluster resources between multiple users or projects.

For instance, if you type kubectl get pods --namespace=dev and press tab after dev, the autocomplete feature will suggest all available namespaces starting with dev

This can be extremely helpful when you are managing numerous namespaces and need to switch between them frequently.

Learn more in our detailed guide to kubectl restart pod

Contexts

Lastly, kubectl autocomplete is beneficial when working with contexts. Contexts in Kubernetes allow you to switch between different clusters and namespaces. Instead of having to remember the exact context name, you can use kubectl autocomplete.

For example, if you type kubectl config use-context prod and press the Tab key, kubectl autocomplete will suggest all contexts that start with prod. This simplifies the process of switching between different contexts.

Common Issues When Setting Up or Using kubectl autocomplete 

Incorrect Shell Configuration

One common issue that users encounter when setting up or using ‘kubectl autocomplete’ is an incorrect shell configuration. The autocomplete feature works with bash, zsh, and PowerShell. However, the setup process is different for each shell. If you set up ‘kubectl autocomplete’ for the wrong shell, it will not work.

Version Incompatibility

Another common issue is version incompatibility. kubectl autocomplete may not work if the version of your kubectl does not support it. You may need to upgrade your kubectl to the latest version to use the autocomplete feature.

Misspelled or Incorrect Commands

While kubectl autocomplete is an excellent tool for mitigating typos, it can still fail if you misspell commands or use incorrect syntax. The autocomplete feature can only suggest commands based on what it recognizes.

Lack of Permissions

Finally, lack of permissions can also prevent you from using kubectl autocomplete. If you do not have the necessary permissions to execute certain commands or access specific resources, the autocomplete feature may not work as expected. It is important to ensure that you have the required permissions to use kubectl autocomplete effectively.

Kubernetes Troubleshooting With Komodor

Kubernetes is a complex system, and often, something will go wrong, simply because it can. In situations like this, you’ll likely begin the troubleshooting process by reverting to some of the above kubectl commands to try and determine the root cause. This process, however, can often run out of hand and turn into a stressful, ineffective, and time-consuming task.

This is the reason why we created Komodor, a tool that helps dev and ops teams stop wasting their precious time looking for needles in (hay)stacks every time things go wrong.

Acting as a single source of truth (SSOT) for all of your k8s troubleshooting needs, Komodor offers:

  • Change intelligence: Every issue is a result of a change. Within seconds we can help you understand exactly who did what and when.
  • In-depth visibility: A complete activity timeline, showing all code and config changes, deployments, alerts, code diffs, pod logs and etc. All within one pane of glass with easy drill-down options.
  • Insights into service dependencies: An easy way to understand cross-service changes and visualize their ripple effects across your entire system.
  • Seamless notifications: Direct integration with your existing communication channels (e.g., Slack) so you’ll have all the information you need, when you need it.

If you are interested in checking out Komodor, use this link to sign up for a Free Trial.