[英]How to get cluster id using kubectl command
I need cluster-id using kubectl command. 我需要使用kubectl命令的cluster-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"
}
I need cluster-id
of this particular json 我需要此特定json的cluster-id
root@vagrant-xenial64:~# kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json.cluster_id}'
root@vagrant-xenial64:~#
Above command returns empty string. 上面的命令返回空字符串。 I tried many other combinations too 我也尝试了许多其他组合
Your ConfigMap
resource data
-field contains a string which are interpreted as-is when you run jsonpath
to select it via '{.data.cluster-config\\.json}'
. ConfigMap
资源data
字段包含一个字符串,当您运行jsonpath
通过'{.data.cluster-config\\.json}'
选择它时,它按'{.data.cluster-config\\.json}'
。 What i mean is that the shell you use will print it as JSON at stdout
although it's stored differently in Kubernetes. 我的意思是,尽管它在Kubernetes中的存储方式不同,但您使用的shell仍会在stdout
上将其打印为JSON。 If you run kubectl get cm cluster-info -n kube-system -o json
and look at the data
-field it might look something like this: 如果运行kubectl get cm cluster-info -n kube-system -o json
并查看data
-field,它可能看起来像这样:
"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"
}
You won't be able to access the "fields" within that string with jsonpath
since it's not actually part of the ConfigMap
API resource fields. 您将无法使用jsonpath
访问该字符串中的“字段”,因为它实际上不是ConfigMap
API资源字段的一部分。
You could try to use a second tool to fetch it though, using jq
, a command-line JSON processor. 您可以尝试使用第二个工具通过命令行JSON处理器jq
来获取它。 This tool would interpret the output of jsonpath
as JSON on the fly and parse it accordingly. 该工具将jsonpath
将jsonpath
的输出解释为JSON并进行相应的解析。
Example: 例:
kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json}' | jq '.cluster_id'
"xxx"
If installing eg jq
defeat any purposes i would recommend to use a combination of already available tools (assuming you're on Linux) like grep
, awk
and sed
: 如果安装jq
失败了,我建议使用grep
, awk
和sed
等已经可用的工具(假设您使用的是Linux)的组合:
kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json}' | grep cluster_id | awk '{ print $2 }' | sed -e 's/"//' -e 's/",//'
xxx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.