Kubernetes

Enable Auto-completion for Kubectl in a Linux Bash Shell

Enable Auto-completion for Kubectl in a Linux Bash Shell

Install Kubectl on Linux

1. 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 Step 7.

kubectl autocompletion

2. 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"

3. 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"

4. Validate the kubectl binary against the checksum file:

echo "$(cat kubectl.sha256)  kubectl" | sha256sum --check

5. Install kubectl:

sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

6. Type kubectl . You should now see the tool is installed.

kubectl autocompletion - installation

Set up Auto-completion

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

9. 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

Set Up an Alias for Kubectl and Enable for Auto-completion

10. Set an alias for kubectl as k.

echo 'alias k=kubectl' >>~/.bashrc

11. Enable the alias for auto-completion.

echo 'complete -o default -F __start_kubectl k' >>~/.bashrc

12. 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

Summary

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 more about how Spacelift works with Kubernetes

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