This is the second part of an article about how to monitor a Raspberry Pi using Prometheus and node_explorer. The first part is available here.
4. Install Grafana in Dokku
As in the first part we did with prometheus, we'll create the Dokku application, and allow to Grafana to write in the data folder.
# Create the Dokku application dokku apps:create grafana # Create the directory where we'll store the prometheus data mkdir -p /var/lib/dokku/data/storage/grafana/ # The Grafana container uses the user id `472`, so we change the permissions to do that directory writable # Related: https://github.com/grafana/grafana/blob/293ecbdd4fe1e0b636eb7d0a7e0184ace0f20a80/Dockerfile#L39-L40 sudo chown -R 472:472 /var/lib/dokku/data/storage/grafana/
In our local, we'll create the git repository, and add the files needed:
# Initialize the repository git init # Add a new remote called `dokku` with our Dokku host git remote add dokku firstname.lastname@example.org:grafana
Dockerfile(using the Grafana as base, we'll include the specific configuration for nginx)
nginx.conf.sigil(just to increase the size limit when we do POST using grafana, for example to do snapshots of some dashboard).
app.json(optional, but I'm using the dokku-require plugin, this file configures the mounted volumes).
As we're using a Dockerfile Deployment, we're adding this
sigil template to our application
WORKDIR. It's a copy from the default template, just adding the
client_max_body_size config, which allows to
POST bigger requests, for example when we'll export a Grafana snapshot.
After push to our Dokku repository, we'll be able to connect using the default domain and port, but we could setup those:
dokku domains:add grafana grafana.mydomain.com dokku proxy:ports-set grafana http:80:3000 dokku letsencrypt grafana
You can open now Grafana.mydomain.com in your browser, and introduce the default user: admin / admin.
5. Connecting Grafana to Prometheus
Grafana and Prometheus are working in two different containers, so if we want that Grafana read the data from Prometheus, we have to connect both networks.
Using Dokku, it's easy to create a new network, and use it in both containers:
dokku network:create grafana-prometheus dokku network:set prometheus attach-post-create grafana-prometheus dokku network:set grafana attach-post-create grafana-prometheus # After that, we could see a report of the networks per application dokku@ubuntu-dokku:~$ dokku network:report prometheus =====> prometheus network information Network attach post create: grafana-prometheus Network attach post deploy: Network bind all interfaces: false Network web listeners: 172.17.0.7:9090 # But we could use also the name of the container, for example: http://prometheus.web.1:9090
With this URL, we can create a new datasource from our Grafana website: https://grafana.mydomain.com/datasources, adding the URL of Prometheus.
After that, we can start to do some queries over prometheus, there are a bunch of examples on their website.
6. Importing a node_exporter dashboard
An interesting feature of Grafana, is the capability to import dashboards, even they have a repository with a lot of these.
Thanks to that, we can find a Node Exporter Full dashboard, which we could import just using the ID (1860) in https://grafana.mydomain.com/dashboard/import, and we'll have a dashboard like the following.
In this part, we've discovered how to install Grafana in Dokku, and also how to connect it to the Prometheus installed in the first part of this article to use as a data source.
As you've seen, this is a cheap way to get monitored a server, from a Raspberry Pi in your home, to a more professional setup in your company, because you can even define alarms to get notified when something is going wrong.