[英]How to get 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"
}
我需要此特定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:~#
上面的命令返回空字符串。 我也嘗試了許多其他組合
ConfigMap
資源data
字段包含一個字符串,當您運行jsonpath
通過'{.data.cluster-config\\.json}'
選擇它時,它按'{.data.cluster-config\\.json}'
。 我的意思是,盡管它在Kubernetes中的存儲方式不同,但您使用的shell仍會在stdout
上將其打印為JSON。 如果運行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"
}
您將無法使用jsonpath
訪問該字符串中的“字段”,因為它實際上不是ConfigMap
API資源字段的一部分。
您可以嘗試使用第二個工具通過命令行JSON處理器jq
來獲取它。 該工具將jsonpath
將jsonpath
的輸出解釋為JSON並進行相應的解析。
例:
kubectl get cm cluster-info -n kube-system -o jsonpath='{.data.cluster-config\.json}' | jq '.cluster_id'
"xxx"
如果安裝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.