Ben
Ben

Reputation: 5087

Specify kubectl client version installed via gcloud SDK

I probably missed this in the docs somewhere, but since I haven't found it yet, I'll ask: How can I specify the version of kubectl CLI when installing with the gcloud SDK?

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"13+", GitVersion:"v1.13.9-2+4a03651a7e7e04", GitCommit:"4a03651a7e7e04a0021b2ef087963dfb7bd0a17e", GitTreeState:"clean", BuildDate:"2019-08-16T19:08:17Z", GoVersion:"go1.11.5", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"13+", GitVersion:"v1.13.7-gke.24", GitCommit:"2ce02ef1754a457ba464ab87dba9090d90cf0468", GitTreeState:"clean", BuildDate:"2019-08-12T22:05:28Z", GoVersion:"go1.11.5b4", Compiler:"gc", Platform:"linux/amd64"}
$ gcloud components update

All components are up to date.
$ which kubectl
/Users/me/Projects/googlecloud/google-cloud-sdk/bin/kubectl
$ which gcloud
/Users/me/Projects/googlecloud/google-cloud-sdk/bin/gcloud
$ ls -nL /Users/me/Projects/googlecloud/google-cloud-sdk/bin | grep kubectl
-rwxr-xr-x   1 501  20  44296840 Aug 16 12:08 kubectl
-rwxr-xr-x   1 501  20  54985744 Apr 30 21:56 kubectl.1.11
-rwxr-xr-x   1 501  20  56860112 Jul  7 21:34 kubectl.1.12
-rwxr-xr-x   1 501  20  44329928 Aug  5 02:52 kubectl.1.13
-rwxr-xr-x   1 501  20  48698616 Aug  5 02:55 kubectl.1.14
-rwxr-xr-x   1 501  20  48591440 Aug  5 02:57 kubectl.1.15

So I'm using the gcloud-installed kubectl, and I see that the version I want is locally installed. The gcloud components update command run previously indicated that kubectl would be set to the default version of 1.13, but I haven't caught any indication of how to change the default version.

I imagine I could create a link, or copy the version I want onto Users/me/Projects/googlecloud/google-cloud-sdk/bin/kubectl, but I'm leary of messing with the managed environs of gcloud.

Upvotes: 4

Views: 4035

Answers (2)

German Lashevich
German Lashevich

Reputation: 2433

GKE only

Do not change kubectl version if you're working only with GKE, because kubectl supports only one version forward and backward skew. For example, if you use kubectl 1.16 against GKE 1.14, you may experience some bugs, such as --watch flag is not working properly. gcloud provides just the right version for the current version of GKE.

Multiple cluster versions

explicit version

If you're working with different Kubernetes cluster, I'd suggest using the gcloud version of kubectl as a default one. For any specific version of kubectl, just create a dir ~/bin/kubectl, put there kubectl1.15, kubectl1.16, etc. and add the dir to your PATH.
With such setup you can explicitly use appropriate version:

$ # Working with GKE
$ kubectl ...
$ # Working with K8s 1.15
$ kubectl1.15 ...

implicit version

Using direnv you can make switching between versions transparent.
There are many ways of doing this, here is on example.
Let's say you have a project which requires kubectl 1.15. Inside the project dir create env/bin subdir and link there all binaries you need (kubectl1.15, helm2, etc.), create .envrc file with the following content:

export PATH="$(PWD)/env/bin:${PATH}"

Run direnv allow in the project dir (it's needed only once for any new .envrc). After that you'll have all binaries from env/bin in your path.
And then, in the dir and all subdirs:

$ # Invokes kubectl 1.15
$ kubectl ...
$ # Invokes Helm 2
$ helm ...

Upvotes: 2

Ben
Ben

Reputation: 5087

Whelp, I went ahead and ran the following

KUBE_BIN=$(which kubectl)
rm $KUBE_BIN
ln ~/googlecloud/google-cloud-sdk/bin/kubectl.1.15 $KUBE_BIN

and now i get

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.2", GitCommit:"f6278300bebbb750328ac16ee6dd3aa7d3549568", GitTreeState:"clean", BuildDate:"2019-08-05T09:23:26Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"darwin/amd64"}

and everything seems to be working just fine...

Upvotes: 4

Related Questions