简体   繁体   English

如何让 kubectl 日志与日志一起输出 pod 名称?

[英]How to get kubectl logs to output pod name alongside logs?

I'm using the handy kubectl logs -l label=value command to get log from all my pods matching a label.我正在使用方便的kubectl logs -l label=value命令从与标签匹配的所有 pod 中获取日志。 I want to see which pod outputted what log, but only the log text is displayed.我想看看哪个pod输出了什么日志,但是只显示日志文本。 Is there a way to control the log format, or a command argument which will let me do this?有没有办法控制日志格式,或者让我这样做的命令参数?

使用很棒的kubetail脚本

我使用 stern 显示来自所有 pod https://github.com/wercker/stern 的日志。

kubectl现在有一个--prefix选项,允许您在日志消息之前添加 pod 名称的前缀。

As simple as this:就这么简单:

for pod in $(kubectl get po -l key=value -oname); do echo $pod; kubectl logs $pod; done;

this will fetch the names of the pods by their label, then will one by one print the logs after writing the name of the pod.这将通过标签获取 pod 的名称,然后将在写入 pod 名称后一一打印日志。 So, it will look like something like this:所以,它看起来像这样:

pod1
log
log
log
pod2
log
log
log
...
podn
log
log
log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM