如标题所述,是否可以从API中找出K8s集群的名称?我查看了API但没有找到。
在API中无法找到K8s集群名称。如标题所述,是否可以从API中找出K8s集群的名称?我查看了API但没有找到。
在API中无法找到K8s集群名称。这将返回一个集群名称。
K8s:
kubectl config view -o jsonpath='{.clusters[].name}{"\n"}'
Openshift:
oc config view -o jsonpath='{.clusters[].name}{"\n"}'
const k8s = require('@kubernetes/client-node');
const kubeConfig = new k8s.KubeConfig();
kubeConfig.loadFromFile(process.env.KUBECONFIG_PATH);
clusterName = kubeConfig.getCurrentContext();
kubectl config get-clusters
kubectl config get-contexts
$ kubectl config get-clusters --> 获取现有集群列表
有一个很棒的工具叫做kubectx
https://github.com/ahmetb/kubectx。
kubectx
- 列出所有先前添加的集群,并突出显示当前使用的集群。这只需要输入一个单词,而不是kubectl config current-context
。
kubectx <cluster>
- 切换到所选的集群。
此外,这个工具还带有kubens
,它对命名空间执行完全相同的操作:
kubens
- 列出所有命名空间并显示当前命名空间,
kubens <namespace>
- 切换到所选的命名空间。
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
$ kubectl config get-clusters
NAME
kubernetes