kubectl get -o jsonpath

kubectl get -o jsonpath{..something}
会自动递归匹配到key为something的value
kubectl get -o jsonpath

会精确匹配到.a.b下key为something的value

在 JSON里[]是 Array(也就是数组),通过下表引用,{}是Ojbect(也就是对象),通过"."引用。

range的使用
kubectl get pv pvc-f0c3815a-9536-41f1-98a9-1753c7180329 -o jsonpath=' { .spec.nodeAffinity.required.nodeSelectorTerms[*]} '
map[matchExpressions:[map[key:kubernetes.io/hostname operator:In values:[ainode-2]]]]

kubectl get pv pvc-f0c3815a-9536-41f1-98a9-1753c7180329 -o jsonpath=' {range .spec.nodeAffinity.required.nodeSelectorTerms[]} {range .matchExpressions[]} {.values} {end}{"\n"}{end} '
[ainode-2]

可以不带中括号,

{range .items[*]}[{.metadata.name}, {.status.capacity}] {end}

查看集群所有node的label

for i in kubectl get nodes | awk '{print $1}';do kubectl get node $i -o jsonpath=' {.metadata.name} {"\n"} {.metadata.labels} {"\n"}' ;done

查看一个pod的所有容器

kubectl get pod xxx-xxx -o jsonpath='{range .spec.containers[*]} [{.name}] {end}'

posted on 2022-01-21 15:42  yangras  阅读(692)  评论(0编辑  收藏  举报

导航