CKA [Installation] – 使用JsonPath输出Pod的信息
Kubernetes JsonPath 官方文档:https://kubernetes.io/docs/reference/kubectl/jsonpath/
✅ 基本的Kubernetes JsonPath的使用

✅ 实践测试
1. 创建2个pod
kubectl run nginx1 --image=nginx
kubectl run nginx2 --image=nginx
2. 把pod列表输出成json格式
kubetl get pod -o yaml
{
"apiVersion": "v1",
"items": [
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"annotations": {
"cni.projectcalico.org/containerID": "e95a1fd539b25d58153c9ee7c055bedea95741898fbba38db36ab16201d8bd7c",
"cni.projectcalico.org/podIP": "10.244.120.85/32",
"cni.projectcalico.org/podIPs": "10.244.120.85/32"
},
"creationTimestamp": "2025-10-26T07:26:57Z",
"generation": 1,
"labels": {
"run": "nginx1"
},
"name": "nginx1",
"namespace": "default",
"resourceVersion": "13537",
"uid": "4d425488-20e7-4a33-9868-88fda1934410"
},
"spec": {
"containers": [
{
"image": "nginx",
"imagePullPolicy": "Always",
"name": "nginx1",
"resources": {},
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"volumeMounts": [
{
"mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
"name": "kube-api-access-hbccj",
"readOnly": true
}
]
}
],
"dnsPolicy": "ClusterFirst",
"enableServiceLinks": true,
"nodeName": "minikube",
"preemptionPolicy": "PreemptLowerPriority",
"priority": 0,
"restartPolicy": "Always",
"schedulerName": "default-scheduler",
"securityContext": {},
"serviceAccount": "default",
"serviceAccountName": "default",
"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": [
{
"name": "kube-api-access-hbccj",
"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": "2025-10-26T07:27:11Z",
"observedGeneration": 1,
"status": "True",
"type": "PodReadyToStartContainers"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2025-10-26T07:26:57Z",
"observedGeneration": 1,
"status": "True",
"type": "Initialized"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2025-10-26T07:27:11Z",
"observedGeneration": 1,
"status": "True",
"type": "Ready"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2025-10-26T07:27:11Z",
"observedGeneration": 1,
"status": "True",
"type": "ContainersReady"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2025-10-26T07:26:57Z",
"observedGeneration": 1,
"status": "True",
"type": "PodScheduled"
}
],
"containerStatuses": [
{
"containerID": "docker://e3b7b275663d60e3056a8c8ea00995f3842925f568f8ff86aa2861ab8d77ae5f",
"image": "nginx:latest",
"imageID": "docker-pullable://nginx@sha256:029d4461bd98f124e531380505ceea2072418fdf28752aa73b7b273ba3048903",
"lastState": {},
"name": "nginx1",
"ready": true,
"resources": {},
"restartCount": 0,
"started": true,
"state": {
"running": {
"startedAt": "2025-10-26T07:27:10Z"
}
},
"volumeMounts": [
{
"mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
"name": "kube-api-access-hbccj",
"readOnly": true,
"recursiveReadOnly": "Disabled"
}
]
}
],
"hostIP": "192.168.49.2",
"hostIPs": [
{
"ip": "192.168.49.2"
}
],
"observedGeneration": 1,
"phase": "Running",
"podIP": "10.244.120.85",
"podIPs": [
{
"ip": "10.244.120.85"
}
],
"qosClass": "BestEffort",
"startTime": "2025-10-26T07:26:57Z"
}
},
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"annotations": {
"cni.projectcalico.org/containerID": "a2d9625cc574021f334c9bec0d46ef27b7cc081ca6109c535622034a85895199",
"cni.projectcalico.org/podIP": "10.244.120.86/32",
"cni.projectcalico.org/podIPs": "10.244.120.86/32"
},
"creationTimestamp": "2025-10-26T07:27:02Z",
"generation": 1,
"labels": {
"run": "nginx2"
},
"name": "nginx2",
"namespace": "default",
"resourceVersion": "13543",
"uid": "1fd4b1a1-a393-4096-8107-9894be16fcc4"
},
"spec": {
"containers": [
{
"image": "nginx",
"imagePullPolicy": "Always",
"name": "nginx2",
"resources": {},
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"volumeMounts": [
{
"mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
"name": "kube-api-access-l77wm",
"readOnly": true
}
]
}
],
"dnsPolicy": "ClusterFirst",
"enableServiceLinks": true,
"nodeName": "minikube",
"preemptionPolicy": "PreemptLowerPriority",
"priority": 0,
"restartPolicy": "Always",
"schedulerName": "default-scheduler",
"securityContext": {},
"serviceAccount": "default",
"serviceAccountName": "default",
"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": [
{
"name": "kube-api-access-l77wm",
"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": "2025-10-26T07:27:14Z",
"observedGeneration": 1,
"status": "True",
"type": "PodReadyToStartContainers"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2025-10-26T07:27:02Z",
"observedGeneration": 1,
"status": "True",
"type": "Initialized"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2025-10-26T07:27:14Z",
"observedGeneration": 1,
"status": "True",
"type": "Ready"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2025-10-26T07:27:14Z",
"observedGeneration": 1,
"status": "True",
"type": "ContainersReady"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2025-10-26T07:27:02Z",
"observedGeneration": 1,
"status": "True",
"type": "PodScheduled"
}
],
"containerStatuses": [
{
"containerID": "docker://606af6f59533aa7625f5275c48ecca1c8919663a99a123e0c396d0b9130e44eb",
"image": "nginx:latest",
"imageID": "docker-pullable://nginx@sha256:029d4461bd98f124e531380505ceea2072418fdf28752aa73b7b273ba3048903",
"lastState": {},
"name": "nginx2",
"ready": true,
"resources": {},
"restartCount": 0,
"started": true,
"state": {
"running": {
"startedAt": "2025-10-26T07:27:13Z"
}
},
"volumeMounts": [
{
"mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
"name": "kube-api-access-l77wm",
"readOnly": true,
"recursiveReadOnly": "Disabled"
}
]
}
],
"hostIP": "192.168.49.2",
"hostIPs": [
{
"ip": "192.168.49.2"
}
],
"observedGeneration": 1,
"phase": "Running",
"podIP": "10.244.120.86",
"podIPs": [
{
"ip": "10.244.120.86"
}
],
"qosClass": "BestEffort",
"startTime": "2025-10-26T07:27:02Z"
}
}
],
"kind": "List",
"metadata": {
"resourceVersion": ""
}
}
3.使用jsonpath evaluator , 来把json资料extract出来,我们现在制作的是从json输出pod name
https://jsonpath.com/
$.items[*].metadata.name

4. 使用kubctl get pod 通过jsonpath 输出pod名出来出来 (在最尾端添加breakline)
kubectl get pod -o jsonpath='{$.items[*].metadata.name}{"\n"}'

5. 使用 jsonpath range来loop 每一个pod名,以达到每个pod之后就breakline
kubectl get pod -o jsonpath='{range $.items[*]}{.metadata.name}{"\n"}{end}'

6. 在这个range loop 多添加显示pod ip
kubectl get pod -o jsonpath='{range $.items[*]}{.metadata.name}{"\t"}{.status.podIP}{"\n"}{end}'

![]()
Facebook评论