1. jsonpath の構文の概要
Kubectl は JSONPath テンプレートをサポートしています。
JSONPath テンプレートは、{} で囲まれた JSONPath 式で構成されます。Kubectl は、JSONPath 式を使用して、JSON オブジェクト内の特定のフィールドをフィルターし、出力をフォーマットします。元の JSONPath テンプレート構文に加えて、次の関数と構文も有効です。
- JSONPath 式内のテキストを二重引用符で囲むには、二重引用符を使用します。
range
,演算子を使用してend
リストを反復処理します。- 負のインデックスを使用してリストを戻します。負のインデックスはリストを「ラップアラウンド」せず、 で
-index + listLength> = 0
ある限り有効です。
関数 |
説明 |
例 |
結果 |
---|---|---|---|
文章 |
プレーンテキスト |
種類は {.kind} です |
種類はリストです |
@ |
現在のオブジェクト |
{@} |
入力と同じ |
。また [] |
子オペレーター |
{.kind} または {['kind']} |
リスト |
.. |
再帰降下 |
{..名前} |
127.0.0.1 127.0.0.2 自分 e2e |
* |
ワイルドカード。すべてのオブジェクトを取得する |
{.items[*].metadata.name} |
[127.0.0.1 127.0.0.2] |
[開始:終了:ステップ] |
添字演算子 |
{.users[0].name} |
自分自身 |
[、] |
共用体演算子 |
{.items[*]['metadata.name', 'status.capacity']} |
127.0.0.1 127.0.0.2 マップ[cpu:4] マップ[cpu:8] |
?() |
フィルター |
{.users[?(@.name=="e2e")].user.password} |
ひみつ |
範囲、終了 |
反復リスト |
{範囲 .items[*]}[{.metadata.name}, {.status.capacity}] {end} |
[127.0.0.1、マップ[cpu:4]] [127.0.0.2、マップ[cpu:8]] |
」 |
解釈された実行文字列を引用符で囲む |
{範囲 .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"
}
}
2. jsonpath経由で情報を取得する例
2.1 プレーンテキストモード
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath='kind is {.kind}'
kind is Pod
2.2 現在のオブジェクトを取得する
[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:<nil> lastTransitionTime:2020-08-25T09:15:55Z status:True type:Initialized] map[lastProbeTime:<nil> lastTransitionTime:2020-08-25T09:15:59Z status:True type:Ready] map[lastProbeTime:<nil> lastTransitionTime:2020-08-25T09:15:59Z status:True type:ContainersReady] map[lastProbeTime:<nil> 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]]
2.3 ポッドの API バージョンを取得する
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath='{.apiVersion}'
v1
2.4 ポッドの名前を取得する
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath='{.metadata.name}'
redis-5b4495ddb4-szjtz
2.5 yaml のすべての名前を再帰的に取得する
[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
2.6 すべての状態条件の種類を取得する
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath={.status.conditions[*].type}
Initialized Ready ContainersReady PodScheduled[
2.7 ステータスの最初の条件の種類を取得する
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath={.status.conditions[0].type}
Initialized
2.8 最初の状態条件から最後の状態条件の終わりまで、2 つごとを取得します。
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath={.status.conditions[0:3:2].type}
Initialized ContainersReady
2.8 ステータスを取得し、ステータス条件を入力する
[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]
2.9 スペースと改行の引用符
[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]
2.10 リソースの CPU 値を取得する
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath='{range .spec.containers[*].resources}[{..cpu}]{end}'
[500m 250m]
2.11 リソース内の CPU とメモリを取得する
[root@VM_1_4_centos ~]# kubectl get pod redis-5b4495ddb4-szjtz -o=jsonpath='{range .spec.containers[*].resources}[{..cpu}, {..memory}]{end}'
[500m 250m, 1Gi 256Mi]
2.22 promotheusのポッドでalertmanageのコンテナポートを取得する
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]]
2.23 すべてのコンテナIDとポッドIPを取得する
kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}[{.status.containerStatuses[0].containerID}, {.status.podIP}]{"\n"}{end}'