1. Monitoring Kubernetes: what to collect #
Four sources of metrics.
The sources #
| Source | Content | Exposed by |
|---|---|---|
| kube-state-metrics | Object state: pods, deployments, jobs, PVCs... | kube-state-metrics Deployment |
| node-exporter | Node CPU, RAM, disk, network | DaemonSet |
| kubelet / cAdvisor | Container consumption | Each node (/metrics/cadvisor) |
| Control plane | API server, scheduler, etcd, controller-manager | Cluster components |
| Your applications | Business and technical metrics | /metrics endpoint |
2. Installing kube-prometheus-stack #
All-in-one with Helm.
Installation #
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install monitoring prometheus-community/kube-prometheus-stack \
-n monitoring --create-namespace \
--set grafana.adminPassword=ChangeMe! \
--set prometheus.prometheusSpec.retention=15d \
--set prometheus.prometheusSpec.serviceMonitorSelectorNilUsesHelmValues=false
kubectl -n monitoring get pods
kubectl -n monitoring port-forward svc/monitoring-grafana 3000:80
kubectl -n monitoring port-forward svc/monitoring-kube-prometheus-prometheus 9090:9090The chart installs the operator, Prometheus, Alertmanager, Grafana with ready-to-use Kubernetes dashboards, node-exporter, kube-state-metrics and a standard set of alert rules.
3. The Operator's CRDs #
Declaring what you want to monitor.
ServiceMonitor and PodMonitor #
prometheus.yml anymore: you declare Kubernetes objects.# Application exposant /metrics sur le port nommé "http-metrics"
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: mon-api
namespace: monitoring
labels: { release: monitoring }
spec:
namespaceSelector: { matchNames: [production] }
selector:
matchLabels: { app: mon-api }
endpoints:
- port: http-metrics
path: /metrics
interval: 30s# Sans Service : cibler directement les pods
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata: { name: workers, namespace: monitoring, labels: { release: monitoring } }
spec:
namespaceSelector: { matchNames: [production] }
selector: { matchLabels: { app: worker } }
podMetricsEndpoints:
- port: metricsPrometheusRule #
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata: { name: mon-api-alertes, namespace: monitoring, labels: { release: monitoring } }
spec:
groups:
- name: mon-api
rules:
- alert: ApiHighErrorRate
expr: sum(rate(http_requests_total{app="mon-api",status=~"5.."}[5m])) / sum(rate(http_requests_total{app="mon-api"}[5m])) > 0.05
for: 10m
labels: { severity: critical }
annotations: { summary: "Plus de 5 % d'erreurs sur mon-api" }kubectl get servicemonitors,podmonitors,prometheusrules -A
kubectl -n monitoring get prometheus -o yaml | grep -A3 serviceMonitorSelectorCommon mistake: If yourServiceMonitoris not picked up, first check that itsreleaselabel matches theserviceMonitorSelectorof thePrometheusobject, then that the Service port is named.
4. Kubernetes PromQL queries #
The essentials.
A few useful queries #
# Pods qui redémarrent
increase(kube_pod_container_status_restarts_total[1h]) > 3
# Pods non prêts
kube_pod_status_ready{condition="false"} == 1
# CPU par namespace (cœurs)
sum by (namespace) (rate(container_cpu_usage_seconds_total{container!=""}[5m]))
# Mémoire utilisée / demandée
sum by (pod) (container_memory_working_set_bytes{container!=""})
/ sum by (pod) (kube_pod_container_resource_requests{resource="memory"})
# Réplicas indisponibles
kube_deployment_status_replicas_unavailable > 0
# PVC bientôt pleins
kubelet_volume_stats_available_bytes / kubelet_volume_stats_capacity_bytes < 0.1Good practice: For large clusters, delegate long-term storage to Mimir/Thanos with remoteWrite and reduce Prometheus's local retention.My progress
Sign in to track your progress and take notes. Sign in
💬 Recent discussions about this course
- Prometheus consomme 12 Go de RAM : comment trouver les métriques coupables ?✓ solved · 2 reply(ies)
- PromQL : quelle différence entre rate() et irate() ?✓ solved · 1 reply(ies)
Ask a question about this lesson →See all discussions about this course
Course Glossary
Alerting
The process of notifying teams (via email, Slack, Teams) when a specific event, threshold breach, or anomaly is detected.
Architecture
The overall structural design of an IT system, defining its various components, their relationships, and the principles guiding its evolution.
OpenTelemetry
An open source framework providing standards, APIs, and SDKs to collect and export observability data (traces, metrics, logs) in an agnostic way.
Recommended Articles
Prometheus on Kubernetes and the Prometheus web interface
Deploy Prometheus on Kubernetes with kube-prometheus-stack (Prometheus Operator, ServiceMonitor, ...
Deploying Thanos on Kubernetes (kube-prometheus-stack, Helm and Operator)
Install Thanos on Kubernetes: sidecar via kube-prometheus-stack, objstore secret, Bitnami/kube-th...
Operating VictoriaMetrics: vmalert, backups, Kubernetes and monitoring
Evaluate rules and alerts with vmalert, back up with vmbackup, deploy on Kubernetes with the oper...
Prometheus: visualizing your metrics (UI, Grafana and dashboards)
Visualize Prometheus data: the web interface, PromQL graphs, connecting to Grafana, RED and USE d...
Prometheus best practices: metrics, cardinality, rules and operations
Prometheus best practices: metric naming, labels and cardinality, scrape intervals, recording rul...
Metric types in Prometheus
Discover in detail the four metric types supported by Prometheus (Counter, Gauge, Histogram, Summ...
PromQL: an introduction to the Prometheus query language
Learn PromQL, the Prometheus query language: selectors, rate, sum by, histograms and first querie...
Elastic Agent on Kubernetes
Deploy Elastic Agent on Kubernetes: Fleet-managed DaemonSet, Kubernetes integration, kube-state-m...
Fluent Bit on Kubernetes: DaemonSet, multiline, buffering and monitoring
Deploy Fluent Bit on Kubernetes with Helm, enrich with the kubernetes filter, handle multiline lo...