简体   繁体   English

在 kubernetes 如何跟踪未来 Job pod 的日志?

[英]In kubernetes how can I tail the logs of future Job pods?

I have a Job which creates a pod and I can tail its logs like this:我有一个创建 pod 的作业,我可以像这样跟踪它的日志:

kubectl logs job/migration --follow

However, since it's a Job, the pod is expected to run, complete and then terminate.但是,由于它是一个作业,因此 pod 应该会运行、完成然后终止。 If I run the above command, I can see the logs for the last run of the pod but kubectl exits after printing out all of the logs, presumably because all of the pods its attempting to follow are terminated.如果我运行上面的命令,我可以看到最后一次运行 pod 的日志,但是kubectl在打印出所有日志后退出,大概是因为它试图跟随的所有 pod 都被终止了。

My Job has this annotation on it:我的工作上有这个注释:

  annotations:
    helm.sh/hook: post-install,post-upgrade

Such that when I run helm install... it will cause the Job to create another pod, which again runs and terminates as expected.这样当我运行helm install...时,它将导致 Job 创建另一个 pod,该 pod 再次按预期运行和终止。

My question is, is it possible to construct a kubctl logs... command such that I can follow the logs of future migration pods?我的问题是,是否可以构建一个kubctl logs...命令以便我可以跟踪未来迁移 pod 的日志? Such that I could have the command tailing in one terminal and then run helm install in another and then I would see the logs of the new pod in my terminal immediately?这样我就可以在一个终端中执行命令,然后在另一个终端中运行helm install ,然后我会立即在终端中看到新 pod 的日志?

I think the solution is to use xargs .我认为解决方案是使用xargs

To accomplish this we need to add -o name to get just the names of the existing pods and the new pods, then pipe it into xargs and follow the logs of those as they happen.为此,我们需要添加-o name以仅获取现有 pod 和新 pod 的名称,然后将 pipe 添加到 xargs 中,并在它们发生时跟踪它们的日志。

kubectl get pods -n example -l component=migration --watch -o name \
  | xargs kubectl logs -n example --follow

Refinements welcome.欢迎细化。

You could also use stern .您也可以使用stern

stern --namespace example migration or stern --namespace -l component=migration will show all logs of all present and future pods that have the word 'migration' in their name resp. stern --namespace example migrationstern --namespace -l component=migration将显示所有当前和未来名称中包含“migration”一词的 Pod 的所有日志。 the label. label。

The tool prints out all logs of all pods/containers, matching against (partial) names, so you could also use stern --namespace example mig .该工具打印出所有 pod/容器的所有日志,与(部分)名称匹配,因此您也可以使用stern --namespace example mig You can also include/exclude certain containers, run against all namespaces and get pretty good coloring schema to distinguish different pods/containers.您还可以包含/排除某些容器,针对所有命名空间运行并获得非常好的着色模式来区分不同的 pod/容器。

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

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