Learn Labs
Operations

Monitoring

kube-state-metrics, metrics-server, events, alerts, and the dashboard

Kubernetes produces three kinds of signal, from three different places.

SignalSourceAnswers
Object statekube-state-metricsIs 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 usagemetrics-server (kubelets, cAdvisor)How much CPU and memory is each pod using right now?
Eventsthe API serverWhat 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"} 3
- job_name: kubernetes-statehonor_labels: truekeep KSM's own namespace/pod labelsstatic_configs:- targets: ["host.docker.internal:8281"]

Inside 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

KubeNodeNotReady kube_node_status_condition{condition="Ready",status="true"} == 0 for 2m criticalKubePodCrashLooping waiting_reason{reason="CrashLoopBackOff"} == 1 for 5mKubeContainerOOMKilled restarts increased AND last_terminated_reason{reason="OOMKilled"}KubeDeploymentUnavailable replicas_available < spec_replicas for 10mKubePodPending kube_pod_status_phase{phase="Pending"} == 1 for 10mKubeHpaMaxedOut current_replicas >= spec_max_replicas for 15m

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 node

The 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.MinReplicas

Events 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.

Lab

  1. Open the dashboard and run autoscaling.py. Which panels move, and how late?
  2. Run resources.py and check whether KubeContainerOOMKilled fires. Why might it miss an OOM kill entirely? (Hint: what happens to last_terminated_reason when the pod is replaced?)
  3. Write an alert for "a node's pods have been evicted but replacements are Pending."

On this page