k8s学习-kubectl命令行 jsonpath的使用
  TEZNKK3IfmPf 2023年11月14日 177 0

参考官网手册

​​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的使用

获取当前对象

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

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

获取pod的apiversion

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

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

获取pod的name

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

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

递归获取yaml所有的name

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

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

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

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

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

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

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

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

 

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

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

 

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

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

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

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

空格和换行符的引用

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

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

获取resources中的cpu的值

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

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

获取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的使用

 

获取容器的ip

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

 

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

 

获取所有的containerID和pod ip

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

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

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

 

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

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

获取所有的容器名称和镜像名称以及容器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的使用

使用ansible批量获取容器镜像和ID

集群很机器很多的时候,可以使用ansible批量获取指定容器的镜像和ID

例如,查询集群中所有名称为netchecker-server-6b59d6d5bc-7rdq7的pod的镜像版本和容器ID

kubectl get pods netchecker-server-6b59d6d5bc-7rdq7 -n default -o=jsonpath='[{.metadata.name},{.status.containerStatuses[0].image},{.status.containerStatuses[0].containerID}]{"\n"}'

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

 

使用ansible 批量执行

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}]'"

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

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2023年11月15日   37   0   0 k8s
  TEZNKK3IfmPf   2023年11月15日   24   0   0 k8s
TEZNKK3IfmPf