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?
CodePudding user response:
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.
CodePudding user response:
You could also use 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. the label.
The tool prints out all logs of all pods/containers, matching against (partial) names, so you could also use 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.