Kubernetes observability for SREs hinges on understanding the cluster's health, performance, and operational state. Three foundational components provide crucial insights: metrics-server, kube-state-metrics, and events. metrics-server is a cluster-wide aggregator of resource usage data. It collects CPU and memory metrics from Kubelets on each node, making this information available via the Kubernetes API. Its primary role isn't for long-term storage but to power built-in features like kubectl top node and kubectl top pod, and, critically, to enable Horizontal Pod Autoscalers (HPA) and Vertical Pod Autoscalers (VPA) to make scaling decisions based on actual resource consumption. Without metrics-server, your autoscaling capabilities based on resource usage are severely limited, and real-time performance diagnostics become significantly harder.
While metrics-server tells you how much resource a pod or node is consuming, kube-state-metrics tells you what state Kubernetes objects are in. It's a service that listens to the Kubernetes API server and generates metrics about the state of various objects like Deployments, Pods, Nodes, PersistentVolumes, and more. Instead of resource usage, it exposes metrics such as kube_deployment_status_replicas_available, kube_pod_status_phase (e.g., Running, Pending, Failed), or kube_persistentvolumeclaim_status_phase. These metrics are exposed in Prometheus format, making them invaluable for understanding the overall health, capacity, and operational status of your cluster. SREs leverage kube-state-metrics for building dashboards that monitor deployment rollout status, identify stuck pods or volumes, track node readiness, and generally understand the "health" of the control plane and workloads from a logical, rather than resource-usage, perspective.
Finally, Kubernetes events provide a chronological log of changes and occurrences within the cluster. Whenever a pod is scheduled, a container starts or fails, a volume is attached, or a node goes offline, an event is generated. These are short-lived records, typically retained for only an hour or so by default, making them crucial for real-time debugging of transient issues. An SRE investigating why a pod isn't starting would immediately check events associated with that pod or its owning deployment to see scheduling failures, image pull errors, or volume attachment problems. While ephemeral, events are an indispensable first-line diagnostic tool, providing context for why something is in a particular state. Together, these three pillars form a comprehensive picture: metrics-server for resource consumption, kube-state-metrics for object state, and events for understanding the dynamics and lifecycle changes that lead to those states.
Key Takeaways
metrics-serverprovides real-time CPU/memory usage metrics for pods and nodes, essential forkubectl topand HPA/VPA.kube-state-metricsexposes Kubernetes object state (e.g., pod phases, deployment replicas) as Prometheus metrics, crucial for logical cluster health and capacity planning.Eventsoffer a short-lived, chronological log of cluster activities and state changes, vital for immediate troubleshooting and understanding lifecycle issues.- These components form a complementary observability foundation, covering resource usage, object state, and the dynamics that drive those states.
Code Example
# Check for recent cluster-wide warning events
kubectl get events --all-namespaces \
--field-selector type=Warning \
--sort-by='.lastTimestamp' \
-o custom-columns="LAST SEEN:.lastTimestamp,TYPE:.type,REASON:.reason,OBJECT:.involvedObject.kind/.involvedObject.name,MESSAGE:.message" \
--limit 5How this code works
This command gathers a filtered list of warning events from across a Kubernetes cluster, presenting them in a structured, readable format. Its purpose is to help SREs quickly review potential issues by showing significant events that might indicate problems across different components of the system.
The command begins by fetching all Kubernetes events using kubectl get, expanding its scope to all-namespaces for a cluster-wide view. It then applies a crucial filter with field-selector type=Warning, narrowing down the results to focus solely on events categorized as warnings. These often signal potential underlying issues. The results are then ordered using sort-by='.lastTimestamp', which, by default, sorts in ascending chronological order. This is a subtle point that might trip up a beginner: despite aiming to find "recent" warnings, limit 5 will, in this setup, display the five earliest observed warning events rather than the most current ones. Finally, the -o custom-columns option formats the output, defining custom column headers like LAST SEEN and REASON and extracting specific data points such as involvedObject.kind and message to make the critical event details easy to review.