kubectl icon indicating copy to clipboard operation
kubectl copied to clipboard

Make `kubectl log deployment/foo` read logs from **all** pods

Open guettli opened this issue 1 year ago • 14 comments

What would you like to be added:

Up to now, kubectl log deployment/foo outputs the logs of one pod:

❯ kubectl logs deployments/foo --follow >> foo.log
Found 2 pods, using pod/foo-647ffff4b6-k9c5c

It would be very handy if kubectl could output the logs of all pods of the deployment.

Why is this needed:

The workaround is to use label selectors:

kubectl logs -l app=myapp

But that's not easily automated.

Imagine you get the name and the namespace of a deployment in a script.

There is no easy and automated way to get the matching label selector.

It would be great if kubectl could support reading the logs from all pods of a deployment.

Related thread on slack: https://kubernetes.slack.com/archives/C2GL57FJ4/p1700558613585049?thread_ts=1700558613.585049&cid=C2GL57FJ4

guettli avatar Nov 21 '23 11:11 guettli

/assign

ankritisachan avatar Nov 21 '23 16:11 ankritisachan

@ankritisachan is the best person to work on this feature, thanks for taking this!

apelisse avatar Nov 21 '23 17:11 apelisse

kubectl logs --help is your friend, you'll find there:

...
Options:
    --all-containers=false:
	Get all containers' logs in the pod(s).
...

That flag should do what you're looking for.

soltysh avatar Nov 29 '23 16:11 soltysh

Misread the bit about --all-pods. I had a short conversation about this topic on slack, we'll try to cover this one next week during SIG-CLI bug scrub.

soltysh avatar Nov 29 '23 16:11 soltysh

/triage accepted

mpuckett159 avatar Dec 06 '23 20:12 mpuckett159

I needed this feature a couple of times and support the implementation. (But we probably shouldn't change the default behavior and manage this via new flag, WDYT?)

ardaguclu avatar Dec 07 '23 09:12 ardaguclu

What would the behavior be while the kubectl logs command is running, and new pods spin up (or go away)? Would these be noted explicitly? Would that match the user expectation? Third-party tools like https://github.com/stern/stern or https://github.com/boz/kail support this scenario to some extent.

ahmetb avatar Dec 08 '23 04:12 ahmetb

@ahmetb good question. I would like to see the logs of the new pods, too.

guettli avatar Dec 11 '23 08:12 guettli

Maybe have the source pod name appended to the log line so it's obvious when pods come and go.

mpuckett159 avatar Dec 11 '23 09:12 mpuckett159

/assign

geektype avatar Dec 15 '23 22:12 geektype

@cmwylie19 has a PR ready to ship soon for this.

eddiezane avatar Mar 11 '24 20:03 eddiezane

@cmwylie19 has a PR ready to ship soon for this.

/assign

cmwylie19 avatar Mar 11 '24 20:03 cmwylie19

@ahmetb to answer your question I think that functionality should be coupled to --follow instead of us adding a --all-pods here. Should be a separate issue if we decide to change the functionality for new pods/replicas/etc.

Thoughts?

eddiezane avatar Mar 11 '24 20:03 eddiezane

WIP - https://github.com/kubernetes/kubernetes/pull/123874

cmwylie19 avatar Mar 11 '24 22:03 cmwylie19