As stated in the title, is it possible to find out a K8s cluster name from the API? I looked around the API and could not find it.
11 Answers
kubectl config current-context
does the trick (it outputs little bit more, like project name, region, etc., but it should give you the answer you need).
![](../../users/profiles/1391392.webp)
- 1,860
- 2
- 16
- 22
-
9The context name can be renamed manually and therefore unreliable. – Akihiro HARAI Jun 12 '19 at 02:00
-
1@AkihiroHARAI yes you can rename it manually. But: a) why would you do that? b) running `gcloud container clusters get-credentials` sets the context back. – usamec Jun 16 '19 at 20:32
-
5I wonder why this answer has so many upvotes. `config current-context` doesn't do any k8s API calls, it just reads locally stored `kubectl` config (which is not even, strictly speaking, a part of kubernetes, just a client for its apis). – edio Oct 12 '19 at 14:31
-
@edio because question has tag "google-kubernetes-engine" and this solves problem of finding which cluster you are connected to on GKE – usamec Oct 17 '19 at 12:12
-
2@usamec, the question was about kubernetes **API**. Executing kubectl, which will likely won't be even installed in a POD with a running application is very far from kubernetes API. By this logic I could also suggest to the topic starter to open a browser, navigate to google console and check cluster name in GKE UI. – edio Oct 18 '19 at 13:13
I dont believe there is a k8s cluster name. This command could provide some nice informations
kubectl cluster-info
![](../../users/profiles/6406639.webp)
- 569
- 2
- 10
-
I'm sorry I downvoted for `kubectl cluster-info` not returning a name. Here, take the bounty instead. See http://stackoverflow.com/a/43667827/246241 for the kubernetes ticket. – tback May 03 '17 at 07:20
Unfortunately a cluster doesn't know its own name, or anything else that would uniquely identify it (K8s issue #44954). I wanted to know for helm issue #2055.
![](../../users/profiles/1450.webp)
- 107,187
- 45
- 188
- 235
![](../../users/profiles/246241.webp)
- 9,418
- 5
- 38
- 67
The question is not really well described. However, if this question is related to Google Container Engine
then as coreypobrien mentioned the name of cluster is stored in custom metadata of nodes. From inside a node, run the following command and the output will be name of cluster:
curl http://metadata/computeMetadata/v1/instance/attributes/cluster-name -H "Metadata-Flavor: Google"
If you specify your use case, I might be able to extend my answer to cover it.
![](../../users/profiles/3895774.webp)
- 3,047
- 23
- 40
-
2
-
1
-
How do you make this call using the `k8s.io/client-go`? Tried to use the `RESTClient()` unsuccessfully – supercalifragilistichespirali Feb 27 '20 at 09:28
-
Hi, where can I find the documentation for this? I can find the metadata documentation, but nothing specific about GKE and "cluster-name" for example. Can I also get the project name? – Sam Mar 23 '20 at 12:51
The kubernetes API doesn't know much about the GKE cluster name, but you can easily get the cluster name from the Google metatdata server like this
kubectl run curl --rm --restart=Never -it --image=appropriate/curl -- -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/attributes/cluster-name
![](../../users/profiles/540766.webp)
- 1,561
- 14
- 16
-
This is the answer I have been looking for all day; I needed to see the full list of instance attributes available. `kubectl run curl --rm --restart=Never -it --image=appropriate/curl -- -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/attributes/` – durp Oct 01 '18 at 21:29
-
how can I avoid having kubectl print "pod curl deleted" at the end? – Alex Flint Dec 18 '18 at 20:26
-
1Add `--quiet` after `run`. The entire command would be `kubectl run --quiet curl --rm --restart=Never -it --image=appropriate/curl -- -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/attributes/cluster-name > cluster_name`. @AlexFlint – Akihiro HARAI Jun 12 '19 at 02:04
In case the name you have in your .kube/config file is enough for you, this one-liner does the trick:
kubectl config view --minify -o jsonpath='{.clusters[].name}'
Note 1: The --minify
is key here so it will output the name of your current context only. There are other similar answers posted here but without the "minify" you will be listing other contexts in your config that might confuse you.
Note 2: The name in your .kube/config might not reflect the name in your cloud provider, if the file was autogenerated by the cloud provider the name should match, if you configured it manually you could have typed any name just for local config.
Note 3: Do not rely on kubectl config current-context
this returns just the name of the context, not the name of the cluster.
![](../../users/profiles/607318.webp)
- 491
- 6
- 14
It is the same as getting the current config, but the below command gives clear output:
kubectl config view
![](../../users/profiles/3345375.webp)
- 9,037
- 14
- 52
- 75
![](../../users/profiles/7848611.webp)
- 121
- 1
- 5
-
1This was useful for me! I ran `kubectl config view -o json`. I was then able to determine my "current-cluster" by using the current context and mapping that to the contexts to get the current cluster name. Thanks! – Rob McCabe Jul 21 '19 at 01:13
This command will Check all possible clusters, as you know .KUBECONFIG may have multiple contexts
kubectl config view -o jsonpath='{"Cluster name\tServer\n"}{range .clusters[*]}{.name}{"\t"}{.cluster.server}{"\n"}{end}'
And you will get output like
Cluster name Server
kubernetes https://localhost:6443
![](../../users/profiles/10345160.webp)
- 740
- 5
- 16
Well this returns precisely one thing, a cluster name
K8s:
kubectl config view -o jsonpath='{.clusters[].name}{"\n"}'
Openshift:
oc config view -o jsonpath='{.clusters[].name}{"\n"}'
-
This is incorrect! it just return the FIRST cluster name. This may be diffrent than the cluster of the curent context. – Polymerase Apr 02 '21 at 15:56
For clusters that were installed using kubeadm
, the configuration stored in the kubeadm-config
configmap has the cluster name used when installing the cluster.
$ kubectl -n kube-system get configmap kubeadm-config -o yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: kubeadm-config
namespace: kube-system
data:
ClusterConfiguration: |
clusterName: NAME_OF_CLUSTER
For clusters that are using CoreDNS for their DNS, the "cluster name" from kubeadm is also used as the domain suffix.
$ kubectl -n kube-system get configmap coredns -o yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
data:
Corefile: |
.:53 {
kubernetes NAME_OF_CLUSTER.local in-addr.arpa ip6.arpa {
![](../../users/profiles/11414.webp)
- 5,247
- 3
- 49
- 58