英文:
Extract k8s events specific column
问题
I'm troubleshooting liveness probe failures. I'm able to extract specific entries from k8s event using this approach:
k get events --sort-by=.metadata.creationTimestamp | grep Liveness
I'd like to get only the pod(s) that causing the issue.
I'm considering to pipe a cut, but I'm not sure which delimiter should I use to get the specific column.
Where I can find the delimiter related to that specific k8s resource (Events) used to printout the kubectl output?
Any other suggestion is appreciated.
UPDATE: So far these are the best options (w/o using extra tools) satisfying my specific needs:
k get events -o jsonpath='{range .items[*]}{.involvedObject.name}/{.involvedObject.namespace}: {.message}{"\n"}{end}' | grep Liveness
k get events -o custom-columns=POD:.involvedObject.name,NS:.involvedObject.namespace,MGG:.message | grep Liveness
英文:
I'm troubleshooting liveness probe failures. I'm able to extract specific entries from k8s event using this approach
k get events --sort-by=.metadata.creationTimestamp | grep Liveness
I'd like to get only the pod(s) that causing the issue.
I'm considering to pipe a cut, but I'm not sure which delimiter should I use to get the specific column.
Where I can find the delimiter related to that specific k8s resource(Events) used to printout the kubectl output?
Any other suggestion is appreciated
UPDATE
so far these are the best options (w/o using extra tools) satisfying my specific needs:
k get events -o jsonpath='{range .items[*]}{.involvedObject.name}/{.involvedObject.namespace}: {.message}{"\n"}{end}' | grep Liveness
k get events -o custom-columns=POD:.involvedObject.name,NS:.involvedObject.namespace,MGG:.message | grep Liveness
答案1
得分: 2
在Kubernetes中有一个名为 jsonpath 的特性。
使用此在线工具验证您的jsonpath是否正确: https://jsonpath.com/
使用此在线工具轻松浏览JSON键,无需手动输入键名): http://jsonpathfinder.com/
因此,您的命令将是:
k get events --sort-by=.metadata.creationTimestamp --jsonpath '{ .xxxxxx }'
英文:
there is a feature in kubernetes called jsonpath
validate if your jsonpath is correct with this online tool: https://jsonpath.com/
easily go through json keys with this online tool, so you needn't manually type the key names any more): http://jsonpathfinder.com/
so your command will be:
k get events --sort-by=.metadata.creationTimestamp --jsonpath '{ .xxxxxx }'
答案2
得分: 1
Jsonpath在与筛选器和条件一起使用时有一些限制,也许对于您的情况来说,jq
更合适。
我使用jq
进行了一次测试,以过滤我的探针的输出:
我已经测试过使用来自此链接的yaml文件。
此Pod的探针失败消息是:
存活性探针失败:cat: 无法打开'/tmp/healthy':没有那个文件或目录
此消息在JSON中的路径是.items[*].message
使用jq
,我可以仅过滤包含"Liveness probe failed"的消息,并显示Pod名称:
k get events --sort-by=.metadata.creationTimestamp -o json | jq -c '.items[] | select(.message|test("Liveness probe failed")) | .metadata.name'
输出是:
"liveness-exec.15e791c17b80a3c1"
您可以使用jq
来格式化消息,以获得更有帮助的输出,包含Pod的详细信息。
尝试查看这些参考链接:
希望对您有所帮助!
英文:
Jsonpath is a little bit limited to be used with filter and conditions, maybe for your case jq
will be more suitable.
I did a test using jq
to filter the Output of my probe:
I've tested using the yaml from this link
The message of probe failure from this pod is:
> Liveness probe failed: cat: can't open '/tmp/healthy': No such file or directory
and the path for this message in json is .items[*].message
Using jq
I can filter only message
that contains "Liveness probe failed": and show the pod name:
k get events --sort-by=.metadata.creationTimestamp -o json | jq -c '.items[] | select(.message|test("Liveness probe failed")) | .metadata.name'
The output is:
"liveness-exec.15e791c17b80a3c1"
You can use jq
to format the message in order to get a more helpful output, with pod details.
Try to look this references links:
I hope it helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论