123DevOps··5 min read

Configuring Prometheus from scratch: scraping, Kubernetes, rules, Alertmanager, and remote_write

A complete, annotated prometheus.yml, target discovery in Kubernetes, ServiceMonitor with kube-prometheus-stack, recording and alerting rules, retention, OTLP ingestion, and shipping data to Mimir.

Observability

Prometheus is the de facto standard for metrics in cloud native environments. The model is simple: it pulls (scrapes) metrics from its targets’ HTTP endpoints at regular intervals, stores the time series on its own disk, and lets you query them with PromQL. In this guide, we build a complete configuration, from the first target all the way to shipping data to long-term storage.

Running Prometheus

To try it locally:

docker run -d --name prometheus -p 9090:9090 \
  -v "$(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml" \
  -v prometheus-data:/prometheus \
  prom/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.retention.time=15d \
  --web.enable-lifecycle

On Kubernetes, the most common route is the kube-prometheus-stack chart, which installs Prometheus, Alertmanager, Grafana, node-exporter, kube-state-metrics, and a set of ready-made rules and dashboards:

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install kube-prometheus-stack prometheus-community/kube-prometheus-stack \
  --namespace observability --create-namespace

The prometheus.yml file, piece by piece

global:
  scrape_interval: 30s        # how often to scrape
  evaluation_interval: 30s    # how often to evaluate rules
  external_labels:
    cluster: prod-sa-east-1   # identifies this Prometheus in central storage

rule_files:
  - /etc/prometheus/rules/*.yml

alerting:
  alertmanagers:
    - static_configs:
        - targets: ["alertmanager:9093"]

scrape_configs:
  # Prometheus itself
  - job_name: prometheus
    static_configs:
      - targets: ["localhost:9090"]

  # machine metrics (CPU, memory, disk, network)
  - job_name: node
    static_configs:
      - targets: ["node-exporter:9100"]

  # Kubernetes pods that ask to be scraped via annotation
  - job_name: kubernetes-pods
    kubernetes_sd_configs:
      - role: pod
    relabel_configs:
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
        regex: "true"
        action: keep
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
        regex: (.+)
        target_label: __metrics_path__
      - source_labels: [__meta_kubernetes_namespace]
        target_label: namespace
      - source_labels: [__meta_kubernetes_pod_name]
        target_label: pod

remote_write:
  - url: https://mimir.example.com/api/v1/push
    headers:
      X-Scope-OrgID: prod

What each block does:

  • global: default intervals. 30 seconds is a good balance for most cases; 15 seconds gives you more resolution at twice the number of samples.
  • external_labels: labels added to everything that leaves this Prometheus — essential when several clusters send data to the same place.
  • rule_files: recording and alerting rules.
  • alerting: where to send fired alerts.
  • scrape_configs: the targets — static (static_configs) or automatically discovered (kubernetes_sd_configs, and equivalents for EC2, Consul, DNS, and others).
  • relabel_configs: rules applied before the scrape to filter targets and set labels. The prometheus.io/scrape: "true" annotation is a convention, not a built-in feature — this relabeling is what makes it work.
  • remote_write: sends copies of the samples to long-term storage such as Mimir (see the LGTM stack).

Always validate before applying, and reload without restarting:

promtool check config prometheus.yml
curl -X POST http://localhost:9090/-/reload    # requires --web.enable-lifecycle

Kubernetes with the Prometheus Operator: ServiceMonitor

With kube-prometheus-stack, you don’t edit prometheus.yml directly. Each team declares what should be scraped with a ServiceMonitor resource:

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: checkout
  namespace: checkout
  labels:
    release: kube-prometheus-stack   # must match the Prometheus selector
spec:
  selector:
    matchLabels:
      app: checkout
  endpoints:
    - port: http-metrics
      path: /metrics
      interval: 30s

The Operator generates the scrape configuration automatically. The most common beginner mistake: a ServiceMonitor missing the label Prometheus uses to select them (by default, release set to the Helm release name) — the resource exists, but nothing gets scraped. For pods without a Service, there’s PodMonitor.

Recording and alerting rules

Recording rules precompute expensive queries; alerting rules fire when a condition holds:

# /etc/prometheus/rules/checkout.yml
groups:
  - name: checkout
    rules:
      - record: service:http_requests:rate5m
        expr: sum by (service, status_class) (rate(http_requests_total[5m]))

      - alert: CheckoutHighErrorRate
        expr: |
          sum(rate(http_requests_total{service="checkout", status_class="5xx"}[5m]))
          / sum(rate(http_requests_total{service="checkout"}[5m])) > 0.05
        for: 10m
        labels:
          severity: page
        annotations:
          summary: "More than 5% errors on checkout for 10 minutes"

Validate with promtool check rules and favor alerts on user symptoms — details in symptom-based alerting.

Alertmanager: delivering the alerts

Prometheus only evaluates; the one that notifies is Alertmanager — with grouping, silencing, inhibition, and routing by team and severity. A minimal configuration with Slack:

route:
  receiver: slack-platform
  group_by: [alertname, service]

receivers:
  - name: slack-platform
    slack_configs:
      - api_url_file: /etc/alertmanager/slack-webhook
        channel: "#platform-alerts"
        send_resolved: true

Exporters: metrics for things you don’t own

For systems that don’t expose metrics in the Prometheus format, there are exporters:

Exporter What it exposes
node_exporter Machine CPU, memory, disk, and network
kube-state-metrics State of Kubernetes objects (deployments, pods, jobs)
blackbox_exporter External HTTP, TCP, DNS, and ICMP probes
postgres_exporter / mysqld_exporter Database metrics
redis_exporter Redis metrics

Applications instrumented with OpenTelemetry can also send metrics directly to Prometheus: since version 3, it can receive OTLP when started with --web.enable-otlp-receiver (endpoint /api/v1/otlp/v1/metrics).

Retention, disk, and high availability

  • Local retention: --storage.tsdb.retention.time (by time) and --storage.tsdb.retention.size (by size). Local Prometheus is great for days or weeks; for months or years, use remote_write to Mimir or Thanos.
  • Disk: use fast persistent volumes and monitor free space — a Prometheus that runs out of disk stops scraping.
  • Memory: grows with the number of active series. Keep cardinality under control from day one (see cardinality in Prometheus and Mimir).
  • High availability: run two identical replicas scraping the same targets; Mimir or Thanos deduplicate the data, and Alertmanager deduplicates the alerts.

Checklist

  • external_labels identifying cluster and environment
  • Automatic target discovery (ServiceMonitor or kubernetes_sd_configs)
  • Configuration and rules validated with promtool in CI
  • Alertmanager with routing by team and severity
  • Exporters for infrastructure and databases
  • Local retention defined and remote_write for long-term storage
  • Two replicas for high availability

A well-configured Prometheus is invisible: it collects what it needs, alerts when it should, and is never itself the cause of the incident.

Get updates on new posts and stay in the loop!