DevOps.Academy
CoursesCurriculumInterview prepPricing
◔My learningSign in
DevOps.Academy

Free, hands-on DevOps lessons. Made with care in India.

System status

Learn

All coursesLinux roadmapCurriculumInterview questionsCheat sheetsMy learning

Academy

PricingCertificates

Legal

PrivacyTermsContact

devops.rajeev.pro

MonitoringLesson 3 of 9
Your progress · 0%
On this page
  1. Why dashboards need their own tool
  2. Data sources
  3. Build a dashboard
  4. Variables
  5. Keep dashboards in Git
↖ Course roadmap
Lesson 3 · Grafana

Grafana dashboards

Connect Grafana to Prometheus, build a dashboard people actually read, make it reusable with variables, and keep it in Git with provisioning so it survives a rebuild.

30 min read · intermediate · hands-on

By the end of this lesson you'll be able to

0 of 5 complete

Why dashboards need their own tool

Prometheus answers any question you can type. Grafana is what makes the answers usable by a whole team, at 2 a.m., in seconds.

Before: queries typed from memory

You have Prometheus and PromQL. An incident starts at 2 a.m. You open the Prometheus UI and type queries from memory, one per tab: CPU, memory, error rate. The latency query you can't quite remember.

Every incident started by rebuilding the same graphs.© Animation: DevOps Academy

Every incident started by rebuilding the same graphs.© Animation: DevOps Academy

The problem: the knowledge lives in one head

Your new teammate doesn't know the queries. The product manager asks "is the site healthy?" and gets a screenshot of a PromQL expression. Nobody can see traffic, errors, latency and saturation side by side, for any server.

The queries lived in one person's head.© Animation: DevOps Academy

The queries lived in one person's head.© Animation: DevOps Academy

The fix: saved panels on one dashboard

Grafana saves each query once as a panel and arranges panels on a dashboard. One link shows the whole picture, for any server and any time range, to anyone on the team.

Queries saved once as panels, shown together.© Animation: DevOps Academy

Queries saved once as panels, shown together.© Animation: DevOps Academy

How it works: every panel asks again

Grafana stores no metrics. When you open a dashboard or change a dropdown, every panel sends its saved query to a data source, such as Prometheus, for the chosen time range, and draws what comes back.

Grafana stores dashboards, not metrics.© Animation: DevOps Academy

Grafana stores dashboards, not metrics.© Animation: DevOps Academy

In this lesson you'll connect Grafana to Prometheus, build the dashboard below, make it work for any server, and keep it in Git so a lost Grafana is rebuilt in minutes.

The dashboard you build in this lesson, with real data from the lab machine. Four stat panels answer 'right now', two graphs answer 'over time'.© Screenshot: DevOps Academy

The dashboard you build in this lesson, with real data from the lab machine. Four stat panels answer 'right now', two graphs answer 'over time'.© Screenshot: DevOps Academy

Data sources

A data source is a connection Grafana uses to fetch data: a type (Prometheus, Loki, Jaeger, PostgreSQL, CloudWatch), a URL and, if needed, credentials. You set it up once, and every panel picks it by name.

Start Grafana next to Prometheus on the same Docker network as lesson 2:

$ docker run -d --name grafana --network mon -p 3000:3000 grafana/grafana
$ curl -s localhost:3000/api/health
{
  "database": "ok",
  "version": "13.3.0",
  "commit": "6f8ce3c693b493043ebcd4787566d2b6b118b0f0"
}
docker run -d --name grafana --network mon -p 3000:3000 grafana/grafana
  • dockerbuild and run containers
  • runstart a new container
  • -drun in the background
  • --name grafananame the container
  • --network monwhich network to join
  • -p 3000:3000publish a port, host:container
  • grafana/grafanathe image to start
curl -s localhost:3000/api/health
  • curlmake an HTTP request
  • -ssilent: no progress bar

Log in at http://localhost:3000 (first login admin / admin, then it asks for a new password). Go to Connections → Data sources → Add data source → Prometheus, set the URL to http://prometheus:9090 and press Save & test.

Two details trip people up:

  • The URL is resolved by the Grafana server, not by your browser. Inside Docker, that's http://prometheus:9090. localhost:9090 would point at the Grafana container itself.
  • Access: Server (proxy) is the default and the right choice. The browser asks Grafana, and Grafana asks Prometheus, so Prometheus never has to be reachable from users' laptops.

Provision the data source from a file

Clicking works once. For a server you can rebuild, describe the data source in a file that Grafana reads at startup:

# provisioning/datasources/prometheus.yml
apiVersion: 1
datasources:
  - name: Prometheus
    type: prometheus
    uid: prometheus # a fixed id dashboards can refer to
    access: proxy
    url: http://prometheus:9090
    isDefault: true

Mount the folder at /etc/grafana/provisioning and the API confirms it, marked read-only because the file owns it:

$ curl -s -u admin:admin localhost:3000/api/datasources
[
    {
        "uid": "prometheus",
        "name": "Prometheus",
        "type": "prometheus",
        "access": "proxy",
        "url": "http://prometheus:9090",
        "isDefault": true,
        "readOnly": true,
        ...
    }
]
curl -s -u admin:admin localhost:3000/api/datasources
  • curlmake an HTTP request
  • -ssilent: no progress bar

(The real answer has a few more fields, cut here with ....)

Build a dashboard

A dashboard is a page of panels. A panel is one question: a query, a visualisation, a unit and optional thresholds. Create one with Dashboards → New → New dashboard → Add visualization.

A panel is a saved query plus how to draw it. A dashboard is a JSON file of panels and variables.© Diagram: DevOps Academy

A panel is a saved query plus how to draw it. A dashboard is a JSON file of panels and variables.© Diagram: DevOps Academy

Pick the visualisation from the question

QuestionVisualisationExample from the dashboard
What is it right now?StatCPU used 31.5%
How close is it to a limit?GaugeRoot disk 9.90% used
How did it change over time?Time seriesCPU by mode, network received
Which ones are worst?Bar gauge / tableTop 5 servers by memory

Set the unit and thresholds

Every panel query from lesson 2 works as-is. For "CPU used":

100 * (1 - avg by (instance) (rate(node_cpu_seconds_total{mode="idle", instance=~"$instance"}[5m])))

In the panel options set Unit → Percent (0-100) and Thresholds → 70 orange, 90 red. Units matter more than they look: without one, 974034223104 is a mystery; with bytes(SI) Grafana shows 974 GB. Thresholds turn a number into a judgement a tired person can read in one second.

A real mistake: the panel that said "No data"

The first version of the "Load (1m) per core" panel showed No data, while the other five panels worked. The query was:

node_load1{instance=~"$instance"} / count by (instance) (node_cpu_seconds_total{mode="idle", instance=~"$instance"})

It's the label-matching problem from lesson 2: node_load1 carries job and env, the right side only instance, so the division matched nothing. Adding on (instance) after the / fixed it, and the panel showed 0.365. When a single panel is empty, copy its query into Explore (the compass icon) and run it there. Grafana only draws what the data source returns.

Lay it out for the reader

  • Top row: the answer. Four stat panels a manager can read: is it healthy?
  • Below: the detail. Time series an engineer uses to find out why.
  • One topic per dashboard. "Node overview" for servers, "Checkout service" for an app. A 50-panel dashboard is one nobody reads.
  • Name panels as questions or plain nouns ("CPU used", "Errors per second"), never a PromQL expression.

Variables

Without variables, you'd copy the dashboard for every server and fix forty copies every time a panel changes. A variable is a dropdown at the top of the dashboard whose value is pasted into every query.

Create one in Dashboard settings → Variables → New variable:

SettingValueWhy
NameinstanceUsed in queries as $instance
TypeQueryValues come from the data source
Querylabel_values(node_uname_info, instance)Every instance that has node_exporter
Multi-valueon, with Include AllPick one server, several, or all
RefreshOn time range changeNew servers appear without a reload

Then filter every panel query with instance=~"$instance". Use =~ (regex match), not =: when you pick several servers or All, Grafana fills the variable with a regex like (web-1|web-2), and = would match nothing.

What happens when someone picks a server:

  1. Grafana replaces $instance in every panel's query with the chosen value.
  2. Each panel sends its query to Prometheus for the current time range.
  3. Prometheus returns only that server's series, and the panels redraw.

Other useful variables: $job for the service, $env for staging or production, and the built-in $__rate_interval, which picks a safe rate() window for the current zoom level. Grafana's docs recommend rate(x[$__rate_interval]) instead of a fixed [5m] in dashboards.

Server Monitoring // Prometheus and Grafana TutorialChristian Lempa · Watch on YouTube ↗

