How to Enable Kubectl Bash Completion

How to Enable Kubectl Bash Completion

Kubectl is a command-line tool that is used to interact with Kubernetes clusters. It enables users to manage applications that run on Kubernetes. Bash completion is a feature that allows the shell to automatically complete commands, options, and arguments. Enabling kubectl bash completion can save you a lot of time and effort when working with Kubernetes clusters. In this article, we will discuss how to enable kubectl bash completion in a step-by-step manner.

Table of Contents

  1. Checking kubectl Version

  2. Installing Bash Completion

  3. Configuring Bash Completion

  4. Verifying Bash Completion

  5. More Examples

Checking kubectl Version:

Before we proceed, we need to check the version of kubectl installed on our system. Open a terminal and type the following command:
kubectl version --client

This will display the version of kubectl installed on your system.

Installing Bash Completion:

If you are using a Linux distribution that supports apt, you can install bash completion using the following command:
sudo apt-get install bash-completion

If you are using a Linux distribution that supports yum, you can install bash completion using the following command:

sudo yum install bash-completion

If you are using macOS, you can install bash completion using Homebrew:

brew install bash-completion

Configuring Bash Completion:

Once you have installed bash completion, you need to configure it to work with kubectl. Open your terminal and run the following command to edit the bashrc file:
nano ~/.bashrc

Add the following lines to the end of the file:

source <(kubectl completion bash)
alias k=kubectl
complete -F __start_kubectl k

Save the file and exit the editor.

Verifying Bash Completion:

To verify that bash completion is working, open a new terminal and type the following command:
k run [TAB]

This will display a list of available options for the kubectl run command.

More Examples:

Here are some more examples of how to use bash completion with kubectl:
  • k get [TAB]: This will display a list of available Kubernetes resources.
  • k get pods [TAB]: This will display a list of available pods.
  • k logs [TAB]: This will display a list of available pods to view their logs.
  • k delete [TAB]: This will display a list of available resources that can be deleted.

By following these steps, you can enable kubectl bash completion and save time and effort when working with Kubernetes clusters.

Related Searches and Questions asked:

  • How to Delete Nodes from Kubernetes
  • How to Delete Pods Forcefully on Kubernetes
  • How to Use Kubectl Exec Commands
  • How to Install Kubectl on Windows
  • That's it for this post. Keep practicing and have fun. Leave your comments if any.

    Post a Comment

    0 Comments