How to check Kubernetes and Kubectl Version | Devops Junction

How to check the Kubernetes and Kubectl Version using the kubectl command line that's the objective of this article.

As you know the kubectl is a command line tool for communicating with a Kubernetes cluster's control plane, using the Kubernetes API. Provided by Kubernetes itself

if you are new to Kubectl and wondering how to install Kubectl. refer to the following links

We presume that you have kubectl installed and configured.

upon completing the installation you might want to check the version of kubectl and your Kubernetes cluster.

This same command can be used for all types of Managed and self-hosted kubernetes like

  • minikube
  • kind
  • AWS Elastic Kubernetes Service ( EKS)
  • Google Kubernetes Engine (GKE)
  • Azure Kubernetes Service (AKS)
  • Digital Ocean Kubernetes service etc

 

How to check the kubectl version and Kubernetes cluster version

To check the version of kubectl you can use the kubectl version command the same command would return the version of Kubernetes cluster too.

By default when you are issuing the kubectl version command you would see an output with the version of client and server like this

kubectl version

# kubectl version
Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.0", GitCommit:"cb303e613a121a29364f75cc67d3d580833a7479", GitTreeState:"clean", BuildDate:"2021-04-08T16:31:21Z", GoVersion:"go1.16.1", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"21+", GitVersion:"v1.21.12-eks-a64ea69", GitCommit:"d4336843ba36120e9ed1491fddff5f2fec33eb77", GitTreeState:"clean", BuildDate:"2022-05-12T18:29:27Z", GoVersion:"go1.16.15", Compiler:"gc", Platform:"linux/amd64"}

If you look at the output you can see it's returning two values

  • client version: version of the kubectl client installed on your local
  • server version: version of the Kubernetes cluster itself that you are connected to

The client and server versions can be the same or different. the client and version difference can be +/-1. beyond that would return a warning message

something like this

# kubectl version – context kind-localk8s
Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.0", GitCommit:"cb303e613a121a29364f75cc67d3d580833a7479", GitTreeState:"clean", BuildDate:"2021-04-08T16:31:21Z", GoVersion:"go1.16.1", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"24", GitVersion:"v1.24.0", GitCommit:"4ce5a8954017644c5420bae81d72b09b735c21f0", GitTreeState:"clean", BuildDate:"2022-05-19T15:39:43Z", GoVersion:"go1.18.1", Compiler:"gc", Platform:"linux/amd64"}
WARNING: version difference between client (1.21) and server (1.24) exceeds the supported minor version skew of +/-1

Now we know how to get the kubectl version and Kubernetes cluster version.

Here the combination of the Major and Minor version would be the actual version.

From the preceding output, you can say the Kubernetes version is 1.24 and the kubectl version is 1.21

Here are more examples of the kubectl version command.

 

kubectl version output as JSON

To get the output as JSON you can use the following command

# kubectl version -o json
{
"clientVersion": {
"major": "1",
"minor": "21",
"gitVersion": "v1.21.0",
"gitCommit": "cb303e613a121a29364f75cc67d3d580833a7479",
"gitTreeState": "clean",
"buildDate": "2021-04-08T16:31:21Z",
"goVersion": "go1.16.1",
"compiler": "gc",
"platform": "darwin/amd64"
},
"serverVersion": {
"major": "1",
"minor": "21+",
"gitVersion": "v1.21.12-eks-a64ea69",
"gitCommit": "d4336843ba36120e9ed1491fddff5f2fec33eb77",
"gitTreeState": "clean",
"buildDate": "2022-05-12T18:29:27Z",
"goVersion": "go1.16.15",
"compiler": "gc",
"platform": "linux/amd64"
}
}

version output as YAML

To get the output as YAML you can use the following command

# kubectl version -o yaml
clientVersion:
buildDate: "2021-04-08T16:31:21Z"
compiler: gc
gitCommit: cb303e613a121a29364f75cc67d3d580833a7479
gitTreeState: clean
gitVersion: v1.21.0
goVersion: go1.16.1
major: "1"
minor: "21"
platform: darwin/amd64
serverVersion:
buildDate: "2022-05-12T18:29:27Z"
compiler: gc
gitCommit: d4336843ba36120e9ed1491fddff5f2fec33eb77
gitTreeState: clean
gitVersion: v1.21.12-eks-a64ea69
goVersion: go1.16.15
major: "1"
minor: 21+
platform: linux/amd64

 

Client Version only with Kubectl

Get only the client version of kubectl, you can use JQ along with JSON like this

# kubectl version -o json | jq .clientVersion
{
"major": "1",
"minor": "21",
"gitVersion": "v1.21.0",
"gitCommit": "cb303e613a121a29364f75cc67d3d580833a7479",
"gitTreeState": "clean",
"buildDate": "2021-04-08T16:31:21Z",
"goVersion": "go1.16.1",
"compiler": "gc",
"platform": "darwin/amd64"

or you can simply use the --client option which is inbuilt

kubectl version – client -o json
{
"clientVersion": {
"major": "1",
"minor": "21",
"gitVersion": "v1.21.0",
"gitCommit": "cb303e613a121a29364f75cc67d3d580833a7479",
"gitTreeState": "clean",
"buildDate": "2021-04-08T16:31:21Z",
"goVersion": "go1.16.1",
"compiler": "gc",
"platform": "darwin/amd64"
}
}

 

Get only Kubernetes Cluster Version

to get only the Kubernetes server(cluster)  version using kubectl, you can use the following JQ and JSON combo

# kubectl version -o json | jq .serverVersion
{
"major": "1",
"minor": "21+",
"gitVersion": "v1.21.12-eks-a64ea69",
"gitCommit": "d4336843ba36120e9ed1491fddff5f2fec33eb77",
"gitTreeState": "clean",
"buildDate": "2022-05-12T18:29:27Z",
"goVersion": "go1.16.15",
"compiler": "gc",
"platform": "linux/amd64"

 

Sometimes you might just want the major and minor numbers combined for any automation or monitoring.

Here is the command to get that

# kubectl version -o json | jq -rj '.serverVersion|.major,".",.minor'
1.21

 

Hope this short article helps. If you have any questions reach out to us

Refer to our other Kubernetes articles here

If you are looking for any DevOps support for you or for your organization talk to us at [email protected] and visit https://gritfy.com

 

Cheers
Sarav AK

Follow me on Linkedin My Profile
Follow DevopsJunction onFacebook orTwitter
For more practical videos and tutorials. Subscribe to our channel

Buy Me a Coffee at ko-fi.com

Signup for Exclusive "Subscriber-only" Content

Loading