如何使用kubectl通过jsonpath获取输出



我运行kubectl get events来获取事件详细信息,现在我想进行模糊搜索以获取前缀为nginx-*的特定pod

假设,我的输出如下

$ kubectl get events -o json
{
"apiVersion": "v1",
"items": [
{
"apiVersion": "v1",
"count": 1,
"eventTime": null,
"firstTimestamp": "2020-03-12T06:18:58Z",
"involvedObject": {
"apiVersion": "v1",
"kind": "Pod",
"name": "nginx-6db489d4b7-99xmd",
"namespace": "default",
"resourceVersion": "9683",
"uid": "64f6eeb1-c267-4ee1-b34d-14e65573d63f"
},
"kind": "Event",
"lastTimestamp": "2020-03-12T06:18:58Z",
"message": "Successfully assigned default/nginx-6db489d4b7-99xmd to kind-worker3",
"metadata": {
"creationTimestamp": "2020-03-12T06:18:58Z",
"name": "nginx-6db489d4b7-99xmd.15fb7a182197a184",
"namespace": "default",
"resourceVersion": "9703",
"selfLink": "/api/v1/namespaces/default/events/nginx-6db489d4b7-99xmd.15fb7a182197a184",
"uid": "de0ff737-e4d6-4218-b441-26c68a1ee8bd"
},
"reason": "Scheduled",
"reportingComponent": "",
"reportingInstance": "",
"source": {
"component": "default-scheduler"
},
"type": "Normal"
},
{
"apiVersion": "v1",
"count": 1,
"eventTime": null,
"firstTimestamp": "2020-03-12T06:18:59Z",
"involvedObject": {
"apiVersion": "v1",
"fieldPath": "spec.containers{nginx}",
"kind": "Pod",
"name": "nginx-6db489d4b7-99xmd",
"namespace": "default",
"resourceVersion": "9693",
"uid": "64f6eeb1-c267-4ee1-b34d-14e65573d63f"
},
"kind": "Event",
"lastTimestamp": "2020-03-12T06:18:59Z",
"message": "Pulling image "nginx"",
"metadata": {
"creationTimestamp": "2020-03-12T06:18:59Z",
"name": "nginx-6db489d4b7-99xmd.15fb7a18754d0bfc",
"namespace": "default",
"resourceVersion": "9709",
"selfLink": "/api/v1/namespaces/default/events/nginx-6db489d4b7-99xmd.15fb7a18754d0bfc",
"uid": "d541f134-5e9c-4b7f-b035-ae4d49a3745f"
},
"reason": "Pulling",
"reportingComponent": "",
"reportingInstance": "",
"source": {
"component": "kubelet",
"host": "kind-worker3"
},
"type": "Normal"
},
{
"apiVersion": "v1",
"count": 1,
"eventTime": null,
"firstTimestamp": "2020-03-12T06:18:26Z",
"involvedObject": {
"apiVersion": "v1",
"fieldPath": "spec.containers{nginx}",
"kind": "Pod",
"name": "nginx",
"namespace": "default",
"resourceVersion": "9555",
"uid": "f9d0ae86-4d7d-4553-91c2-efc0c3f8144f"
},
"kind": "Event",
"lastTimestamp": "2020-03-12T06:18:26Z",
"message": "Pulling image "nginx"",
"metadata": {
"creationTimestamp": "2020-03-12T06:18:26Z",
"name": "nginx.15fb7a10b4975ae0",
"namespace": "default",
"resourceVersion": "9565",
"selfLink": "/api/v1/namespaces/default/events/nginx.15fb7a10b4975ae0",
"uid": "f66cf712-1284-4f65-895a-5fbfa974e317"
},
"reason": "Pulling",
"reportingComponent": "",
"reportingInstance": "",
"source": {
"component": "kubelet",
"host": "kind-worker"
},
"type": "Normal"
},
{
"apiVersion": "v1",
"count": 1,
"eventTime": null,
"firstTimestamp": "2020-03-12T06:18:38Z",
"involvedObject": {
"apiVersion": "v1",
"fieldPath": "spec.containers{nginx}",
"kind": "Pod",
"name": "nginx",
"namespace": "default",
"resourceVersion": "9555",
"uid": "f9d0ae86-4d7d-4553-91c2-efc0c3f8144f"
},
"kind": "Event",
"lastTimestamp": "2020-03-12T06:18:38Z",
"message": "Successfully pulled image "nginx"",
"metadata": {
"creationTimestamp": "2020-03-12T06:18:38Z",
"name": "nginx.15fb7a13a4aed9fc",
"namespace": "default",
"resourceVersion": "9613",
"selfLink": "/api/v1/namespaces/default/events/nginx.15fb7a13a4aed9fc",
"uid": "55a4a512-d5c0-41da-ae9c-c1654b6bbdfe"
},
"reason": "Pulled",
"reportingComponent": "",
"reportingInstance": "",
"source": {
"component": "kubelet",
"host": "kind-worker"
},
"type": "Normal"
}
],
"kind": "List",
"metadata": {
"resourceVersion": "",
"selfLink": ""
}
}

我只想从吊舱nginx-*获得消息。

$ kubectl get events -o=jsonpath='{.items[*].involvedObject}'

但我不知道如何检查名称是否为nginx-*,然后导出其消息

"involvedObject": {
"apiVersion": "v1",
"kind": "Pod",
"name": "nginx-6db489d4b7-99xmd",
"namespace": "default",
"resourceVersion": "9683",
"uid": "64f6eeb1-c267-4ee1-b34d-14e65573d63f"
},
"message": "Successfully assigned default/nginx-6db489d4b7-99xmd to kind-worker3",

Kuectl jsonpath实现不支持regexp匹配,因此仅使用此工具是不可能实现的(看看这个github问题(。幸运的是,您总是可以使用jq来过滤事件,请看下面的示例。

kubectl get events -ojson | jq '.items[] | select(.involvedObject.name | test("^ngin-")) | .message'

相关内容

  • 没有找到相关文章

最新更新