Automated Monitoring With Grafana and Prometheus
In this article, I would like to explain a Docker image that I created to keep Grafana dashboards in sync across environments.
Join the DZone community and get the full member experience.
Join For FreeDepending on the number of environments in which you have monitoring enabled, it may be cumbersome to keep all your Grafana dashboards synchronized across environments when changes occur.
In this article, I would like to show a Docker image I created to keep Grafana dashboards in sync across environments.
What's in the Image
- Prometheus.
- Grafana.
- Supervisor.
How to Use It
With all options:
docker run --name automated-grafana -d -p 9090:9090 -p 6666:6666 \
-e "ENVIRONMENT=prod" \
-e "GF_SERVER_HTTP_PORT=6666" \
-e "WAITING_TIME=20" \
-v `pwd`/prometheus-config:/prometheus-config \
-v `pwd`/dashboards:/dashboards \
-v `pwd`/users:/users \
-v `pwd`/sources:/sources \
serragnoli/automated-grafana-prometheus
-p 9090:9090
: Exposes the Prometheus default port to the host.-p 6666:6666
: Exposes a custom Grafana port that was determined by-e "GF_SERVER_HTTP_PORT=6666"
; both of these parameter values need to match.-e "ENVIRONMENT=prod"
: Determines which Prometheus config to use. Ensure that aprometheus-<env> .yml
suffixed with the environment where you'll deploy to exists in the directory. In this example, the value isprod
and/prometheus-config/prometheus-prod.yml
is expected to exist.-e "WAITING_TIME=20"
: By default, each JSON file is loaded 10 seconds apart from each other. There may be situations that 10 seconds may be too much or too little time. When that's the case, use this variable to control the interval of loading each JSON file.-v `pwd`/dashboards:/dashboards
-v `pwd`/users:/users
-v `pwd`/sources:/sources
: Map the directories containing the JSON files to be loaded by Grafana in the container. It's worth mentioning that the/dashboards
directory must only contain dashboard JSON files and the same applies to the other directories.
After running the above command, you should have Prometheus running on localhost:9090
and Grafana running on localhost:6666
.
Advantages
- You can run the same image everywhere; just use a Prometheus config file that points to
/metrics
endpoints for the environment you're deploying to. - You can load as many dashboards, users, and data-source JSON files as you want by only mapping the directories that contain these files from the host to the container.
- You get fewer moving parts in your monitoring system. Therefore, there are fewer things to worry about, i.e. lost/slow network connectivity between Prometheus and Grafana.
Disadvantages
- Difficulty to scale each component individually as they are in the same container;
- Breaking the Docker rule of thumb one process per Docker container.
Final Considerations
I've added sample JSON files to the image page in DockerHub: dashboard.json
, user.json
, and data-source.json
.
I can't tell if I have tried to reinvent the wheel with this image, but the fact is that I couldn't find anything already automating Grafana dashboards in the same way.
As I mentioned above, this is currently my favorite solution and if I learn a better way of doing it, I'll update this article.
Opinions expressed by DZone contributors are their own.
Comments