0
点赞
收藏
分享

微信扫一扫

k8s学习-kubectl命令行 jsonpath的使用

参考官网手册

说明

​​JSONPath 支持 | Kubernetes​​

JSONPath 模板由 {} 包起来的 JSONPath 表达式组成。Kubectl 使用 JSONPath 表达式来过滤 JSON 对象中的特定字段并格式化输出。 除了原始的 JSONPath 模板语法,以下函数和语法也是有效的:

  1. 使用双引号将 JSONPath 表达式内的文本引起来。
  2. 使用range,end 运算符来迭代列表。
  3. 使用负片索引后退列表。负索引不会“环绕”列表,并且只要-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



使用举例

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 之类的工具

# kubectl 的 JSONpath 输出不支持正则表达式
# 下面的命令不会生效
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'

使用例子

k8s学习-kubectl命令行 jsonpath的使用_运算符

文本方式

kubectl get pod cm-test-pod -o jsonpath='kind is {.kind}'

k8s学习-kubectl命令行 jsonpath的使用_正则表达式_02

获取当前对象

kubectl get pod cm-test-pod -o jsonpath='{@}'

k8s学习-kubectl命令行 jsonpath的使用_运算符_03

获取pod的apiversion

kubectl get pod cm-test-pod -o jsonpath='{.apiVersion}'

k8s学习-kubectl命令行 jsonpath的使用_正则表达式_04

获取pod的name

 kubectl get pod cm-test-pod -o jsonpath='{.metadata.name}'

k8s学习-kubectl命令行 jsonpath的使用_正则表达式_05

递归获取yaml所有的name

kubectl get pod cm-test-pod -o jsonpath='{..name}'

k8s学习-kubectl命令行 jsonpath的使用_正则表达式_06

获取所有状态条件中的类型

kubectl get pod cm-test-pod -o jsonpath='{.status.conditions[*].type}'

k8s学习-kubectl命令行 jsonpath的使用_正则表达式_07

获取状态第一个条件的类型

kubectl get pod cm-test-pod -o jsonpath='{.status.conditions[0].type}'

k8s学习-kubectl命令行 jsonpath的使用_json_08


从第一个状态条件开始到最后一个结束,每隔2个获取一次

kubectl get pod cm-test-pod -o jsonpath='{.status.conditions[0:3:2].type}'


k8s学习-kubectl命令行 jsonpath的使用_json_09

获取状态条件中的状态和类型

 kubectl get pod cm-test-pod -o jsonpath='{range .status.conditions[*]}[{..status},{..type}]{end}'

k8s学习-kubectl命令行 jsonpath的使用_运算符_10

空格和换行符的引用

kubectl get pod cm-test-pod -o jsonpath='{range .status.conditions[*]}[{..status},{..type}]{"\n"}{end}'

k8s学习-kubectl命令行 jsonpath的使用_json_11

获取resources中的cpu的值

 kubectl  get pod nginx-67d5fc57d8-jkfjp -n quota-example  -o jsonpath='{range .spec.*].resources}[{..cpu}]{"\n"}{end}'

k8s学习-kubectl命令行 jsonpath的使用_正则表达式_12

获取resources中的cpu和memory的值

kubectl  get pod nginx-67d5fc57d8-jkfjp -n quota-example  -o jsonpath='{range .spec.containers[*].resources}[{..cpu},{..memroy}]{"\n"}{end}'

k8s学习-kubectl命令行 jsonpath的使用_json_13


获取容器的ip

kubectl  get pod nginx-67d5fc57d8-jkfjp -n quota-example  -o jsonpath='{.status.podIPs[].ip}{"\n"}'


k8s学习-kubectl命令行 jsonpath的使用_json_14


获取所有的containerID和pod ip

kubectl get pods --all-namespaces    -o=jsonpath='{range .items[*]}[{.status.containerStatuses[0].containerID}, {.status.podIP}]{"\n"}{end}'

k8s学习-kubectl命令行 jsonpath的使用_正则表达式_15

获取所有的容器名称和镜像名称

 

kubectl get pods -n kube-system -o=jsonpath='{range .items[*]}[{.metadata.name},{.status.containerStatuses[0].image}]{"\n"}{end}'

k8s学习-kubectl命令行 jsonpath的使用_正则表达式_16

获取所有的容器名称和镜像名称以及容器ID到文件

kubectl get pods -n kube-system -o=jsonpath='{range .items[*]}[{.metadata.name},{.status.containerStatuses[0].image},{.status.containerStatuses[0].containerID}]{"\n"}{end}' > resulst.txt

k8s学习-kubectl命令行 jsonpath的使用_正则表达式_17

举报

相关推荐

0 条评论