Q: How do you get events for a specific namespace sorted by time?
To get events for a specific namespace sorted by time, run kubectl get events -n prod --sort-by=.lastTimestamp, or use the newer kubectl events -n prod, which sorts chronologically by default.
Want to master this scenario in a live sandbox? KodeKloud's CKA & CKAD Hands-On Certification Track covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: To <strong>get events for a specific namespace sorted by time</strong>, run <code>kubectl get events -n prod --sort-by=.lastTimestamp</code>, or use the newer <code>kubectl events -n prod</code>, which sorts chronologically by default. Plain <code>kubectl get events</code> returns results in no guaranteed time order, which makes incident timelines confusing. Interviewers ask this to see whether you can reconstruct what happened from events and know their limits: they expire quickly and are not a durable log.
Mental model: events are short-lived hints
Events are API objects emitted by controllers and the kubelet, describing things like scheduling, image pulls and probe failures.
- Type:
NormalorWarning. Warnings are where you usually start. - Involved object: each event references a pod, node, ReplicaSet or other object, and has a reason and message.
- Retention: the API server keeps events for one hour by default (
--event-ttl). After that they are gone, so export them if you need history. - Deduplication: repeated events are aggregated with a count, so
lastTimestampreflects the latest occurrence. - Why sorting matters: an incident is a sequence, such as scheduled, pulled, started, failed probe, killed. Without time ordering the cause and effect are scrambled.
Hands-on: get events for a namespace sorted by time
Choose the command that fits your cluster version and your need.
kubectl get events -n prod --sort-by=.lastTimestamp
kubectl get events -n prod --sort-by=.metadata.creationTimestamp
kubectl events -n prod
kubectl events -n prod --types=Warning
- Classic:
kubectl get events -n prod --sort-by=.lastTimestamp. Oldest first, newest last, so the most recent lines are at the bottom of the terminal. - Alternative field:
--sort-by=.metadata.creationTimestampworks when events from the newer events.k8s.io API leavelastTimestampempty. - Dedicated command:
kubectl events -n prod(available in kubectl 1.26 and newer) is sorted by default and supports--for,--typesand--watch. - All namespaces: add
-A. - Reading the order: with ascending sort the newest event is the last line, so read upwards from the bottom to find the most recent change.
- Include the namespace explicitly: always pass
-nin runbooks rather than relying on your current context, which avoids reading the wrong cluster namespace during an incident.
Filter, watch and focus
Cut the noise down to what matters.
kubectl get events -n prod --field-selector type=Warning --sort-by=.lastTimestamp
kubectl get events -n prod --field-selector involvedObject.kind=Pod,reason=BackOff
kubectl get events -n prod --sort-by=.lastTimestamp \
-o custom-columns=TIME:.lastTimestamp,TYPE:.type,REASON:.reason,OBJECT:.involvedObject.name,MSG:.message
- Warnings only:
kubectl get events -n prod --field-selector type=Warning --sort-by=.lastTimestamp. - One object:
kubectl events -n prod --for pod/my-app-6d9f7c-abcde, or with--field-selector involvedObject.name=my-app-6d9f7c-abcdeon the older command. - Live stream:
kubectl get events -n prod -workubectl events -n prod --watchduring a rollout. - Custom columns:
-o custom-columns=TIME:.lastTimestamp,TYPE:.type,REASON:.reason,OBJECT:.involvedObject.name,MSG:.messagegives a compact timeline. - Combine selectors: chain field selectors with commas to match both a kind and a reason, which is the quickest way to answer a question like which pods hit FailedMount.
Edge cases and reading events correctly
Events are easy to misread if you ignore context.
- Empty output: either nothing happened in the last hour, you are in the wrong namespace, or events were rate limited.
- Order trap: without
--sort-bythe output is not chronological, so a recent failure can hide in the middle of the list. - Cluster-scoped objects: node events appear in the
defaultnamespace, or inkube-systemfor some, so check there when a node misbehaves. - Event spam: a crash-looping pod can flood events; filter by reason such as
BackOfforFailedScheduling. - Describe shows events too:
kubectl describe podappends the relevant events at the bottom, already scoped to that object, which is often quicker than listing the namespace. - Aggregation caveat: a high count with an old first timestamp means the problem is recurring, not new, so compare first and last timestamps before declaring it fresh.
Production practice: persist and alert
Do not rely on kubectl for anything you need after an hour.
- Export: ship events to your log backend with a tool such as kubernetes-event-exporter or your observability agent.
- Alert on Warning reasons: FailedScheduling, FailedMount, Unhealthy and OOMKilling are good starters.
- Runbooks: include the exact events command and namespace in the incident runbook.
- Combine: events plus
kubectl logs --previousgives you cause and effect. - Retention planning: if your cluster is busy, events may rotate even faster than the TTL because of rate limiting, so do not depend on them for audits.
- Link to metrics: match event timestamps against dashboards for restarts, memory and latency to confirm which event was the trigger.
- I use kubectl get events -n NS --sort-by=.lastTimestamp, or kubectl events, to get a chronological timeline.
- I filter with field selectors on type and involvedObject and watch events during rollouts.
- I automate event export and alerts since events expire after the default one hour.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Running kubectl get events without --sort-by The default order is not chronological, so you misread the sequence of failures.
- Investigating an old incident with kubectl events The default event TTL is one hour, so older events have already been deleted.
- Looking in the wrong namespace for node events Node-related events are often not in the workload's namespace.
- Treating Normal events as noise only Events such as Killing, Pulled and Scheduled provide the timeline context for the warnings.