Sub topic 7
Sorting and filtering events
This chapter assumes you can run kubectl get and kubectl describe. It also assumes you know that an event is a short record the cluster creates about an object, such as a Pod being scheduled or an image pull failing. Here you learn to find the events that matter in a busy namespace.
Why raw event output is hard to use
kubectl get events returns every event in the current namespace. The default order is not guaranteed to be chronological. On a busy cluster you get hundreds of lines about many objects, and the line you need is buried in them.
Two techniques fix this: sort the list by time, and filter it down to one object or one type.
Sorting by time
Use --sort-by with a field path. Sorting on .metadata.creationTimestamp puts the oldest events first and the newest last. The newest events are the ones next to your prompt.
kubectl get events --sort-by=.metadata.creationTimestampThe creationTimestamp is when the event object was first created. An event that has been repeated many times keeps its original creation time. Its LAST SEEN column still shows the most recent occurrence, so the sort order and the LAST SEEN column can disagree for repeating events.
Filtering with field selectors
A field selector keeps only the events whose field has a given value. Two fields cover most needs:
involvedObject.nameis the name of the object the event is about.typeis eitherNormalorWarning.
kubectl get events --field-selector involvedObject.name=web-0
kubectl get events -A --field-selector type=WarningThe first command shows events for the object named web-0 in the current namespace. The second shows only Warning events in every namespace, because -A is short for --all-namespaces.
You can combine selectors with a comma, which means AND. You can also combine a selector with sorting:
kubectl get events --field-selector involvedObject.kind=Pod,type=Warning --sort-by=.metadata.creationTimestampThe kubectl events command
Recent kubectl versions include kubectl events. It sorts by time by default, so you do not need --sort-by. The --for flag restricts the output to one object:
kubectl events --for pod/web-0If the command is not found, your kubectl is older. Use get events with selectors instead.
Choosing a method
| Goal | Best command |
|---|---|
| Everything recent in a namespace, in order | `kubectl events` or `get events --sort-by` |
| History of one object | `kubectl events --for pod/web-0` |
| Problems anywhere in the cluster | `get events -A --field-selector type=Warning` |
| Events plus status and conditions | `kubectl describe pod web-0` |
kubectl describe prints the same events in its Events section at the bottom, which is convenient when you also want the object's status.
Common mistakes
- Wrong namespace. Events live in the same namespace as their object. If you see nothing, add
-n <namespace>or-A. - Misspelled selector fields. Only some fields are supported. An unsupported field returns an error, so check the message.
- Reading old events as current. Events older than the retention window are gone, and a repeated event may look older than it is. The last chapters of this tutorial cover this.
Exercises
Exercise
Write a command that shows only Warning events for the Pod web-0 in the current namespace, sorted by time.
Show answer
kubectl get events --field-selector involvedObject.name=web-0,type=Warning --sort-by=.metadata.creationTimestamp
Exercise
You run kubectl get events and see nothing, but the Pod is failing in the prod namespace. What is the likely cause and fix?
Show answer
Events are namespaced and you queried the current namespace, which is not prod. Run the command with -n prod, or use -A.