如何从 K8s API 获得 Kubernetes 集群名称

如标题所述,是否有可能从 API 中找到一个 K8s 集群名称?我在 API 周围找了一圈,但是没有找到。

94150 次浏览

I dont believe there is a k8s cluster name. This command could provide some nice informations

kubectl cluster-info

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

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.

Update: A common workaround is to create a ConfigMap containing the cluster name and read that when required (#2055 comment 1244537799).

apiVersion: v1
kind: ConfigMap
metadata:
name: cluster-info
namespace: kube-system
data:
cluster-name: foo

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.

$ kubectl config get-clusters --> get you the list of existing clusters

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).

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 {

It is the same as getting the current config, but the below command gives clear output:

kubectl config view

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

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"}'

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.

Using python k8s client. But this won't work with incluster_kubeconfig.

from kubernetes import config


cluster_context = config.kube_config.list_kube_config_contexts()
print (cluster_context)
([{'context': {'cluster': 'k01.test.use1.aws.platform.gov', 'user': 'k01-test'}, 'name': 'k01.test.use1.aws.platform.gov'}], {'context': {'cluster': 'k01.test.use1.aws.platform.gov', 'user': 'k01-test'}, 'name': 'k01.test.use1.aws.platform.gov'})


cluster_name = cluster_context[1]['context']['cluster']
print (cluster_name)
k01.test.use1.aws.platform.gov

at-least for kubespray clusters, the following works for me

kubectl config current-context | cut -d '@' -f2

Using kubectl command:

$ kubectl config get-clusters
NAME
kubernetes
kubectl config get-clusters
    

kubectl config get-contexts

There is a great tool called kubectx https://github.com/ahmetb/kubectx.

Listing all previously added clusters is as easy as writing kubectx. This command will also show currently used cluster/its name.

Switching to a cluster of choice: kubectx <cluster-of-choice>.

Moreover this tool comes also with kubens which does exactly the same for namespaces:

kubens lists all namespaces and shows the current one,

kubens <namespace> picks up a namespace of choice.