Kubectl Install Cheat Sheet

Wherein I detail the process I follow to install kubectl, kubectx and kubens, plus autocompletion for all 3, in my WSL instance.

kubectl

Download kubectl with a curl command, make it executable, then move it to /usr/local/bin:

$ curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   154  100   154    0     0    649      0 --:--:-- --:--:-- --:--:--   649
100 44.4M  100 44.4M    0     0  12.2M      0  0:00:03  0:00:03 --:--:-- 14.8M
$ ls
kubectl
$ chmod +x ./kubectl
$ sudo mv ./kubectl /usr/local/bin/kubectl
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.4", GitCommit:"e6c093d87ea4cbb530a7b2ae91e54c0842d8308a", GitTreeState:"clean", BuildDate:"2022-02-16T12:38:05Z", GoVersion:"go1.17.7", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.4", GitCommit:"e6c093d87ea4cbb530a7b2ae91e54c0842d8308a", GitTreeState:"clean", BuildDate:"2022-02-16T12:32:02Z", GoVersion:"go1.17.7", Compiler:"gc", Platform:"linux/amd64"}

Enable kubectl autocompletion by appending a line to .bashrc:

$ echo 'source <(kubectl completion bash)' >>~/.bashrc

Add an alias for kubectl and enable autocompletion for that alias too:

$ echo 'alias k=kubectl' >>~/.bashrc
$ echo 'complete -F __start_kubectl k' >>~/.bashrc

And set kubectl edit to use VS Code:

$ echo export KUBE_EDITOR=\"code --wait\" >> ~/.bash_env

kubectx and kubens

Clone the git repository and link the binaries with git and ln:

$ sudo git clone https://github.com/ahmetb/kubectx /opt/kubectx
Cloning into '/opt/kubectx'...
remote: Enumerating objects: 1457, done.
remote: Counting objects: 100% (172/172), done.
remote: Compressing objects: 100% (115/115), done.
remote: Total 1457 (delta 85), reused 97 (delta 51), pack-reused 1285
Receiving objects: 100% (1457/1457), 905.30 KiB | 2.43 MiB/s, done.
Resolving deltas: 100% (817/817), done.
$ sudo ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx
$ sudo ln -s /opt/kubectx/kubens /usr/local/bin/kubens

Enable autocompletion by linking the autocomplete files:

$ sudo ln -sf ~/.kubectx/completion/kubens.bash /usr/share/bash-completion/completions/kubens
$ sudo ln -sf ~/.kubectx/completion/kubectx.bash /usr/share/bash-completion/completions/kubectx

Summary

Paste the following commands into a WSL terminal to install kubectl, add an alias for it, and enable autocomplete:

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl

echo 'source <(kubectl completion bash)' >>~/.bashrc
echo 'alias k=kubectl' >>~/.bashrc
echo 'complete -F __start_kubectl k' >>~/.bashrc

echo export KUBE_EDITOR=\"code --wait\" >> ~/.bash_env

sudo git clone https://github.com/ahmetb/kubectx /opt/kubectx
sudo ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx
sudo ln -s /opt/kubectx/kubens /usr/local/bin/kubens

sudo ln -sf /opt/kubectx/completion/kubens.bash /usr/share/bash-completion/completions/kubens
sudo ln -sf /opt/kubectx/completion/kubectx.bash /usr/share/bash-completion/completions/kubectx

References

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *