How to get Kubernetes cluster name from K8s API

喜欢而已 提交于 2020-03-17 05:14:19

问题


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.


回答1:


Unfortunately a cluster doesn't know it's own name, or anything else that would uniquely identify it (K8s issue #44954). I wanted to know for helm issue #2055.




回答2:


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




回答3:


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

kubectl cluster-info




回答4:


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.




回答5:


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



回答6:


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 {



回答7:


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

kubectl config view




回答8:


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



回答9:


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



来源:https://stackoverflow.com/questions/38242062/how-to-get-kubernetes-cluster-name-from-k8s-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!