Kubectl 支持 JSONPath 模板。
JSONPath 模板由 {} 包起来的 JSONPath 表达式组成。Kubectl 使用 JSONPath 表达式来过滤 JSON 对象中的特定字段并格式化输出。除了原始的 JSONPath 模板语法,以下函数和语法也是有效的:
range
,end
运算符来迭代列表。-index + listLength> = 0
就有效。 函数 |
描述 |
示例 |
结果 |
---|---|---|---|
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 |
下面我们在k8s中使用jsonpath来获取我们想要的内容
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o json
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"annotations": {
"tke.cloud.tencent.com/networks-status": "[{\n \"name\": \"tke-bridge\",\n \"ips\": [\n \"172.16.3.92\"\n ],\n \"default\": true,\n \"dns\": {}\n}]"
},
"creationTimestamp": "2020-08-25T09:15:55Z",
"generateName": "redis-5b4495ddb4-",
"labels": {
"k8s-app": "redis",
"pod-template-hash": "5b4495ddb4",
"qcloud-app": "redis"
},
"name": "redis-5b4495ddb4-szjtz",
"namespace": "default",
"ownerReferences": [
{
"apiVersion": "apps/v1",
"blockOwnerDeletion": true,
"controller": true,
"kind": "ReplicaSet",
"name": "redis-5b4495ddb4",
"uid": "bef91151-d94e-46d5-a8a9-59a8f3ae634e"
}
],
"resourceVersion": "12100505479",
"selfLink": "/api/v1/namespaces/default/pods/redis-5b4495ddb4-szjtz",
"uid": "968a56ba-80ed-41d3-ad27-deb7d060bfe7"
},
"spec": {
"containers": [
{
"image": "redis",
"imagePullPolicy": "Always",
"name": "redis",
"resources": {
"limits": {
"cpu": "500m",
"memory": "1Gi"
},
"requests": {
"cpu": "250m",
"memory": "256Mi"
}
},
"securityContext": {
"privileged": false
},
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"volumeMounts": [
{
"mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
"name": "default-token-cl2h8",
"readOnly": true
}
]
}
],
"dnsPolicy": "ClusterFirst",
"enableServiceLinks": true,
"imagePullSecrets": [
{
"name": "qcloudregistrykey"
}
],
"nodeName": "10.168.1.4",
"restartPolicy": "Always",
"schedulerName": "default-scheduler",
"securityContext": {},
"serviceAccount": "default",
"serviceAccountName": "default",
"terminationGracePeriodSeconds": 30,
"volumes": [
{
"name": "default-token-cl2h8",
"secret": {
"defaultMode": 420,
"secretName": "default-token-cl2h8"
}
}
]
},
"status": {
"conditions": [
{
"lastProbeTime": null,
"lastTransitionTime": "2020-08-25T09:15:55Z",
"status": "True",
"type": "Initialized"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2020-08-25T09:15:59Z",
"status": "True",
"type": "Ready"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2020-08-25T09:15:59Z",
"status": "True",
"type": "ContainersReady"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2020-08-25T09:15:55Z",
"status": "True",
"type": "PodScheduled"
}
],
"containerStatuses": [
{
"containerID": "docker://0a4540282684c9dd282d13eba9a4ae44433f625e65ef40473cda7ca00b2ea73e",
"image": "redis:latest",
"imageID": "docker-pullable://redis@sha256:09c33840ec47815dc0351f1eca3befe741d7105b3e95bc8fdb9a7e4985b9e1e5",
"lastState": {},
"name": "redis",
"ready": true,
"restartCount": 0,
"started": true,
"state": {
"running": {
"startedAt": "2020-08-25T09:15:59Z"
}
}
}
],
"hostIP": "10.168.1.4",
"phase": "Running",
"podIP": "172.16.3.92",
"podIPs": [
{
"ip": "172.16.3.92"
}
],
"qosClass": "Burstable",
"startTime": "2020-08-25T09:15:55Z"
}
}
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath='kind is {.kind}'
kind is Pod
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath={@}
map[apiVersion:v1 kind:Pod metadata:map[annotations:map[tke.cloud.tencent.com/networks-status:[{
"name": "tke-bridge",
"ips": [
"172.16.3.92"
],
"default": true,
"dns": {}
}]] creationTimestamp:2020-08-25T09:15:55Z generateName:redis-5b4495ddb4- labels:map[k8s-app:redis pod-template-hash:5b4495ddb4 qcloud-app:redis] name:redis-5b4495ddb4-szjtz namespace:default ownerReferences:[map[apiVersion:apps/v1 blockOwnerDeletion:true controller:true kind:ReplicaSet name:redis-5b4495ddb4 uid:bef91151-d94e-46d5-a8a9-59a8f3ae634e]] resourceVersion:12100505479 selfLink:/api/v1/namespaces/default/pods/redis-5b4495ddb4-szjtz uid:968a56ba-80ed-41d3-ad27-deb7d060bfe7] spec:map[containers:[map[image:redis imagePullPolicy:Always name:redis resources:map[limits:map[cpu:500m memory:1Gi] requests:map[cpu:250m memory:256Mi]] securityContext:map[privileged:false] terminationMessagePath:/dev/termination-log terminationMessagePolicy:File volumeMounts:[map[mountPath:/var/run/secrets/kubernetes.io/serviceaccount name:default-token-cl2h8 readOnly:true]]]] dnsPolicy:ClusterFirst enableServiceLinks:true imagePullSecrets:[map[name:qcloudregistrykey]] nodeName:10.168.1.4 restartPolicy:Always schedulerName:default-scheduler securityContext:map[] serviceAccount:default serviceAccountName:default terminationGracePeriodSeconds:30 volumes:[map[name:default-token-cl2h8 secret:map[defaultMode:420 secretName:default-token-cl2h8]]]] status:map[conditions:[map[lastProbeTime: lastTransitionTime:2020-08-25T09:15:55Z status:True type:Initialized] map[lastProbeTime: lastTransitionTime:2020-08-25T09:15:59Z status:True type:Ready] map[lastProbeTime: lastTransitionTime:2020-08-25T09:15:59Z status:True type:ContainersReady] map[lastProbeTime: lastTransitionTime:2020-08-25T09:15:55Z status:True type:PodScheduled]] containerStatuses:[map[containerID:docker://0a4540282684c9dd282d13eba9a4ae44433f625e65ef40473cda7ca00b2ea73e image:redis:latest imageID:docker-pullable://redis@sha256:09c33840ec47815dc0351f1eca3befe741d7105b3e95bc8fdb9a7e4985b9e1e5 lastState:map[] name:redis ready:true restartCount:0 started:true state:map[running:map[startedAt:2020-08-25T09:15:59Z]]]] hostIP:10.168.1.4 phase:Running podIP:172.16.3.92 podIPs:[map[ip:172.16.3.92]] qosClass:Burstable startTime:2020-08-25T09:15:55Z]]
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath='{.apiVersion}'
v1
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath='{.metadata.name}'
redis-5b4495ddb4-szjtz
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath='{..name}'
qcloudregistrykey default-token-cl2h8 redis default-token-cl2h8 redis redis-5b4495ddb4-szjtz redis-5b4495ddb4
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath={.status.conditions[*].type}
Initialized Ready ContainersReady PodScheduled[
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath={.status.conditions[0].type}
Initialized
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath={.status.conditions[0:3:2].type}
Initialized ContainersReady
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath='{range .status.conditions[*]}[{..status}, {..type}]{end}'
[True, Initialized][True, Ready][True, ContainersReady][True, PodScheduled]
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath='{range .status.conditions[*]}[{..status}, {..type}]{"\n"}{end}'
[True, Initialized]
[True, Ready]
[True, ContainersReady]
[True, PodScheduled]
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath='{range .spec.containers[*].resources}[{..cpu}]{end}'
[500m 250m]
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath='{range .spec.containers[*].resources}[{..cpu}, {..memory}]{end}'
[500m 250m, 1Gi 256Mi]
prometheus的yaml文件只截取部分
[root@VM_1_4_centos ~]# kubectl get pod prometheus-7674d56d7f-dqxfx -n kube-ops -o json
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "prometheus-7674d56d7f-dqxfx",
"namespace": "kube-ops",
},
"spec": {
"containers": [
{
"args": [
"--config.file=/etc/alertmanager/config.yml",
"--storage.path=/alertmanager/data"
],
"image": "prom/alertmanager:v0.15.3",
"imagePullPolicy": "IfNotPresent",
"name": "alertmanager",
"ports": [
{
"containerPort": 9093,
"name": "http",
"protocol": "TCP"
}
],
.........
},
{
"command": [
"/bin/prometheus"
],
"image": "prom/prometheus:v2.4.3",
"imagePullPolicy": "IfNotPresent",
"name": "prometheus",
"ports": [
{
"containerPort": 9090,
"name": "http",
"protocol": "TCP"
}
}
.......
[root@VM_1_4_centos ~]# kubectl get pod prometheus-7674d56d7f-dqxfx -n kube-ops -o=jsonpath='{.spec.containers[?(@.name=="alertmanager")].ports}'
[map[containerPort:9093 name:http protocol:TCP]]
kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}[{.status.containerStatuses[0].containerID}, {.status.podIP}]{"\n"}{end}'
JSONPath 支持 | Kubernetes