k8s解析数据 -o=jsonpath=‘{}‘ 的使用

server/2024/10/18 14:20:58/

kubectl -o=jsonpath= 是 Kubernetes 命令行工具 kubectl 中的一个输出选项,用于以 JSONPath 格式提取并展示 API 对象的部分内容。JSONPath 是一种查询语言,它允许用户在 JSON 结构中导航和提取数据。这对于自动化脚本和快速查询 Kubernetes 资源的特定部分非常有用。

当你执行 kubectl 命令并使用 -o--output 标志时,可以指定输出格式。如果后面跟的是 jsonpath= ,那么你就可以提供一个 JSONPath 表达式来精确地选择你想要从资源描述中提取的信息。

基本语法结构如下:

kubectl [command] [resource] [-o=jsonpath='{expression}']

这里的 expression 是一个遵循 JSONPath 语法的字符串,用于指定要提取的数据路径。例如:

  • {.metadata.name} 会提取对象的名称。
  • {.spec.containers[*].name} 会提取所有容器的名称,如果有多个容器的话。
  • {range .spec.containers[*]}{.name}{"\n"}{end} 使用 range 来迭代每个容器,并打印它们的名称,每个名称后跟一个换行符。

JSONPath 表达式中常用的符号包括:

  • . 访问对象的属性。
  • [] 访问数组元素或作为筛选条件。
  • {} 定义表达式或者用于创建输出模板。
  • * 通配符,匹配数组中的所有元素。
  • @ 引用当前对象。

通过这种方式,kubectl -o=jsonpath= 提供了一种灵活且强大的方法来查看 Kubernetes 资源的定制化视图。

示例如下:

