RousselTMLEARNING

Article · Prometheus: From Discovery to Expertise

Prometheus in Kubernetes: Operator and ServiceMonitor

1. Monitoring Kubernetes: what to collect #

Four sources of metrics.

The sources #

SourceContentExposed by
kube-state-metricsObject state: pods, deployments, jobs, PVCs...kube-state-metrics Deployment
node-exporterNode CPU, RAM, disk, networkDaemonSet
kubelet / cAdvisorContainer consumptionEach node (/metrics/cadvisor)
Control planeAPI server, scheduler, etcd, controller-managerCluster components
Your applicationsBusiness 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:9090

The 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 #

How the Prometheus Operator works
No need to edit 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: metrics

PrometheusRule #

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 serviceMonitorSelector
Common mistake: If your ServiceMonitor is not picked up, first check that its release label matches the serviceMonitorSelector of the Prometheus object, then that the Service port is named.

4. Kubernetes PromQL queries #

The essentials.

A few useful queries #

Prometheus targets in Kubernetes
A quick check of the collected targets.
# 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.1
Good practice: For large clusters, delegate long-term storage to Mimir/Thanos with remoteWrite and reduce Prometheus's local retention.

📄 Prometheus in the Kubernetes world (Operator, ServiceMonitor, kube-prometheus-stack) →

My progress

  • ⏱ Estimated time : ≈ 12 min

Sign in to track your progress and take notes. Sign in

💬 Recent discussions about this course

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

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!