Monitoring
kube-state-metrics, metrics-server, events, alerts, and the dashboard
Kubernetes produces three kinds of signal, from three different places.
| Signal | Source | Answers |
|---|---|---|
| Object state | kube-state-metrics | Is the Deployment short of replicas? Is a pod crash-looping, pending, OOM-killed? Is the HPA at its max? Is a node not ready? |
| Resource usage | metrics-server (kubelets, cAdvisor) | How much CPU and memory is each pod using right now? |
| Events | the API server | What did controllers just do or fail to do, and why? |
kube-state-metrics
kube-state-metrics watches the API server and turns objects into metrics,
one series per object and field. The bootstrap exposes it on a NodePort,
mapped to localhost:8281, and the Prometheus stack scrapes it as job
kubernetes-state:
curl -s localhost:8281/metrics | grep '^kube_horizontalpodautoscaler_status_current_replicas'
kube_horizontalpodautoscaler_status_current_replicas{namespace="shop",horizontalpodautoscaler="web"} 3Inside a real cluster, Prometheus runs in the cluster (usually via the
Prometheus Operator and kube-prometheus-stack) and discovers targets through
the API (kubernetes_sd_configs), including every kubelet's cAdvisor
endpoint for per-container CPU, memory and CPU throttling. This module
scrapes from outside the cluster, so it sees object state only.
Alerts
During the labs, Prometheus recorded:
max_over_time(ALERTS{alertname=~"Kube.*"}[90m])
KubeNodeNotReady lab-worker firing # node_failure.py
KubeDeploymentUnavailable pending # rollouts and drains: recovered before 10m
KubePodPending pending # statefulset.py: redis-0 pinned to a cordoned nodeThe for: durations did their job: rollouts and drains briefly reduce
available replicas and should not page anyone; a dead node should.
KubeHpaMaxedOut never fired, since the autoscaling lab kept the HPA at
its maximum for about 2 minutes, not 15.
The OOM alert and two unit tests for it are in
prometheus/config/tests/alerts.test.yml: a restart whose last termination
was an OOM kill fires; a restart for any other reason doesn't.
The dashboard
Grafana provisions Kubernetes Workloads from
grafana/dashboards/Platform/kubernetes-workloads.json, with a namespace
selector: nodes ready, pods running and pending, restarts, OOM-killed
containers and Deployments short of replicas across the top; then desired
vs available replicas, HPA current/desired/max, restarts and pods by phase;
then CPU requested and memory limits per namespace, and node conditions.
Events
kubectl get events -n shop --sort-by=.lastTimestamp
Warning Unhealthy pod/web-... Liveness probe failed: HTTP probe failed with statuscode: 500
Normal Killing pod/web-... Container web failed liveness probe, will be restarted
Normal SuccessfulRescale horizontalpodautoscaler/web New size: 3; reason: Current number of replicas below Spec.MinReplicasEvents explain why: why a pod is pending, why it was killed, why the HPA scaled. They're kept for about an hour by default; ship them to your log system (an event exporter) to keep them for incidents.
Monitor the platform, not just the apps: the Gateway's NGINX (NGINX Gateway Fabric serves Prometheus metrics), CoreDNS, the control plane's API latency and etcd. When those break, every app's alerts fire at once, and the platform alert is the one that points at the cause.
Lab
- Open the dashboard and run
autoscaling.py. Which panels move, and how late? - Run
resources.pyand check whetherKubeContainerOOMKilledfires. Why might it miss an OOM kill entirely? (Hint: what happens tolast_terminated_reasonwhen the pod is replaced?) - Write an alert for "a node's pods have been evicted but replacements are Pending."