关于kubernetes的jsonpath
我们通常可以见到这种类型的命令:
kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l app=ks-install -o jsonpath='{.items[0].metadata.name}') -f
这里jsonpath是做什么用的,下面我们探究一下,
在kuberctl get --help中的描述:
-o, --output='': Output format. One of: json|yaml|wide|name|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=... See custom columns [http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns], golang template [http://golang.org/pkg/te
xt/template/#pkg-overview] and jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].
访问:http://kubernetes.io/docs/user-guide/jsonpath
kubectl get pod -n kubesphere-system -l app=ks-install -o json
点击查看代码
{
"apiVersion": "v1",
"items": [
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"annotations": {
"cni.projectcalico.org/containerID": "53f8ab7fbf2ce1ae23f9848168e23f75aff5d0fc1a89dadd02153937549dba08",
"cni.projectcalico.org/podIP": "10.233.96.1/32",
"cni.projectcalico.org/podIPs": "10.233.96.1/32"
},
"creationTimestamp": "2022-01-03T07:54:26Z",
"generateName": "ks-installer-69df988b79-",
"labels": {
"app": "ks-install",
"pod-template-hash": "69df988b79",
"version": "v3.2.1"
},
"name": "ks-installer-69df988b79-t2lrn",
"namespace": "kubesphere-system",
"ownerReferences": [
{
"apiVersion": "apps/v1",
"blockOwnerDeletion": true,
"controller": true,
"kind": "ReplicaSet",
"name": "ks-installer-69df988b79",
"uid": "0151a8f9-932e-4185-bcd5-be838502c47d"
}
],
"resourceVersion": "991",
"uid": "d4afe165-f4e1-4f45-85ae-a0ab49c515b4"
},
"spec": {
"containers": [
{
"image": "registry.cn-beijing.aliyuncs.com/kubesphereio/ks-installer:v3.2.1",
"imagePullPolicy": "Always",
"name": "installer",
"resources": {},
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"volumeMounts": [
{
"mountPath": "/etc/localtime",
"name": "host-time"
},
{
"mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
"name": "kube-api-access-92t8r",
"readOnly": true
}
]
}
],
"dnsPolicy": "ClusterFirst",
"enableServiceLinks": true,
"nodeName": "node2",
"preemptionPolicy": "PreemptLowerPriority",
"priority": 0,
"restartPolicy": "Always",
"schedulerName": "default-scheduler",
"securityContext": {},
"serviceAccount": "ks-installer",
"serviceAccountName": "ks-installer",
"terminationGracePeriodSeconds": 30,
"tolerations": [
{
"effect": "NoExecute",
"key": "node.kubernetes.io/not-ready",
"operator": "Exists",
"tolerationSeconds": 300
},
{
"effect": "NoExecute",
"key": "node.kubernetes.io/unreachable",
"operator": "Exists",
"tolerationSeconds": 300
}
],
"volumes": [
{
"hostPath": {
"path": "/etc/localtime",
"type": ""
},
"name": "host-time"
},
{
"name": "kube-api-access-92t8r",
"projected": {
"defaultMode": 420,
"sources": [
{
"serviceAccountToken": {
"expirationSeconds": 3607,
"path": "token"
}
},
{
"configMap": {
"items": [
{
"key": "ca.crt",
"path": "ca.crt"
}
],
"name": "kube-root-ca.crt"
}
},
{
"downwardAPI": {
"items": [
{
"fieldRef": {
"apiVersion": "v1",
"fieldPath": "metadata.namespace"
},
"path": "namespace"
}
]
}
}
]
}
}
]
},
"status": {
"conditions": [
{
"lastProbeTime": null,
"lastTransitionTime": "2022-01-03T07:54:27Z",
"status": "True",
"type": "Initialized"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2022-01-03T07:54:52Z",
"status": "True",
"type": "Ready"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2022-01-03T07:54:52Z",
"status": "True",
"type": "ContainersReady"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2022-01-03T07:54:27Z",
"status": "True",
"type": "PodScheduled"
}
],
"containerStatuses": [
{
"containerID": "docker://709d6513812c960d2a379ea125779a1374621b08e4d495d85c3cca402aad142b",
"image": "registry.cn-beijing.aliyuncs.com/kubesphereio/ks-installer:v3.2.1",
"imageID": "docker-pullable://registry.cn-beijing.aliyuncs.com/kubesphereio/ks-installer@sha256:749f1ed10c672ae460f79932553ac5ba321b98df972b778041dc2cc7402dd478",
"lastState": {},
"name": "installer",
"ready": true,
"restartCount": 0,
"started": true,
"state": {
"running": {
"startedAt": "2022-01-03T07:54:52Z"
}
}
}
],
"hostIP": "172.30.47.69",
"phase": "Running",
"podIP": "10.233.96.1",
"podIPs": [
{
"ip": "10.233.96.1"
}
],
"qosClass": "BestEffort",
"startTime": "2022-01-03T07:54:27Z"
}
}
],
"kind": "List",
"metadata": {
"resourceVersion": "",
"selfLink": ""
}
}
根据这个描述:
{.items[0].metadata.name} 得到的就是“ks-installer-69df988b79-t2lrn”
命令就相当于:
kubectl logs -n kubesphere-system ks-installer-69df988b79-t2lrn -f
省去了手动查找pod的步骤。
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程