kubectl get pod  csi-attacher-lxcfsplugin-0 -o yaml
apiVersion: v1
kind: Pod
metadata:creationTimestamp: "2024-04-17T06:33:35Z"generateName: csi-attacher-lxcfsplugin-labels:app: csi-attacher-lxcfsplugincontroller-revision-hash: csi-attacher-lxcfsplugin-75c7658464statefulset.kubernetes.io/pod-name: csi-attacher-lxcfsplugin-0name: csi-attacher-lxcfsplugin-0namespace: lxcfs-csiownerReferences:- apiVersion: apps/v1blockOwnerDeletion: truecontroller: truekind: StatefulSetname: csi-attacher-lxcfspluginuid: c520c298-7c0f-40c5-9878-8ea47be81e4aresourceVersion: "34427971"uid: b64b595e-c2a5-49c8-8ef2-a9b9987b2fe3
spec:containers:- args:- --csi-address=$(ADDRESS)- --v=5env:- name: ADDRESSvalue: /var/lib/csi/sockets/pluginproxy/csi.sockimage: docker.io/lxcfs:v1imagePullPolicy: Nevername: csi-provisionerresources:limits:cpu: 150mmemory: 100Mirequests:cpu: 150mmemory: 100MiterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- mountPath: /var/lib/csi/sockets/pluginproxyname: socket-dir- mountPath: /var/run/secrets/kubernetes.io/serviceaccountname: kube-api-access-xsjrpreadOnly: true- args:- --v=5- --csi-address=$(ADDRESS)env:- name: ADDRESSvalue: /var/lib/csi/sockets/pluginproxy/csi.sockimage: docker.io/lxcfs:v1imagePullPolicy: Nevername: csi-attacherresources:limits:cpu: 150mmemory: 100Mirequests:cpu: 150mmemory: 100MiterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- mountPath: /var/lib/csi/sockets/pluginproxy/name: socket-dir- mountPath: /var/run/secrets/kubernetes.io/serviceaccountname: kube-api-access-xsjrpreadOnly: true- args:- --nodeid=$(NODE_ID)- --endpoint=$(CSI_ENDPOINT)- --log-retain-date=5- --log-compress- --log-file-path=/var/log/controller.log- --log-max-size=200- --log-max-backups=10command:- /csi-serverenv:- name: NODE_IDvalueFrom:fieldRef:apiVersion: v1fieldPath: spec.nodeName- name: CSI_ENDPOINTvalue: unix://plugin/csi.sockimage: docker.io/lxcfs:v1imagePullPolicy: Nevername: csi-serverresources:limits:cpu: 150mmemory: 100Mirequests:cpu: 150mmemory: 100MiterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- mountPath: /pluginname: socket-dir- mountPath: /var/logname: log-volume- mountPath: /var/run/secrets/kubernetes.io/serviceaccountname: kube-api-access-xsjrpreadOnly: true- args:- /liveness/csi-livenessprobe --csiAddress=/var/lib/kubelet/plugins/csi-lxcfsplugin/csi.sock--httpEndpoint=127.0.0.1:10926command:- /bin/sh- -cimage: docker.io/lxcfs:v1imagePullPolicy: Nevername: health-checkresources:limits:cpu: 10mmemory: 100Mrequests:cpu: 10mmemory: 100MterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- mountPath: /plugin/name: socket-dir- mountPath: /var/run/secrets/kubernetes.io/serviceaccountname: kube-api-access-xsjrpreadOnly: truednsPolicy: ClusterFirstenableServiceLinks: truehostname: csi-attacher-lxcfsplugin-0nodeName: docker123nodeSelector:node-role.kubernetes.io/master: ""preemptionPolicy: PreemptLowerPrioritypriority: 1000000000priorityClassName: lxcfs-csi-priority-classrestartPolicy: AlwaysschedulerName: default-schedulersecurityContext: {}serviceAccount: csi-attacherserviceAccountName: csi-attachersubdomain: csi-attacherterminationGracePeriodSeconds: 30tolerations:- operator: Existsvolumes:- emptyDir: {}name: socket-dir- hostPath:path: /var/lib/docker/logs/lxcfs-csi-servertype: DirectoryOrCreatename: log-volume- name: kube-api-access-xsjrpprojected:defaultMode: 420sources:- serviceAccountToken:expirationSeconds: 3607path: token- configMap:items:- key: ca.crtpath: ca.crtname: kube-root-ca.crt- downwardAPI:items:- fieldRef:apiVersion: v1fieldPath: metadata.namespacepath: namespace
status:conditions:- lastProbeTime: nulllastTransitionTime: "2024-04-17T06:33:35Z"status: "True"type: Initialized- lastProbeTime: nulllastTransitionTime: "2024-04-22T02:26:11Z"status: "True"type: Ready- lastProbeTime: nulllastTransitionTime: "2024-04-22T02:26:11Z"status: "True"type: ContainersReady- lastProbeTime: nulllastTransitionTime: "2024-04-17T06:33:35Z"status: "True"type: PodScheduledcontainerStatuses:- containerID: containerd://3c0eeb141059a8dc099c1d3c4c98a6a6f25b855335b319a240f167983b5c00f4image: docker.io/lxcfs:v1imageID: sha256:19ec336305e47bfd0f64a7d1eb17dc01cb67cbbb856dbf18307999208c70c80blastState: {}name: csi-attacherready: truerestartCount: 0started: truestate:running:startedAt: "2024-04-22T02:25:41Z"- containerID: containerd://aa0dd18c42e1e6ecade723a358856e8705cf20ebc418290e74ec0c76325fd78aimage: docker.io/lxcfs:v1imageID: sha256:1011f81cb57bccc5591e54874ca3a16e0b20a8f80765ee2bab2eb55d3782e84elastState: {}name: csi-provisionerready: truerestartCount: 0started: truestate:running:startedAt: "2024-04-22T02:25:41Z"- containerID: containerd://23465fdf825016cefb4982c3367d9e34839f60707e2ecb57b4b6e617d3b341e0image: docker.io/lxcfs:v1imageID: sha256:e28f1150ab6c9bcf294be21403faf6862ad460891408a628ac23102d5f29b61dlastState: {}name: csi-serverready: truerestartCount: 0started: truestate:running:startedAt: "2024-04-22T02:25:56Z"- containerID: containerd://116a11be9a0b548bd04589c59228702043f0d3b1dad39a11f25f1aa1d68378beimage: docker.io/lxcfs:v1imageID: sha256:f9cf76b861fe4ba215a07ac5851d43aea7247996490992e38176e51ada755dd3lastState: {}name: health-checkready: truerestartCount: 0started: truestate:running:startedAt: "2024-04-22T02:26:11Z"hostIP: 11.166.91.187phase: RunningpodIP: 10.244.0.54podIPs:- ip: 10.244.0.54qosClass: GuaranteedstartTime: "2024-04-17T06:33:35Z"

1、获取kind

$ kubectl get pod  csi-attacher-lxcfsplugin-0 -o=jsonpath='{.kind}'
Pod

2、获取metadata 中的 generateName

$ kubectl get pod  csi-attacher-lxcfsplugin-0 -o=jsonpath='{.metadata.generateName}'
csi-attacher-lxcfsplugin-

3、获取metadata 中的labels中的app

$ kubectl get pod  csi-attacher-lxcfsplugin-0 -o=jsonpath='{.metadata.labels.app}'
csi-attacher-lxcfsplugin

4、获取metadata 中的ownerReferences中的uid

$ kubectl get pod  csi-attacher-lxcfsplugin-0 -o=jsonpath='{.metadata.ownerReferences}'
[{"apiVersion":"apps/v1","blockOwnerDeletion":true,"controller":true,"kind":"StatefulSet","name":"csi-attacher-lxcfsplugin","uid":"c520c298-7c0f-40c5-9878-8ea47be81e4a"}]
# 得到一个列表,且列表中只有一个值
$ kubectl get pod  csi-attacher-lxcfsplugin-0 -o=jsonpath='{.metadata.ownerReferences[0].uid}'
c520c298-7c0f-40c5-9878-8ea47be81e4a

