如何使用 kubectl 命令获取集群 ID
How to get cluster id using kubectl command
我需要使用 kubectl 命令的集群 ID。
root@vagrant-xenial64:~# kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json}'
{
"cluster_id": "xxx",
"cluster_name": "prod-yyy-mmm",
"cluster_type": "rrr",
"cluster_pay_tier": "vvv",
"datacenter": "cse",
"account_id": "456777",
"created": "2018-06-32323dffdf:35:48+0000"
}
我需要 cluster-id
这个 json
root@vagrant-xenial64:~# kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json.cluster_id}'
root@vagrant-xenial64:~#
以上命令 returns 空字符串。
我也尝试了很多其他组合
您的 ConfigMap
资源 data
-field 包含一个字符串,当您通过 运行 jsonpath
到 select 它时,该字符串将按原样解释16=]。我的意思是,您使用的 shell 会将其打印为 JSON at stdout
,尽管它在 Kubernetes 中的存储方式不同。如果您 运行 kubectl get cm cluster-info -n kube-system -o json
并查看 data
字段,它可能看起来像这样:
"data": {
"cluster-config.json": "{\n \"cluster_id\": \"xxx\",\n \"cluster_name\": \"prod-yyy-mmm\",\n \"cluster_type\": \"rrr\",\n \"cluster_pay_tier\": \"vvv\",\n \"datacenter\": \"cse\",\n \"account_id\": \"456777\",\n \"created\": \"2018-06-32323dffdf:35:48+0000\"\n}\n"
}
您将无法使用 jsonpath
访问该字符串中的 "fields",因为它实际上不是 ConfigMap
API resource 字段的一部分。
您可以尝试使用第二个工具来获取它,使用 jq
,一个命令行 JSON 处理器。该工具会将 jsonpath
的输出即时解释为 JSON 并相应地对其进行解析。
示例:
kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json}' | jq '.cluster_id'
"xxx"
如果安装例如jq
打败我建议使用现有工具组合的任何目的(假设您使用 Linux),例如 grep
、awk
和 sed
:
kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json}' | grep cluster_id | awk '{ print }' | sed -e 's/"//' -e 's/",//'
xxx
我需要使用 kubectl 命令的集群 ID。
root@vagrant-xenial64:~# kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json}'
{
"cluster_id": "xxx",
"cluster_name": "prod-yyy-mmm",
"cluster_type": "rrr",
"cluster_pay_tier": "vvv",
"datacenter": "cse",
"account_id": "456777",
"created": "2018-06-32323dffdf:35:48+0000"
}
我需要 cluster-id
这个 json
root@vagrant-xenial64:~# kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json.cluster_id}'
root@vagrant-xenial64:~#
以上命令 returns 空字符串。 我也尝试了很多其他组合
您的 ConfigMap
资源 data
-field 包含一个字符串,当您通过 运行 jsonpath
到 select 它时,该字符串将按原样解释16=]。我的意思是,您使用的 shell 会将其打印为 JSON at stdout
,尽管它在 Kubernetes 中的存储方式不同。如果您 运行 kubectl get cm cluster-info -n kube-system -o json
并查看 data
字段,它可能看起来像这样:
"data": {
"cluster-config.json": "{\n \"cluster_id\": \"xxx\",\n \"cluster_name\": \"prod-yyy-mmm\",\n \"cluster_type\": \"rrr\",\n \"cluster_pay_tier\": \"vvv\",\n \"datacenter\": \"cse\",\n \"account_id\": \"456777\",\n \"created\": \"2018-06-32323dffdf:35:48+0000\"\n}\n"
}
您将无法使用 jsonpath
访问该字符串中的 "fields",因为它实际上不是 ConfigMap
API resource 字段的一部分。
您可以尝试使用第二个工具来获取它,使用 jq
,一个命令行 JSON 处理器。该工具会将 jsonpath
的输出即时解释为 JSON 并相应地对其进行解析。
示例:
kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json}' | jq '.cluster_id'
"xxx"
如果安装例如jq
打败我建议使用现有工具组合的任何目的(假设您使用 Linux),例如 grep
、awk
和 sed
:
kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json}' | grep cluster_id | awk '{ print }' | sed -e 's/"//' -e 's/",//'
xxx