⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Kubernetes Interview Questions Scenario 144 of 207 in Kubernetes
Senior DevOps / SRE [L2] Kubernetes Additional Kubernetes Scenarios (Q101-Q200) Production Scenario [L2]
🎯 Target Role / Context: Kubernetes Scenario #134 (L2) · Additional Kubernetes Scenarios (Q101-Q200)

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.

#Kubernetes #L2 #Container Orchestration #K8s #Terraform State
🎙️ Candidate Opening & Architectural Context
"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."
Advertisement
⚡ Recommended Practice Lab

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.

1️⃣

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: Normal or Warning. 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 lastTimestamp reflects 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.
Pro Tip: If you are debugging something that happened more than an hour ago, events are probably already gone. Check logs and metrics.
2️⃣

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.creationTimestamp works when events from the newer events.k8s.io API leave lastTimestamp empty.
  • Dedicated command: kubectl events -n prod (available in kubectl 1.26 and newer) is sorted by default and supports --for, --types and --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 -n in runbooks rather than relying on your current context, which avoids reading the wrong cluster namespace during an incident.
Advertisement
3️⃣

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-abcde on the older command.
  • Live stream: kubectl get events -n prod -w or kubectl events -n prod --watch during a rollout.
  • Custom columns: -o custom-columns=TIME:.lastTimestamp,TYPE:.type,REASON:.reason,OBJECT:.involvedObject.name,MSG:.message gives 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.
4️⃣

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-by the output is not chronological, so a recent failure can hide in the middle of the list.
  • Cluster-scoped objects: node events appear in the default namespace, or in kube-system for some, so check there when a node misbehaves.
  • Event spam: a crash-looping pod can flood events; filter by reason such as BackOff or FailedScheduling.
  • Describe shows events too: kubectl describe pod appends 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.
5️⃣

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 --previous gives 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.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: use kubectl events (1.26 or newer) for a sorted timeline, and always export events to your logging system because the API server drops them after about an hour."
⚡ 60-Second Elevator Pitch Talking Points
  • 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.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. Running kubectl get events without --sort-by The default order is not chronological, so you misread the sequence of failures.
  2. Investigating an old incident with kubectl events The default event TTL is one hour, so older events have already been deleted.
  3. Looking in the wrong namespace for node events Node-related events are often not in the workload's namespace.
  4. Treating Normal events as noise only Events such as Killing, Pulled and Scheduled provide the timeline context for the warnings.
Advertisement
📥 FREE DOWNLOAD · 101-PAGE COMPANION HANDBOOK
Studying for Kubernetes & SRE Technical Rounds?
Download the complete 100-question PDF field guide covering all 11 core modules with offline diagnostic runbooks.
📥 Download PDF (Free) Read Online Guide →
Want more Kubernetes scenarios?
Explore our complete collection of scenario-based Kubernetes interview runbooks.
Browse All Kubernetes Questions →