首页 > 解决方案 > 如何使用 cli 获取 kube-contexts 列表

问题描述

我正在尝试获取 kube-contexts 列表(并过滤 gke 集群),并使用一些工具结束了:

kubectl config get-contexts | tr -s ' ' | cut -d ' ' -f 2 | grep gke

输出:

gke_dev-redacted
gke_prod-redacted

有没有更简单的方法来做到这一点(这不取决于命令输出不使用制表符,而是使用多个空格的事实)。该命令不支持 yaml 或 json 输出:

--output yaml is not available in kubectl config get-contexts; resetting to default output format

标签: kuberneteskubectl

解决方案


您可以提供--output标志以仅显示上下文的名称,例如:

$ kubectl config get-contexts --output=name
minikube

然后很容易找到 GKE 上下文:

$ kubectl config get-contexts --output=name | grep "gke_*"

推荐阅读