kubernetes: kubectl get events doesnt sort events by last seen time.
Here is one example see below:-
LASTSEEN FIRSTSEEN COUNT NAME KIND SUBOBJECT TYPE REASON SOURCE MESSAGE
13m 13m 1 nginx-deployment-3392909933-1tfwp Pod Normal Scheduled {default-scheduler } Successfully assigned nginx-deployment-3392909933-1tfwp to kubernetes-minion-group-kcbf
13m 13m 1 nginx-deployment-3392909933-1tfwp Pod spec.containers{nginx} Normal Pulling {kubelet kubernetes-minion-group-kcbf} pulling image "nginx"
13m 13m 1 nginx-deployment-3392909933-1tfwp Pod spec.containers{nginx} Normal Pulled {kubelet kubernetes-minion-group-kcbf} Successfully pulled image "nginx"
13m 13m 1 nginx-deployment-3392909933-1tfwp Pod spec.containers{nginx} Normal Created {kubelet kubernetes-minion-group-kcbf} Created container with docker id 0f5fc6ae789f
13m 13m 1 nginx-deployment-3392909933-1tfwp Pod spec.containers{nginx} Normal Started {kubelet kubernetes-minion-group-kcbf} Started container with docker id 0f5fc6ae789f
13m 13m 1 nginx-deployment-3392909933-3ct1t Pod Normal Scheduled {default-scheduler } Successfully assigned nginx-deployment-3392909933-3ct1t to kubernetes-minion-group-9q61
13m 13m 1 nginx-deployment-3392909933-3ct1t Pod spec.containers{nginx} Normal Pulling {kubelet kubernetes-minion-group-9q61} pulling image "nginx"
13m 13m 1 nginx-deployment-3392909933-3ct1t Pod spec.containers{nginx} Normal Pulled {kubelet kubernetes-minion-group-9q61} Successfully pulled image "nginx"
13m 13m 1 nginx-deployment-3392909933-3ct1t Pod spec.containers{nginx} Normal Created {kubelet kubernetes-minion-group-9q61} Created container with docker id 291683894adc
13m 13m 1 nginx-deployment-3392909933-3ct1t Pod spec.containers{nginx} Normal Started {kubelet kubernetes-minion-group-9q61} Started container with docker id 291683894adc
13m 13m 1 nginx-deployment-3392909933-d70zp Pod Normal Scheduled {default-scheduler } Successfully assigned nginx-deployment-3392909933-d70zp to kubernetes-minion-group-5bdr
13m 13m 1 nginx-deployment-3392909933-d70zp Pod spec.containers{nginx} Normal Pulling {kubelet kubernetes-minion-group-5bdr} pulling image "nginx"
13m 13m 1 nginx-deployment-3392909933-d70zp Pod spec.containers{nginx} Normal Pulled {kubelet kubernetes-minion-group-5bdr} Successfully pulled image "nginx"
13m 13m 1 nginx-deployment-3392909933-d70zp Pod spec.containers{nginx} Normal Created {kubelet kubernetes-minion-group-5bdr} Created container with docker id 823f95040680
13m 13m 1 nginx-deployment-3392909933-d70zp Pod spec.containers{nginx} Normal Started {kubelet kubernetes-minion-group-5bdr} Started container with docker id 823f95040680
13m 13m 1 nginx-deployment-3392909933 ReplicaSet Normal SuccessfulCreate {replicaset-controller } Created pod: nginx-deployment-3392909933-1tfwp
13m 13m 1 nginx-deployment-3392909933 ReplicaSet Normal SuccessfulCreate {replicaset-controller } Created pod: nginx-deployment-3392909933-3ct1t
13m 13m 1 nginx-deployment-3392909933 ReplicaSet Normal SuccessfulCreate {replicaset-controller } Created pod: nginx-deployment-3392909933-d70zp
13m 13m 1 nginx-deployment Deployment Normal ScalingReplicaSet {deployment-controller } Scaled up replica set nginx-deployment-3392909933 to 3
8m 8m 1 secret-env-pod Pod Normal Scheduled {default-scheduler } Successfully assigned secret-env-pod to kubernetes-minion-group-5bdr
8m 8m 1 secret-env-pod Pod spec.containers{test-container} Normal Pulling {kubelet kubernetes-minion-group-5bdr} pulling image "gcr.io/google_containers/busybox"
8m 8m 1 secret-env-pod Pod spec.containers{test-container} Normal Pulled {kubelet kubernetes-minion-group-5bdr} Successfully pulled image "gcr.io/google_containers/busybox"
8m 8m 1 secret-env-pod Pod spec.containers{test-container} Normal Created {kubelet kubernetes-minion-group-5bdr} Created container with docker id 06390f89067f
8m 8m 1 secret-env-pod Pod spec.containers{test-container} Normal Started {kubelet kubernetes-minion-group-5bdr} Started container with docker id 06390f89067f
10m 10m 1 secret-test-pod Pod Normal Scheduled {default-scheduler } Successfully assigned secret-test-pod to kubernetes-minion-group-5bdr
10m 10m 1 secret-test-pod Pod spec.containers{test-container} Normal Pulling {kubelet kubernetes-minion-group-5bdr} pulling image "kubernetes/mounttest:0.1"
10m 10m 1 secret-test-pod Pod spec.containers{test-container} Normal Pulled {kubelet kubernetes-minion-group-5bdr} Successfully pulled image "kubernetes/mounttest:0.1"
10m 10m 1 secret-test-pod Pod spec.containers{test-container} Normal Created {kubelet kubernetes-minion-group-5bdr} Created container with docker id c865b528a5ea
10m 10m 1 secret-test-pod Pod spec.containers{test-container} Normal Started {kubelet kubernetes-minion-group-5bdr} Started container with docker id c865b528a5ea
After 13m, it should be 10m and then 8m. This feature would allow us to see whats wrong easily. I have had other examples where its all mixed up and its not easy to see where your just deployed pod is failing. Tested on 1.3.0
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 90
- Comments: 62 (17 by maintainers)
Newbie k8s user here, was wondering why running
kubectl get events
doesn’t sort events by time, definitely confused me a bit at first.Looks like
kubectl get events --sort-by='.metadata.creationTimestamp'
does the trick though (posting this for others who may run into this issue)—unless I’m missing something, sorta seems that should be the default though 😃This is still happening, is there any timeline to fix this?
Related: https://github.com/kubernetes/kubernetes/issues/36304#issuecomment-581864033
Sometimes (quite often) there’s an event with
"lastTimestamp": null
(see kubectl get event -o json).This causes
kubectl get event --sort-by=lastTimestamp
to fail:sorter.go:354] Field {.lastTimestamp} in [][][]reflect.Value is an unsortable type: interface, err: unsortable interface: interface
Still an issue and still confusing to the user. I’d love to see a fix for this!
kubectl get events --sort-by='.lastTimestamp'
is doing the trick for me.Sort by lastTimestamp works on my cluster, but why it’s not the default? Isn’t it the most meaningful order?
This is still driving me nuts everytime I’m investigating a failing pod, so I’d like to keep this open.
Seriously?
Looking at a history with a random order of the event isn’t an issue for you @llech ?
@PaulCapestany I think you should sort by
.lastTimestamp
rather than.metadata.creationTimestamp
in case there was a compaction (duplicate events):Agree that this is very confusing.
@AdoHe @bgrant0607 @lavalamp @pwittrock Here are my findings so far using printf debugging:-
1: sorting of events across different resources can be achieved using the command
kubectl get events --sort-by='{.lastTimestamp}'
otherwise they are all mixed up2: doing kubectl get events would show up events in mixed order across all resources(tested with pods). This should show events in sorted order by default, since when events from different resources are shown, the user is only interested in the latest events or events in chronological order. Per resource events are anyways shown in
kubectl describe
3:
k8s.io/kubernetes/pkg/kubectl/resource_printer.go
has handlers for different kinds of objectsbut when i tried to print the stack trace, i only see printEvent being called, and printEventList is not used at all, even though i see the kubectl getting a response with api.EventList object.
Here is a sample stack trace for
kubectl get events
Here is the corresponding kubectl http response with -v=10 debugging ,
4: I saw similar behavior with kubectl get pods, where although there are handlers called printPodList, they are never called.
5: the printEventList handler is never called and that is where the sorting was added as part of this PR https://github.com/kubernetes/kubernetes/issues/2948 I think that PR only solved it for kubectl describe pods, but did not solve it fot kubectl get events or i am still missing something.
6: Someone more familiar with the framework can confirm how these printXXXList functions are supposed to be used. But for sure in the kubectl get events path, the printEventList is not being called.
7: k8s.io/kubernetes/pkg/kubectl/cmd/get.go as logic which figures if the object is ListType only for watches, but for regular flow, it breaks down the response into individual objs and then calls PrintObj on each obj(rather than calling it on the uber EventList obj), which ensures that the corresponding handlers for api.EventList will never be called even though the response was api.EventList.
The new command
kubectl alpha events
is now out in Kubernetes 1.23 - give it a try! If you have any feedback or thoughts on how it could evolve, please raise a new issue and tag me.There’s still some weird behavior that happens with this.
It will work initially, but every so often it will print an event that occurred days ago.
Upon further research, this works:
However, this is not sorted, and old events are randomly printed as if they just happened:
@zedtux
I think you misread @llech’s comment - they are arguing in favor of events being time ordered.
Yes, imo this is a bugfix and not a feature request.
One that has been open for multiple years 😦
--sort-by='{.lastTimestamp}'
doesn’t seem to sort correctly :you need to first do a
kubectl get events -o=json
to see what fields you can use for it. in some caseslastTimestamp
for example is being included in metadata and is empty or duplicated (i think helm includes it in some “managedFields”). You need to find the proper time field that can be used for sort and use that one. as example, in some projects i useget event --sort-by='.lastTimestamp'
and in others i useget event --sort-by='.metadata.creationTimestamp'
While waiting for the new
oc events
command, I came up with the following:You can use it like:
oc-events -A
,oc events -n foo
, etc.As of Kubernetes 1.18 all new objects have metadata for server-side apply, which gives us a new way to sort events:
Kudos to @stefanprodan who told me about this.
I’ve seen this with events from kube-scheduler - they have
eventTimestamp
set but notfirstTimestamp
orlastTimestamp
.And there doesn’t seem to be a way to tell jsonpath to take one field or fall back to another.
The
kubectl get event --sort-by .lastTimestamp
fails for me but this command works:Does not work for me on Gke with version v1.18.15-gke.1500 😦
/lifecycle frozen
@krmayankk This issue is now assigned to you.