5、获取spec 第四个containers中的resources中的requests中的cpu

$ kubectl get pod  csi-attacher-lxcfsplugin-0 -o=jsonpath='{.spec.containers[3]}' 
{"args":["/liveness/csi-livenessprobe --csiAddress=/var/lib/kubelet/plugins/csi-lxcfsplugin/csi.sock --httpEndpoint=127.0.0.1:10926"],"command":["/bin/sh","-c"],"image":"reg.docker.alibaba-inc.com/alibaba_apsara_paas/lxcfs_csi_livenessprobe:unionbase-v1","imagePullPolicy":"Never","name":"health-check","resources":{"limits":{"cpu":"10m","memory":"100M"},"requests":{"cpu":"10m","memory":"100M"}},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File","volumeMounts":[{"mountPath":"/plugin/","name":"socket-dir"},{"mountPath":"/var/run/secrets/kubernetes.io/serviceaccount","name":"kube-api-access-xsjrp","readOnly":true}]}$ kubectl get pod  csi-attacher-lxcfsplugin-0 -o=jsonpath='{.spec.containers[3].resources}' 
{"limits":{"cpu":"10m","memory":"100M"},"requests":{"cpu":"10m","memory":"100M"}}$ kubectl get pod  csi-attacher-lxcfsplugin-0 -o=jsonpath='{.spec.containers[3].resources.requests}' 
{"cpu":"10m","memory":"100M"}$ kubectl get pod  csi-attacher-lxcfsplugin-0 -o=jsonpath='{.spec.containers[3].resources.requests.cpu}' 
10m

6、获取每一个容器的name

$ kubectl get pod  csi-attacher-lxcfsplugin-0 -o=jsonpath='{range .spec.containers[*]}{.name}{"\n"}{end}' 
csi-provisioner
csi-attacher
csi-server
health-check

http://www.ppmy.cn/server/28055.html

相关文章

STM32 工程移植 LVGL:一步一步完成

STM32 工程移植 LVGL:一步一步完成 LVGL,作为一款强大且灵活的开源图形库,专为嵌入式系统GUI设计而生,极大地简化了开发者在创建美观用户界面时的工作。作为一名初学者,小编正逐步深入探索LVGL的奥秘,并决…

在CSDN创作了6个月,我收获了什么?文末送书~

作者主页:阿玥的小东东主页! 正在学习:python和C/C 期待大家的关注哦 目录 一次很好的机会,让我开始了CSDN之旅 首先来看看我的几位领路人 创作动力 1W粉丝 在CSDN我收获了什么? 很高的展现量 认证创作者身份 社…

OpenFeign修改HttpClient为Apache HttpClient 5

OpenFeign中http client 如果不做特殊配置,OpenFeign默认使用JDK自带的HttpURLConnection发送HTTP请求, 由于默认HttpURLConnection没有连接池、性能和效率比较低。所以修改为Apache HttpClient 5。 总结为两步: 加依赖改yml 具体操作请往…

代谢组数据分析五:溯源分析

MetOrigin Analysis {#MetOriginAnalysis} 微生物群及其代谢产物与人类健康和疾病密切相关。然而,理解微生物组和代谢物之间复杂的相互作用是具有挑战性的。 在研究肠道代谢物时,代谢物的来源是一个无法避免的问题即代谢物到底是来自肠道微生物的代谢还是宿主本身代谢产生的…

【深耕 Python】Data Science with Python 数据科学(17)Scikit-learn机器学习(二)

写在前面 关于数据科学环境的建立,可以参考我的博客: 【深耕 Python】Data Science with Python 数据科学(1)环境搭建 往期数据科学博文一览: 【深耕 Python】Data Science with Python 数据科学(2&…

企微魔盒V15.6独立版源码+搭建环境教程

企微魔盒V15.6修复已知问题。 搭建教程: 系统环境:CentOS、 运行环境:宝塔 Linux 网站环境:Nginx 1.15.10 MySQL 5.6.46 PHP-7.1-7.3 常见插件:fileinfo ; redis

软件工程全过程性文档(软件全套文档整理)

软件项目相关全套精华资料包获取方式①:进主页。 获取方式②:本文末个人名片直接获取。 在软件开发的全过程中,文档是记录项目进展、决策、设计和测试结果的重要工具。以下是一个简要的软件全过程性文档梳理清单: 需求分析阶段…

命名空间、C++的输入输出、缺省参数(默认参数)、函数重载

🐶博主主页:ᰔᩚ. 一怀明月ꦿ ❤️‍🔥专栏系列:线性代数,C初学者入门训练,题解C,C的使用文章,「初学」C,linux 🔥座右铭:“不要等到什么都没有了…