Ravindra BagaleCourses & study guides Track your progress

Labs · DevOps

Lab: Run Node Exporter, Prometheus and Grafana with Docker Compose, Build a Disk Panel and Trigger a Real Disk Alert

Intermediate50 minLinux machine or VM with Docker Compose · A browser

Course: DevOps · Chapter 13: Monitoring Basics · Lesson: 13.13 Lab: disk metric path and a triggered alert

Chapter 13 explains golden signals and the Prometheus path; this lab builds it on one machine.

Chala mitrano! Customer cha phone yenyaadhi dashboard var laal disla pahije. Aaj ek disk alert muddam fire karuya.

Suppose we are…

Suppose we are on the operations team of a Netflix-style app and a server once stopped because its disk filled up with logs. Nobody noticed until users complained. One metric and one alert would have warned us an hour earlier.

In this lab we run Node Exporter (host metrics), Prometheus (stores metrics and checks rules) and Grafana (dashboards) on one Linux machine, then fill the disk a little on purpose so the alert fires.

Goal of this lab

By the end you will have:

  • Prometheus scraping Node Exporter (Status → Targets shows UP).
  • A Grafana panel with disk used percent.
  • An alert rule that goes from Inactive to Firing and back.

What you need

  • A Linux machine or VM with Docker and Compose (Node Exporter needs Linux to read the real host disk).
  • Ports 3000 and 9090 free.
  • About 2 GB free disk for the test file.
  • 50 minutes.

Part 1: Config files

  1. Create the folder and the Prometheus config:

    prometheus.yml

    global:
      scrape_interval: 15s
      evaluation_interval: 15s
    rule_files:
      - /etc/prometheus/alerts.yml
    scrape_configs:
      - job_name: node
        static_configs:
          - targets: ["node-exporter:9100"]
    
    mkdir mon && cd mon
    
  2. Create the alert rule. For the lab the threshold is low so you can trigger it; in real life use 80 or 90:

    alerts.yml

    groups:
      - name: host
        rules:
          - alert: DiskAlmostFull
            expr: 100 * (1 - node_filesystem_avail_bytes{mountpoint="/",fstype!="tmpfs"} / node_filesystem_size_bytes{mountpoint="/",fstype!="tmpfs"}) > LAB_LIMIT
            for: 1m
            labels:
              severity: warning
            annotations:
              summary: "Disk on / is above LAB_LIMIT percent"
    

    Run df -h /, note your Use%, and replace both LAB_LIMIT values with that number plus 2 (for example 47 if you are at 45%).

  3. Create the compose file:

    compose.yaml

    services:
      node-exporter:
        image: prom/node-exporter:latest
        command: ["--path.rootfs=/host"]
        pid: host
        volumes:
          - /:/host:ro,rslave
      prometheus:
        image: prom/prometheus:latest
        ports: ["127.0.0.1:9090:9090"]
        volumes:
          - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
          - ./alerts.yml:/etc/prometheus/alerts.yml:ro
      grafana:
        image: grafana/grafana:latest
        ports: ["127.0.0.1:3000:3000"]
    

    Ports bind to 127.0.0.1 so only this machine can open the dashboards.

Part 2: Start and check targets

  1. Start the stack:

    docker compose up -d
    docker compose ps
    

    What you should see: three services running.

  2. Open http://localhost:9090 → Status → Targets.

    What you should see: node with state UP.

  3. In the Prometheus Graph page run this query:

    100 * (1 - node_filesystem_avail_bytes{mountpoint="/",fstype!="tmpfs"} / node_filesystem_size_bytes{mountpoint="/",fstype!="tmpfs"})
    

    What you should see: one number close to the Use% from df -h /.

Part 3: Grafana panel

  1. Open http://localhost:3000, log in with admin / admin and set a new password when asked.

  2. Connections → Data sources → Add → Prometheus, URL http://prometheus:9090, Save & test.

    What you should see: Successfully queried the Prometheus API.

  3. Dashboards → New → Add visualization, choose the Prometheus source, paste the disk query, set unit Percent (0-100), title Disk used /, save the dashboard.

    What you should see: a line near your disk percentage.

Part 4: Fire the alert

  1. Create a 2 GB test file to push disk usage over the lab limit:

    fallocate -l 2G ~/fill-test.img
    df -h /
    
  2. Open Prometheus Alerts and wait about 1–2 minutes.

    What you should see: DiskAlmostFull goes Pending and then Firing; the Grafana panel jumps up.

  3. Remove the file:

    rm ~/fill-test.img
    

    What you should see: after a minute the alert returns to Inactive.

Clean up

  1. Stop and remove the stack:

    docker compose down
    ls ~/fill-test.img 2>/dev/null || echo "test file removed"
    

Ravindra Bagale's Tip

One alert that matters beats twenty that nobody reads. Start with disk, then add more only when you know who will act on them.

Common mistakes

MistakeWhat happensFix
Target DOWNNo data in queriesUse the service name node-exporter:9100, not localhost
Data source URL http://localhost:9090 in GrafanaGrafana cannot reach PrometheusUse http://prometheus:9090
Forgetting to delete the fill fileDisk stays highrm ~/fill-test.img
Opening 3000/9090 to the internetDashboards visible to everyoneBind to 127.0.0.1 or put them behind login and HTTPS

Self-check checklist

0 of 5 done

Try-at-home challenge

Add Alertmanager to the compose file and route severity: warning to an email or chat webhook you own. Then add a CPU panel with 100 - avg(rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100.

Samajla ka? Metric, panel, alert, and someone who acts on it. Aata pudhe jaauya Kubernetes.