How to Enable Auto-Completion for Kubectl (Linux, Mac and Windows)

enable auto completion for kubectl

If you frequently use kubectl to manage your Kubernetes clusters, you know how powerful this command-line tool is. However, typing long commands and remembering exact resource names can be a hassle. Fortunately, enabling auto-completion for kubectl can make your life much easier. With auto-completion, you can quickly fill in commands, arguments, and resource names, which speeds up your workflow and reduces errors.

In this guide, I’ll explain how to enable auto-completion for kubectl on Linux, Windows, and Mac.

Why Enable Auto-Completion?

Before we get started with the commands, let me explain why enabling auto-completion is so beneficial:

  • Efficiency: Auto-completion allows you to quickly fill in commands, options, and resource names without typing them out entirely.
  • Error Reduction: It helps in reducing typos and syntax errors by suggesting the correct command or resource name.
  • Convenience: Navigating complex Kubernetes environments becomes much easier when you have auto-completion to assist you.

Now, let’s dive into enabling auto-completion for kubectl on Linux, Mac, and Windows.

Enable Auto-Completion for Kubectl in Linux (Bash)

Follow the steps below to enable auto-completion for kubectl in Linux.

Step 1: Verify Kubectl Installation

First, ensure that kubectl is installed and properly configured on your system. You can verify this by running:

 # kubectl version --client

This command should output the version of kubectl you have installed.

Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.0", GitCommit:"abcdef1234567890", GitTreeState:"clean", BuildDate:"2021-05-05T12:34:56Z", GoVersion:"go1.16.3", Compiler:"gc", Platform:"linux/amd64"}

Step 2: Install bash-completion

To enable auto-completion, you need to have bash-completion installed. If it’s not already installed, you can do so using the following command:

 # apt-get install bash-completion

This command installs the necessary package for enabling auto-completion in bash.

Step 3: Enable Kubectl Auto-Completion

Next, you need to configure your shell to use kubectl auto-completion. Open your terminal and add source <(kubectl completion bash) to your ~/.bashrc file.

 # echo "source <(kubectl completion bash)" >> ~/.bashrc

This command appends the required source command to your .bashrc file.

Finally, apply the changes by sourcing your .bashrc:

 # source ~/.bashrc

After running this command, auto-completion should be enabled for kubectl in your bash shell.

Enable Auto-Completion for Kubectl in Mac (Bash)

Here is a step by step guide to enable auto-completion for kubectl on Mac.

Step 1: Install bash-completion

If you’re using a Mac, you can install bash-completion using Homebrew. Run the following command:

 # brew install bash-completion

Step 2: Configure bash-completion

Next, add bash-completion and kubectl auto-completion to your shell configuration file. Open your ~/.bash_profile file in an editor:

 # nano ~/.bash_profile

Add the following lines:

if [ -f /usr/local/share/bash-completion/bash_completion ]; then
  . /usr/local/share/bash-completion/bash_completion
fi
source <(kubectl completion bash)

Save the file and exit the editor.

Step 3: Apply Changes

To apply the changes, source your ~/.bash_profile:

 # source ~/.bash_profile

After running this command, auto-completion should be enabled for kubectl in your bash shell on Mac.

Enable Auto-Completion for Kubectl in Windows (Powershell)

Here is step by step guide to enable auto-completion for kubectl in Windows.

Step 1: Install posh-kubectl Module

For Windows users, you can enable auto-completion in PowerShell by installing the posh-kubectl module. Run the following command:

 # Install-Module -Name posh-kubectl -Scope CurrentUser

This command installs the posh-kubectl module, which adds kubectl auto-completion functionality to PowerShell.

Step 2: Configure PowerShell Profile

Next, add the auto-completion command to your PowerShell profile. Run the following command:

 # Add-Content -Path $PROFILE -Value 'source <(kubectl completion powershell)'

This command appends the necessary source command to your PowerShell profile.

Step 3: Apply Changes

To apply the changes, reload your PowerShell profile:

 # . $PROFILE

After running this command, auto-completion should be enabled for kubectl in PowerShell.

Verify Kubectl Auto-Completion

To verify that auto-completion is working, type kubectl followed by a space and then press the Tab key twice. You should see a list of possible completions. For example:

 # kubectl get [Tab][Tab]

You should see output similar to:

clusterroles       customresourcedefinition   limitranges   nodes       secrets
clusters           daemonsets                 localsubjectaccessreviews   persistentvolumeclaims
configmaps         deployments                namespaces    persistentvolumes

Try out a few more commands to understand how auto-completion works. For example:

 # kubectl create [Tab][Tab]

You should see a list of all options you can use with kubectl create command:

kubectl create 
clusterrolebinding   (Create a cluster role binding for a particular cluster role)
clusterrole          (Create a cluster role)
configmap            (Create a config map from a local file, directory or literal value)
cronjob              (Create a cron job with the specified name)
deployment           (Create a deployment with the specified name)
ingress              (Create an ingress with the specified name)
job                  (Create a job with the specified name)
namespace            (Create a namespace with the specified name)
poddisruptionbudget  (Create a pod disruption budget with the specified name)
priorityclass        (Create a priority class with the specified name)
quota                (Create a quota with the specified name)
rolebinding          (Create a role binding for a particular role or cluster role)
role                 (Create a role with single rule)
secret               (Create a secret using specified subcommand)
serviceaccount       (Create a service account with the specified name)
service              (Create a service using a specified subcommand)
token                (Request a service account token)

Conclusion

Enabling auto-completion for kubectl is a simple but powerful enhancement to your command-line experience. It helps you work more efficiently, reduces errors, and makes exploring kubectl commands much easier. Whether you’re using Linux, Mac, or Windows, you can set up auto-completion in just a few steps. Enjoy the improved productivity!

FAQs

1. Why should I enable auto-completion for kubectl?

Auto-completion improves efficiency by reducing typing and errors when using kubectl.

2. How do I verify if kubectl auto-completion is working?

Type kubectl followed by a space, then press Tab twice. A list of suggestions should appear.

3. Can I enable kubectl auto-completion on Zsh instead of Bash?

Yes, you can enable kubectl auto-completion on Zsh by adding source <(kubectl completion zsh) to your ~/.zshrc file and sourcing it.

4. What is bash-completion, and why is it required?

Bash-completion is a shell extension that enables auto-completion for commands. It is required to use kubectl auto-completion in bash on Linux and Mac.

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva