A monitoring lab with Prometheus and Grafana

Run a small monitoring stack on your own computer: collect metrics from a machine, draw dashboards, set an alert and test a backup, so you can practise the monitoring and observability courses with real tools.

  • Time: 45 minutes
  • Cost: free software, your own computer

What you need

Monitoring has three parts: collect numbers from systems (metrics), look at them (dashboards) and tell someone when they go wrong (alerts). A free, widely used stack does all three:

  • node_exporter publishes a machine’s CPU, memory, disk and network numbers.
  • Prometheus (prometheus.io) scrapes those numbers on a timer and stores them.
  • Grafana (grafana.com) draws dashboards from Prometheus.
  • Alertmanager routes alerts, for example to email or a chat tool.

Set it up

The quickest route is Docker Compose. Create a folder with a prometheus.yml:

global:
  scrape_interval: 15s
scrape_configs:
  - job_name: "node"
    static_configs:
      - targets: ["node-exporter:9100"]

and a docker-compose.yml that runs prom/prometheus, grafana/grafana and prom/node-exporter (mount the file into Prometheus, publish ports 9090 and 3000, and put the three on one network). Then:

docker compose up -d

Open http://localhost:9090 for Prometheus and http://localhost:3000 for Grafana (the first sign-in is admin; change the password immediately). In Grafana add a Prometheus data source pointing to http://prometheus:9090, then import a community “Node Exporter” dashboard.

To monitor another VM, install prometheus-node-exporter there (sudo apt install prometheus-node-exporter) and add vm-address:9100 to the targets.

Check that it works

  • In Prometheus, Status, Targets shows each target UP.
  • The query node_memory_MemAvailable_bytes returns a number, and rate(node_cpu_seconds_total{mode="idle"}[1m]) returns a graph.
  • Your Grafana dashboard shows live CPU and memory. Run yes > /dev/null on a VM and watch the CPU line rise.

Practise safely

  • Keep Grafana and Prometheus on localhost or an internal lab network; they expose detailed information about your systems and have no protection by default.
  • Change default passwords, and never expose these ports to the internet.
  • Metrics can be large; set a retention limit (--storage.tsdb.retention.time=7d) so a lab does not fill the disk.
  • Practise backups on test data: back up a folder with tar or rsync, delete the original, restore it and check it matches. A backup you have never restored is only a hope.

Go further

  • Write an alert rule such as “memory available is under 10 percent for 5 minutes”, trigger it by using memory, and confirm it fires and then clears.
  • Add a service check (for example blackbox_exporter probing a web page) and alert when the page is down.
  • Define a simple service level objective (for instance 99 percent of requests under 300 ms) and chart it.
  • Test a complete recovery: restore a database or folder from backup onto a fresh VM and time it.
  • Compare what the dashboards show with what the logs show when something breaks.

Courses that use this lab

Other lab guides

Software changes quickly. Menu names and version numbers may differ a little from what you see; the ideas and the check at the end of each section stay the same. Always download from the project's own website, and read the licence terms for your situation.