I work with several clusters of kubernetes and each of it is of a specific version, therefore I need to have different kubectl. To manage this what I do is access https://github.com/kubernetes/kubernetes/releases to identify the versions of kubectl and I under the latest git version of each major release.minor with the command (example to download version 1.20.2):
curl -LO "https://storage.googleapis.com/kubernetes-release/release/v1.20.2/bin/linux/amd64/kubectl"
I then copy each version to a directory structure like the following in /opt/kubectl, all this as root or with the corresponding sudo:
/opt/kubectl/kubectl-v0.21.4/kubectl
/opt/kubectl/kubectl-v1.0.6/kubectl
...
/opt/kubectl/kubectl-v1.20.2/kubectl
...
/opt/kubectl/kubectl-v1.8.15/kubectl
/opt/kubectl/kubectl-v1.9.11/kubectl
A couple of images:
To each kubectl I get down and leave it in its corresponding version folder I give +x permissions with:
chmod +x kubectl
And with update-alternatives I manage the different versions with a run like the following to define it, running as many commands as this one as versions have installed:
sudo update-alternatives --install "/usr/local/bin/kubectl" "kubectl" "/opt/kubectl/kubectl-v1.13.12/kubectl" 15
This installation says that option 15 for config "kubectl" points on kubectl link in "/usr/local/bin", which is an executable path of users, to the kubectl executable version 1.13.12 located at /opt/kubectl/kubectl-v1.13.12.
When I have all the kubectl vessions defined in update-alternatives for "config" kubectl I can review it by running:
sudo update-alternatives --list kubectl
it returns in my case:
And to select which version of kubectl I want to activate on the operating system I run the following command:
sudo update-alternatives --config kubectl
In the image we see that option "0" is specified that corresponds to the last version added (with the -install option specified above) to the config "kubectl" because the config "kubectl" is configured in auto mode which is as configured by default if nothing is specified about it. We see in the previous command executed "kubectl version" that the OS currently references version 1.20.2 We can change it to version 1.18.15 by typing the value 12 and pressing Enter:
operatorfeitam@dev03:/opt/kubectl$ sudo update-alternatives --config kubectl
There are 22 choices for the alternative kubectl (providing /usr/local/bin/kubectl).
Selection Path Priority Status
------------------------------------------------------------
* 0 /opt/kubectl/kubectl-v1.20.2/kubectl 22 auto mode
1 /opt/kubectl/kubectl-v0.21.4/kubectl 1 manual mode
2 /opt/kubectl/kubectl-v1.0.6/kubectl 2 manual mode
3 /opt/kubectl/kubectl-v1.1.8/kubectl 3 manual mode
4 /opt/kubectl/kubectl-v1.10.12/kubectl 12 manual mode
5 /opt/kubectl/kubectl-v1.11.10/kubectl 13 manual mode
6 /opt/kubectl/kubectl-v1.12.10/kubectl 14 manual mode
7 /opt/kubectl/kubectl-v1.13.12/kubectl 15 manual mode
8 /opt/kubectl/kubectl-v1.14.10/kubectl 16 manual mode
9 /opt/kubectl/kubectl-v1.15.12/kubectl 17 manual mode
10 /opt/kubectl/kubectl-v1.16.15/kubectl 18 manual mode
11 /opt/kubectl/kubectl-v1.17.17/kubectl 19 manual mode
12 /opt/kubectl/kubectl-v1.18.15/kubectl 20 manual mode
13 /opt/kubectl/kubectl-v1.19.7/kubectl 21 manual mode
14 /opt/kubectl/kubectl-v1.2.7/kubectl 4 manual mode
15 /opt/kubectl/kubectl-v1.20.2/kubectl 22 manual mode
16 /opt/kubectl/kubectl-v1.3.10/kubectl 5 manual mode
17 /opt/kubectl/kubectl-v1.4.9/kubectl 6 manual mode
18 /opt/kubectl/kubectl-v1.5.8/kubectl 7 manual mode
19 /opt/kubectl/kubectl-v1.6.16/kubectl 8 manual mode
20 /opt/kubectl/kubectl-v1.7.16/kubectl 9 manual mode
21 /opt/kubectl/kubectl-v1.8.15/kubectl 10 manual mode
22 /opt/kubectl/kubectl-v1.9.11/kubectl 11 manual mode
Press <enter> to keep the current choice[*], or type selection number: 12
update-alternatives: using /opt/kubectl/kubectl-v1.18.15/kubectl to provide /usr/local/bin/kubectl (kubectl) in manual mode
operatorfeitam@dev03:/opt/kubectl$
Running the following command again to verify that the kubectl in use has changed:
kubectl version
We see that the operating system now has version 1.18.15 in use:
operatorfeitam@dev03:/opt/kubectl$ kubectl version
Client Version: version.Info{Major:"1", Minor:"8", GitVersion:"v1.8.15", GitCommit:"c2bd642c70b3629223ea3b7db566a267a1e2d0df", GitTreeState:"clean", BuildDate:"2018-07-11T17:59:56Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"linux/amd64"}
The connection to the server localhost:8080 was refused - did you specify the right host or port?
This on operating systems other than ubuntu, such as CentOS, is done homologously with alternatives, rather than update-alternatives.