Tutorial: Learn how to get the cluster name in Kubernetes now! 🤔

In this tutorial we’ll review how to get the cluster name in Kubernetes (k8s) using the kubectl config view command with a filter for the context.cluster value.

Kubectl Get Cluster Name TOC

We’ll also provide a detailed explanation regarding why this command is written as it is since it’s rather verbose.

An important aspect of k8s management involves identifying the cluster name — the cluster name is essential for tasks such as configuring contexts, deploying applications, and monitoring cluster health.

This guide provides a step-by-step approach to obtaining the Kubernetes cluster name and should help to improve your proficiency in Kubernetes administration.

Note that testing in this article is conducted locally using minikube.

This article was updated on June 12, 2025.

Step-by-step instructions for getting the k8s cluster name

In this section we’ll explore an alternative approach to acquiring the cluster name via kubectl.

The first step is optional however if you’re using minikube and it’s not running, you’ll need to start it.

You’ll also need to have kubectl installed as well.

Step One: Start minikube.

Start Docker Desktop, which is required to run minikube in your local environment.

Master Kubernetes Microservices Today

Once Docker Desktop is running, we can then start minikube, a single node k8s cluster.

Below I’ve included an example command that I’m using, which is specific to the setup that I have on my Apple laptop:

				
					minikube start --hyperv-virtual-switch "My Virtual Switch" --v=4
				
			

The output below includes pointers to the command as well as the indicator that it has complete successfully along with the cluster name and the default namespace.

Pointer to the command that I'm using to start minikube ("minikube start --hyperv-virtual-switch "My Virtual Switch" --v=4") and the output indicating that it has started correctly with pointers to the "minikube" cluster and "default" namespace.
minikube has started successfully!

In the next step we’ll see how to get the cluster name using the kubectl command and then we’ll break down what’s going on here and why this solution is necessary.

Step Two: Get the cluster name.

Run the following command to acquire the cluster name for the current kubeconfig context:

				
					kubectl config view -o=jsonpath='{.contexts[?(@.name == "'$(kubectl config current-context)'")].context.cluster}'
				
			

The output appears below, note that the pointer directs our attention to the k8s context, which includes the minikube cluster name.

Two pointers: 1.) The context.cluster in the "kubectl config view" command, which has a filter applied for the "context.cluster" and 2.) The output when this command is executed, which is set to "minikube", this is the cluster name.
The "kubectl config view" command with filter for the "context.cluster" which returns the current cluster name in Kubernetes.

Mock CKAD Sample Questions

You might be interested in Five Mock Kubernetes Practice Exam Questions with exercises covering topics such as how to create a pod in a Kubernetes namespace, a review regarding how to create a ConfigMap in Kubernetes, setting up Kubernetes readiness and liveness probes, and more.

See Here

What’s going on here?

If we run the following command:

				
					kubectl config get-contexts
				
			
we can see that for our current setup both the NAME, CLUSTER, and AUTHINFO are set to the same value: “minikube” — however this command is actually returning the NAME and not the CLUSTER, as we’ll prove in a moment.
Pointer to the "kubectl config get-contexts" command and output including the NAME, which is set to "minikube", and the CLUSTER, which is also set to "minikube".
The "kubectl config get-contexts" command with output.

Rename the context

Let’s rename the context from “minikube” to “maxikube” using the command below.

				
					kubectl config rename-context minikube maxikube
				
			

The pointer in the following image shows us what this command should produce when it returns successfully.

Pointer to the command "kubectl config rename-context minikube maxikube" executed successfully and the output includes "Context 'minikube' renamed to 'maxikube'.".
kubectl config rename-context minikube maxikube

When we run the following kubectl command:

				
					kubectl config current-context
				
			

we see that the current context is set to “maxikube” and hence this is not the same as the CLUSTER name.

Two red pointers that indicate that the k8s config current-context has the value "maxikube".
The k8s config current-context has the value "maxikube".

When we next execute:

				
					kubectl config get-contexts
				
			

we can see in the output in the image below where the red arrow points to the NAME, which is set to “maxikube” and the blue arrow points to the CLUSTER, which is set to “minikube” in this case — so how do we get the CLUSTER value?

The red pointer points to the minikube config context name, which is set to "maxikube" and the blue pointer points to the cluster setting, which is set to "minikube".
Red pointer: Kubernetes config context name with value "maxikube" and blue pointer: points to the cluster setting, which has the value "minikube" assigned.

The config view has the information we need and it’s structured as JSON text.

The following command can be used to get this information:

				
					kubectl config view
				
			

The result should look something like what I’ve included in the next image.

Note that the red arrow points to the context NAME and the blue arrow points to the CLUSTER — and we want the CLUSTER.

Red pointer points to the k8s cluster name, which is "minikube" and blue pointer points to the context name, which has been changed to "maxicube".
The k8s clustername is "minikube" and the context name is "maxikube".

We need to parse this JSON data using a JSONpath template and that’s where the full command demonstrated in step one comes into play, and I’ve included this again here:

				
					kubectl config view -o=jsonpath='{.contexts[?(@.name == "'$(kubectl config current-context)'")].context.cluster}'
				
			

When we execute this command it will return to us exactly the cluster name, as shown here.

Pointer to the k8s config context.cluster, which is set to "minikube" -- this is the value which represents the cluster name in Kubernetes.
The context.cluster, which is set to "minikube" -- this value represents the cluster name in Kubernetes.

There are two occurrences of “minikube” in the output however, one for the CLUSTER and the other for AUTHINFO so it’s possible (albeit unlikely) that the “minikube” value could be coming from AUTHINFO — let’s remove this possibility.

Specify a minikube profile

We can change the minikube cluster name by setting a profile when we start minikube as demonstrated here.

In this example we set the minikube profile to “maxikube-cluster” and when minikube has started successfully we can see that “maxikube-cluster” is used as the both the profile name as well as  the cluster name.

The command to start minikube is executed with the profile set to "maxikube-cluster" and we can see that this command returned successfully.
minikube is started with the profile set to the "maxikube-cluster".

I’ve included the command here:

				
					minikube start --hyperv-virtual-switch "My Virtual Switch" --v=4 --profile maxikube-cluster
				
			

The result returned from the call to “kubectl config view” with the JSONpath template filtering for the current-context includes the “maxikube-cluster” value, which is exactly what we need.

The value returned from the call to 'kubectl config view' returns 'maxikube-cluster'.
The value returned from the call to 'kubectl config view' returns 'maxikube-cluster'.

This concludes the explanation — the tutorial conclusion follows.

Tutorial Conclusion

There may be easier ways to find the cluster name using the kubectl command — if you know of one, please add a comment and let me know.

If you liked this instructional then you may also like the following articles, also written by yours truly:

Questions and comments are welcomed.

Frequently Asked Questions (FAQ)

How to get the cluster name using kubectl?

The kubectl command below can be used to return the cluster name:

kubectl config view -o=jsonpath='{.contexts[?(@.name == “‘$(kubectl config current-context)'”)].context.cluster}’

Refer to the tutorial where we explain further how you can use kubectl to get the cluster name — this includes a detailed explanation along with example images demonstrating the output using minikube.

author avatar
ThosPFuller
I am a software engineer based in Northern Virginia (USA) and this website focuses on content engineering, web development, Technical SEO, Search Engine Optimization (SEO).

Leave a Reply