K8s by Example: Prometheus Monitoring
| Prometheus is the standard for Kubernetes metrics. Applications expose metrics on an HTTP endpoint; Prometheus scrapes them periodically. The Prometheus Operator simplifies deployment with custom resources like ServiceMonitor and PodMonitor. Use for: alerting, dashboards, capacity planning, SLO tracking. |
| terminal | |
| Install kube-prometheus-stack via Helm. This deploys Prometheus, Alertmanager, Grafana, and the Prometheus Operator which provides ServiceMonitor, PodMonitor, and PrometheusRule CRDs. | |
| metrics-app.yaml | |
| Applications expose metrics on a | |
| service-monitor.yaml | |
| ServiceMonitor (from Prometheus Operator) defines how to scrape Services. It selects Services by label and specifies which port to scrape. More powerful than annotations for complex setups. | |
| pod-monitor.yaml | |
| PodMonitor scrapes Pods directly without needing a Service. Useful for DaemonSets, Jobs, or Pods that don’t need Services. Selects Pods by label, specifies container port to scrape. | |
| service-with-monitor.yaml | |
| Complete example: Deployment, Service, and ServiceMonitor together. The Service exposes the app; the ServiceMonitor tells Prometheus how to scrape it. Labels connect all three resources. | |
| prometheus-rules.yaml | |
| PrometheusRule defines alerting and recording rules. Recording rules precompute expensive queries. Alerting rules fire when conditions are met for a duration. Labels route alerts to different receivers. | |
| adapter-metrics.yaml | |
| For apps that don’t expose Prometheus metrics, use an adapter sidecar (exporter). The exporter queries the app and translates its metrics to Prometheus format. Common exporters exist for Redis, MySQL, PostgreSQL, etc. | |
| terminal | |
| Verify Prometheus is discovering your targets. Check ServiceMonitor/PodMonitor resources, then query Prometheus targets endpoint or UI to confirm scraping is working. | |