K8senv to manage multiple versions of K8s’ clients (kubectl/velero/helm)

Navratan Lal Gupta
Linux Shots
Published in
5 min readJun 3, 2023

How do you manage numbers of Kubernetes clusters from same jump box ? What if clusters running are on different versions ? What if clusters have different versions of velero (A popular backup and restore solution for K8s) is installed across those clusters ?

We must follow version-skew policy of Kubernetes. One of them is, Version-skew between Kubernetes cluster and kubectl must be within one minor version. So if we want to manage Kubernetes cluster version 1.26.1, We must use kubectl version between 1.25 to 1.27. Similarly, We must use supported clients like velero and helm.

But many times we manage multiple Kubernetes clusters which are running on different version using same version of Kubernetes client like kubectl because we have only one version of kubectl installed on our jumphost.

To solve this, We can use a lightweight and easy to use tool named k8senv. This tool can be used to install multiple version of Kubernetes clients like kubectl, helm and velero on same jumphost. This same tool can also be used to manage the installed versions of Kubernetes client.

How to install K8senv

To install k8senv follow below steps

  1. Create a directory .k8senv/bin in home directory
mkdir -p $HOME/.k8senv/bin

2. Download k8senv utility inside .k8senv/bin directory

cd $HOME/.k8senv/bin
curl -L https://github.com/navilg/k8senv/releases/latest/download/k8senv-linux-amd64 -o k8senv
chmod +x k8senv

To download utility for ARM64 CPU,

cd ~/.k8senv/bin
curl -L https://github.com/navilg/k8senv/releases/latest/download/k8senv-linux-arm64 -o k8senv
chmod +x k8senv

3. Add $HOME/.k8senv/bin directory in PATH environment variable. Make sure to keep this directory first in order.

export PATH="$HOME/.k8senv/bin:$PATH"
echo 'export PATH="$HOME/.k8senv/bin:$PATH"' >> ~/.bash_profile
hash -r
which k8senv
# OR
type k8senv
Installation of k8senv

How to use ?

You can watch the demo video here.

If you are not a “Watch a video” person, continue reading below…

All k8senv commands would be in below structure

k8senv [VERB] [CLIENT] [VERSION]

# [VERB] can be install, use, list, remove or unuse
# [CLIENT] can be kubectl, velero or helm
# [VERSION] can be any client's version in format v*.*.*

Install multiple versions of kubectl

k8senv install kubectl v1.26.1 # OR,
k8senv kubectl install 1.25.1

Above commands will install kubectl version 1.26.1 and 1.25.1 on local system. This installation will not be system-wide.

Install multiple versions of kubevtl

List all the versions of kubectl

When we install multiple versions of kubectl, we will need visibility on what we have installed. We can list all the versions of kubectl installed by k8senv using list command.

k8senv list kubectl # OR,
k8senv kubectl list
List all installed version of kubectl

Switch to a version of kubectl

Once a version of kubectl is installed. We can switch to any of installed version of kubectl to use it. If the version of kubectl is not installed, It will automatically install it when switching.

k8senv use kubectl v1.25.1 # OR,
k8senv kubectl use 1.26.1

If you were using system-installed kubectl in same SHELL session, then you might need to clear the BASH hash table to use k8senv installed kubectl. To do that we can run below command. This will clear the cached path of kubectl from hash table.

hash -d kubectl
# Ignore any error if you get
Switch kubectl versions

When you list all kubectl versions after switching. You can see currently used version of kubectl marked with asterisk (*).

Uninstall/Remove a version of kubectl

When a version of kubectl is not needed anymore, They can be removed from k8senv using below commands

k8senv remove kubectl v1.26.1 # OR,
k8senv kubectl remove 1.25.1
Remove a version of kubectl

Switch to system-installed version of kubectl

Sometimes you might need to switch to system-installed version of kubectl instead of using k8senv installed one. To switch to system-installed version of kubectl, We can use below commands.

k8senv unuse kubectl # OR,
k8senv kubectl unuse

Along with above command, We may also need to clear the cached path of kubectl from hash table using below command.

hash -d kubectl
Unuse k8senv installed kubectl

What about other Kubernetes clients ?

As of now k8senv support kubectl, helm and velero which are widely used Kubernetes clients. All the above commands can be used with these clients by replacing kubectl with helm or velero. See screenshot below

Similar commands with velero client

How to remove k8senv

When you start a machine, you must know how to stop it. In case decide to remove k8senv from your system. You can run below command. Don’t forget to leave any feedback or star on the Github link here.

  1. Delete $HOME/.k8senv directory
rm -rf $HOME/.k8senv
hash -r

2. Remove $HOME/.k8senv/bin directory from PATH environment variable

# Open $HOME/.bash_profile file
vi $HOME/.bash_profile

# Look for line export PATH="$HOME/.k8senv/bin:$PATH"
# Remove that line. Move the cursor on that line and <ESC>dd
# Save and exit <ESC>:wq<ENTER>

I hope this article helps you manage your Kubernetes cluster more easily and efficiently. If it helped, Don’t forget to clap and leave a star on its repository on my GitHub page here.

You may contribute to this project by raising any issue or contributing any feature on GitHub: https://github.com/navilg/k8senv

You can support my work by buying me a cup of coffee on https://www.buymeacoffee.com/linuxshots

Thanks

Navratan Lal Gupta

Linux Shots

--

--

Navratan Lal Gupta
Linux Shots

I talk about Linux, DevOps, Kubernetes, Docker, opensource and Cloud technology. Don't forget to follow me and my publication linuxshots.