kuberneteskubectlkubernetes-jobs

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:

kubectl logs job/migration --follow

However, since it's a Job, the pod is expected to run, complete and then terminate. 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.

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.

My question is, is it possible to construct a kubctl logs ... command such that I can follow the logs of future migration pods? 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?


Solution

  • I think the solution is to use 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.

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

    Refinements welcome.