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.
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}'
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 HereWhat’s going on here?
If we run the following command:
kubectl config get-contexts
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.
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.
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 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.
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.
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.
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.
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:
- Tutorial: Learn how to quickly install the Metrics Server in minikube in five easy steps!
- Learn how to mount a local directory in minikube by exploring three possible solutions.
- Learn how to run Microsoft SQL Server on Mac OSX with Docker.
- Learn how to Setup SSM on EC2 in this tutorial.
Questions and comments are welcomed.
Frequently Asked Questions (FAQ)
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.