Keep dashboards in Git

A dashboard is a JSON document. Open Edit → Settings → JSON Model (or Export → Export as JSON) and you'll see every panel, query and variable. That means it can live in Git, be reviewed in a pull request, and be loaded automatically.

Tell Grafana where the files are with a dashboard provider:

# provisioning/dashboards/provider.yml
apiVersion: 1
providers:
  - name: from-git
    folder: Servers # the folder they appear in
    type: file
    options:
      path: /var/lib/grafana/dashboards # mount your JSON files here

The lab's node-overview.json (a fixed uid of node-overview, six panels, one variable) was mounted at that path, and Grafana loaded it at startup into the Servers folder:

$ curl -s -u admin:admin "localhost:3000/api/search?query=node"
[
    {
        "uid": "node-overview",
        "title": "Node overview",
        "url": "/d/node-overview/node-overview",
        "type": "dash-db",
        "folderTitle": "Servers",
        ...
    }
]
curl -s -u admin:admin "localhost:3000/api/search?query=node"
  • curlmake an HTTP request
  • -ssilent: no progress bar

Grafana checks the folder every 10 seconds by default. When the load-panel fix above was saved to the JSON file, the dashboard updated without a restart.

Start from a community dashboard

grafana.com/grafana/dashboards has thousands of ready dashboards. "Node Exporter Full" (ID 1860) is the classic one for node_exporter. Import it by ID, learn from its queries, then build a smaller one your team will actually read.

In production

Provision data sources and dashboards from Git and treat hand-made changes as drafts. Put Grafana behind your company login (OAuth or SAML), give most people the Viewer role, and change the default admin password on day one. Grafana's own database (SQLite by default) holds users and settings: use PostgreSQL or MySQL if several Grafana servers share it.

⌘
Mini mission

Build and provision a node dashboard

Build a dashboard called 'Node overview' with an instance variable and at least four panels: CPU used, memory used, root disk used and load per core, each with a unit and thresholds. Export it as JSON, delete it from Grafana, then bring it back through a dashboard provider and confirm it with the search API.

Input: Prometheus and node_exporter from lesson 2Output: node-overview.json in a provisioning folder
Reveal one safe solution
$ mkdir -p provisioning/datasources provisioning/dashboards dashboards
$ # save prometheus.yml and provider.yml from this lesson, and your exported JSON as dashboards/node-overview.json
$ docker rm -f grafana
$ docker run -d --name grafana --network mon -p 3000:3000 \
    -v $PWD/provisioning:/etc/grafana/provisioning:ro \
    -v $PWD/dashboards:/var/lib/grafana/dashboards:ro grafana/grafana
$ curl -s -u admin:admin "localhost:3000/api/search?query=node"
mkdir -p provisioning/datasources provisioning/dashboards dashboards
  • mkdirmake a directory
  • -pcreate missing parents, no error if it exists
docker rm -f grafana
  • dockerbuild and run containers
  • rmdelete a container
  • -fstop it first if running
  • grafanathe container
docker run -d --name grafana --network mon -p 3000:3000 \
  • runstart a new container
  • -drun in the background
  • --name grafananame the container
  • --network monwhich network to join
  • -p 3000:3000publish a port, host:container
  • \the image to start
curl -s -u admin:admin "localhost:3000/api/search?query=node"
  • curlmake an HTTP request
  • -ssilent: no progress bar

The search answer should list "Node overview" in the "Servers" folder. Change the server dropdown and check every panel follows. If one panel says "No data", run its query in Explore and look for a label mismatch.

Try it yourself

0 of 6 steps done

Run each task in your own account or terminal, then tick it off.

Knowledge check

Quick check

Question 1 of 5

Where does Grafana keep the CPU numbers you see on a dashboard?

Go deeper

Official reading for this lesson

  • DocsGrafana documentationGrafana Labs ↗
  • DocsConfigure the Prometheus data sourceGrafana Labs ↗
  • DocsProvision Grafana (data sources and dashboards)Grafana Labs ↗
  • DocsDashboard variablesGrafana Labs ↗
  • GuideDashboard best practicesGrafana Labs ↗
  • DocsRun Grafana in DockerGrafana Labs ↗

Or get every quiz answer right and it completes itself.

Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.

← PreviousPrometheus and PromQLNext lesson →Alerting with Alertmanager