转自: https://blog.51cto.com/u_11555417/5521927
参考官网手册
说明 JSONPath 支持 | Kubernetes
JSONPath 模板由 {} 包起来的 JSONPath 表达式组成。Kubectl 使用 JSONPath 表达式来过滤 JSON 对象中的特定字段并格式化输出。 除了原始的 JSONPath 模板语法,以下函数和语法也是有效的:
使用双引号将 JSONPath 表达式内的文本引起来。 使用range,end 运算符来迭代列表。 使用负片索引后退列表。负索引不会“环绕”列表,并且只要-index + listLength> = 0 就有效。 通过kubectl命令行配合jsonpath就能获取过滤到我们关注的信息。
函数 描述 示例 结果 text 纯文本 kind is {.kind}kind is List @ 当前对象 {@}与输入相同 . or [] 子运算符 {.kind} or {['kind']}List .. 递归下降 {..name}127.0.0.1 127.0.0.2 myself e2e * 通配符。获取所有对象 {.items[*].metadata.name}[127.0.0.1 127.0.0.2] [start:end :step] 下标运算符 {.users[0].name}myself [,] 并集运算符 {.items[*]['metadata.name', 'status.capacity']}127.0.0.1 127.0.0.2 map[cpu:4] map[cpu:8] ?() 过滤 {.users[?(@.name=="e2e")].user.password}secret range, end 迭代列表 {range .items[*]}[{.metadata.name}, {.status.capacity}] {end}[127.0.0.1, map[cpu:4]] [127.0.0.2, map[cpu:8]] '' 引用解释执行字符串 {range .items[*]}{.metadata.name}{'\t'}{end}127.0.0.1 127.0.0.2
使用举例
1 2 3 4 5 6 kubectl get pods -o json kubectl get pods -o=jsonpath='{@}' kubectl get pods -o=jsonpath='{.items[0]}' kubectl get pods -o=jsonpath='{.items[0].metadata.name}' kubectl get pods -o=jsonpath="{.items[*]['metadata.name', 'status.capacity']}" kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'
不支持正则表达式,如果需要使用正则表达式进行匹配操作,可以使用jq 之类的工具
1 2 3 4 5 6 kubectl get pods -o jsonpath='{.items[?(@.metadata.name=~/^test$/)].metadata.name}' kubectl get pods -o json | jq -r '.items[] | select(.metadata.name | test("test-")).spec.containers[].image'
使用例子 文本方式 1 kubectl get pod cm-test-pod -o jsonpath='kind is {.kind}'
获取当前对象 1 kubectl get pod cm-test-pod -o jsonpath='{@}'
获取pod的apiversion 1 kubectl get pod cm-test-pod -o jsonpath='{.apiVersion}'
获取pod的name 1 kubectl get pod cm-test-pod -o jsonpath='{.metadata.name}'
递归获取yaml所有的name 1 kubectl get pod cm-test-pod -o jsonpath='{..name}'
获取所有状态条件中的类型 1 kubectl get pod cm-test-pod -o jsonpath='{.status.conditions[*].type}'
获取状态第一个条件的类型 1 kubectl get pod cm-test-pod -o jsonpath='{.status.conditions[0].type}'
从第一个状态条件开始到最后一个结束,每隔2个获取一次 1 kubectl get pod cm-test-pod -o jsonpath='{.status.conditions[0:3:2].type}'
获取状态条件中的状态和类型 1 kubectl get pod cm-test-pod -o jsonpath='{range .status.conditions[*]}[{..status},{..type}]{end}'
空格和换行符的引用 1 kubectl get pod cm-test-pod -o jsonpath='{range .status.conditions[*]}[{..status},{..type}]{"\n"}{end}'
获取resources中的cpu的值 1 kubectl get pod nginx-67d5fc57d8-jkfjp -n quota-example -o jsonpath='{range .spec.*].resources}[{..cpu}]{"\n"}{end}'
获取resources中的cpu和memory的值 1 kubectl get pod nginx-67d5fc57d8-jkfjp -n quota-example -o jsonpath='{range .spec.containers[*].resources}[{..cpu},{..memroy}]{"\n"}{end}'
获取容器的ip 1 kubectl get pod nginx-67d5fc57d8-jkfjp -n quota-example -o jsonpath='{.status.podIPs[].ip}{"\n"}'
获取所有的containerID和pod ip 1 kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}[{.status.containerStatuses[0].containerID}, {.status.podIP}]{"\n"}{end}'
获取所有的容器名称和镜像名称 1 kubectl get pods -n kube-system -o=jsonpath='{range .items[*]}[{.metadata.name},{.status.containerStatuses[0].image}]{"\n"}{end}'
获取所有的容器名称和镜像名称以及容器ID到文件 1 kubectl get pods -n kube-system -o=jsonpath='{range .items[*]}[{.metadata.name},{.status.containerStatuses[0].image},{.status.containerStatuses[0].containerID}]{"\n"}{end}' > resulst.txt
使用ansible批量获取容器镜像和ID 集群很机器很多的时候,可以使用ansible批量获取指定容器的镜像和ID
例如,查询集群中所有名称为netchecker-server-6b59d6d5bc-7rdq7的pod的镜像版本和容器ID
1 kubectl get pods netchecker-server-6b59d6d5bc-7rdq7 -n default -o=jsonpath='[{.metadata.name},{.status.containerStatuses[0].image},{.status.containerStatuses[0].containerID}]{"\n"}'
使用ansible 批量执行
1 ansible k8s-master -m shell -a "kubectl get pods netchecker-server-6b59d6d5bc-7rdq7 -n default -o=jsonpath='[{.metadata.name},{.status.containerStatuses[0].image},{.status.containerStatuses[0].containerID}]'"