Skip to main content
Check Kubernetes Version - Advanced Use Cases with kubectl version
7 min read

Check Kubernetes Version - Advanced Use Cases with kubectl version

Introduction

Kubernetes is a container orchestration powerhouse, and kubectl is its main command-line tool for developers. Understanding your kubectl version affects Kubernetes cluster compatibility and application deployment and management. This article will discuss the kubectl version command in detail. We will cover installation, basic and advanced use, output, and common issues.

This guide will also show you how to utilize the kubectl version command in various situations. Let's get started with its installation, followed by the advanced use cases.

Getting Started with kubectl

Installation of kubectl

Make sure kubectl is installed before using it. kubectl lets you run Kubernetes cluster instructions from the command line. Behind the scenes, kubectl communicates with Kubernetes API server. This simple guide will get you started:

  1. Download kubectl: You can download the latest version of kubectl from the official Kubernetes website. This URL has thorough instructions for Windows, macOS, and Linux.
  2. Install the binary: Follow OS-specific instructions. Downloading and executing the binary is typical. Windows requires installing the.exe file and adding its installation location to the environment variables. Use a package manager to install it and then move it to a directory in your PATH, here is an example: sudo mv /usr/local/bin/
  3. Verify the installation: After installation, run kubectl version --client in your terminal to check. This command shows the installed kubectl version.
    Verifying Kubectl installation

How to use the kubectl version command

Using kubectl version is pretty simple. This command checks the version of kubectl and the Kubernetes server it connects to. Here's how you use it:

  • Open your terminal.
  • Type kubectl version and enter. It returns client (kubectl) and server (Kubernetes cluster) information.
    Checking kubectl version

Basic usage of kubectl version

Understanding the output: client and server versions

The kubectl version command output has two primary parts:

  1. Client version: This shows the version of the kubectl client utility on your machine. Version number, build date, git commit, platform, and more are included. Cloud vendors prepare kubectl for you if you connect to cloud-hosted Kubernetes via their web-based cloud shell. The GCP console below shows the kubectl client tool provisioned in the same browser.

    Cloud based shell access
  2. Server version: When connected to a Kubernetes cluster, kubectl will display the server version. Similar details as the client version, but for the server. If no server version is displayed, the kubectl client has been installed but not connected to a Kubernetes server.

Understanding both versions is critical because client-server compatibility affects cluster resource management. We'll explore the version information provided by kubectl in this section. Developers need to understand these components for Kubernetes troubleshooting and compatibility.

Detailed breakdown of version information

Explanation of version components

To see the details of the version, just run the command kubectl version --output=yaml

Details of kubectl version

Major: This number denotes kubectl's major version. big version changes frequently require big updates and may be incompatible.

Minor: Minor version numbers indicate smaller, more frequent upgrades. Updates usually add functionality while maintaining compatibility with the major version.

GitVersion: This field lists the major and minor versions of kubectl, as well as pre-release IDs like alpha, beta, etc.

GitCommit:Every kubectl version has a source code repository commit. This Git commit hash shows which code modifications are in your version.

GitTreeState: Indicates whether the kubectl binary's source tree was clean or modified. Clean means the binary was built without uncommitted changes.

BuildDate: The kubectl binary was built at this time. It shows how new your version is.

GoVersion: Kubectl is written in Go, hence this shows its GoVersion. It helps determine compatibility and performance.

Compiler:This shows the compiler used to build kubectl. Go compiler gc is typical.

Platform: The kubectl binary's operating system and architecture, such as linux/amd64 or windows/amd64.

Common scenarios and troubleshooting

Scenario 1: Checking compatibility between client and server versions

When working with Kubernetes, make sure that your kubectl client version is compatible with the server's version. Here are few tips regarding backward compatibility of client and server versions.

• Newer clients typically work with older servers. Using a newer kubectl client allows the management of older Kubernetes clusters.

• Some new client features may not work on older servers. Warnings or errors may occur when using features with an incompatible server.

• Older clients may not work with newer servers. Older clients may not understand newer server features or APIs, causing errors or unexpected behavior.

• For the latest features, bug fixes, and security improvements, keep your client and server versions updated. Knowing your older client's compatibility limitations is helpful if updating the server isn't possible.

Note that the official documentation states that within one minor version (older or newer), the client and server versions are generally considered compatible. For example, a v1.28 kubectl client is compatible with server versions v1.27, v1.28, and v1.29. Differences in the patch number (the x in 1.26.x) are usually not a concern for compatibility as they typically involve bug fixes and security patches.

Scenario 2: Debugging common errors

Although the `kubectl version is a very basic command, however, you might still face some errors like connection issues, version mismatches, or configuration problems. Here are some steps to debug these common errors:

Steps to debug:

  1. Check connectivity: If you can't connect to the Kubernetes server, check your internet and/or network stability. Sometimes firewalls cause problems too.

Example of a network connectivity issue is shown below.

network connectivity issue
  1. Version mismatch: For version-related problems, compare client and server versions as in Scenario 1. The warning of a version mismatch is highlighted in red below.

    Version mismatch
  2. Configuration check: You need to make sure that your kubectl is configured correctly. Use the command kubectl config view to check your current configuration. This command can help you track down different issues like:

  • Incorrect current context
  • Authentication failures
  • Misconfigured namespaces
  • Certificate problems
  • User and role misconfigurations

As you can see in the screenshot below, the command kubectl config view provides a lot of details, including the server clusters, context, namespace, etc.

kubectl config view

Advanced usage of kubectl version

Using kubectl version with different output formats (JSON, YAML)

You can use kubectl to display the output in JSON and YAML; these formats are ideal for scripting and automation because they can be parsed and processed programmatically. Let's try both.

JSON format

To get the version details in JSON format, use the command: kubectl version -o json

This command will display detailed version information for both the client and the server (if connected to a Kubernetes cluster) in JSON format. See the screenshot below.

kubectl version json

YAML format

If you prefer YAML, which is often used in Kubernetes configurations, you can use the command:

kubectl version -o yaml

This will output the version information in YAML format, another structured and easy-to-read format that's widely used in Kubernetes environments. See the below screenshot for reference.

kubectl version yaml

Checking version without a Kubernetes cluster (offline mode)

Checking the version of kubectl without access to a Kubernetes cluster may be necessary at time. This helps in installations and troubleshooting.

To verify kubectl version without connecting to a cluster, use the --client flag:

kubectl version --client

This command only displays the kubectl client version, requiring no active Kubernetes cluster connection. An example screenshot below:

kubectl version client only

Conclusion

Kubernetes developers use the kubectl version command not just to check the version but to ensure client-server compatibility, troubleshoot, and streamline deployment as well. This command is a valuable tool for identifying any issues in your Kubernetes configuration and improving productivity.

You can use this command for installation preparation, cluster management, and environment compliance. In this article, we have gone through all the use cases that developers face during their day-to-day Kubernetes management. Kubectl version is an under-utilized command that needs more attention, and developers need to keep it in hand to utilize it effectively.

Related Articles

Using kubectl logs - DevOps Guide

Kubernetes pod logs show container operations and events in real-time. Containers in Kubernetes pods log their stdout and stderr streams. Debugging and monitoring Kubernetes cluster applications requires these logs.

How to deploy Postgres on Kubernetes

This article will serve as a guide for installing PostgreSQL on Kubernetes, covering pre-requisites such as setting up Kubectl, Kubernetes clusters, and Docker.

kubectl port-forward - Kubernetes Port Forwarding Explained

We'll see how to use kubectl port-forward to access internal Kubernetes services from outside the cluster.