What kubectl command can I use to get events sorted by specific fields and print only specific details of events?

7/20/2017

I need to print only specific fields of Kubernetes Events, sorted by a specific field.

This is to help me gather telemetry and analytics about my namespace

How could I do that?

-- suryakrupa
go-templates
kubectl
kubernetes

3 Answers

7/20/2017

Following command does it.

It prints the events sorted by timestamp of creation.

It also users go-template to filter out specific fields of the kubernetes-event object.

kubectl get events  --sort-by='.metadata.creationTimestamp'  -o 'go-template={{range .items}}{{.involvedObject.name}}{{"\t"}}{{.involvedObject.kind}}{{"\t"}}{{.message}}{{"\t"}}{{.reason}}{{"\t"}}{{.type}}{{"\t"}}{{.firstTimestamp}}{{"\n"}}{{end}}'
-- suryakrupa
Source: StackOverflow

4/25/2019

kubectl get events --sort-by='.lastTimestamp'

-- ehbello
Source: StackOverflow

6/11/2018

If you don't mind seeing the output as JSON:

kubectl get event -o json | jq '.items |= sort_by(.lastTimestamp)'

This requires jq.

-- Chris Stryczynski
Source: StackOverflow