Introduction
In today’s fast-paced, data-driven environment, monitoring the performance of your services and infrastructure is essential to ensure stability and efficiency. Prometheus and Grafana are two powerful open-source tools that enable you to achieve this with ease.
Prometheus is a monitoring and alerting toolkit that scrapes metrics from various services and stores them in a time-series database. Grafana is a visualization tool that helps you create interactive dashboards to display and analyze the metrics gathered by Prometheus.
In this blog, we will walk you through the process of setting up Prometheus and Grafana using Docker Compose. The goal is to scrape the metrics of a specific service and visualize the data in Grafana. Whether you’re monitoring application performance or infrastructure, this guide will help you create a seamless, containerized solution for data collection and visualization.
Prerequisites
Before we begin, ensure you have the following installed on your local environment:
Docker
Docker Compose
Basic understanding of Prometheus and Grafana
Step 1: Setting Up Docker Compose for Prometheus and Grafana
We will start by creating a docker-compose.yml file that sets up containers for both Prometheus and Grafana. The goal is to use Prometheus to scrape the metrics of a specific service or instance.
Here’s a simple Docker Compose file that brings up Prometheus and Grafana:
services:
grafana:
image: grafana/grafana
container_name: grafana
ports:
- "3000:3000"
restart: unless-stopped
volumes:
- /data/grafana-data:/var/lib/grafana
environment:
- GF_SECURITY_ADMIN_USER=admin
- GF_SECURITY_ADMIN_PASSWORD=admin
prometheus:
image: prom/prometheus:main
container_name: prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
ports:
- 9090:9090
restart: unless-stopped
volumes:
- ./prometheus:/etc/prometheus
- /data:/prometheus
In this configuration:
Grafana is set up to expose its dashboard on port 3000 and store its data in /data/grafana-data.
Prometheus exposes its metrics endpoint on port 9090 and uses a custom prometheus.yml configuration file stored in the /prometheus directory.
Step 2: Configuring Prometheus
Prometheus needs a configuration file to know which endpoints to scrape for metrics. For this setup, we are assuming that we want to scrape a service that is exposing metrics on a custom endpoint (e.g., /metrics).
Create a prometheus.yml configuration file in prometheus dirctory.
global:
scrape_interval: 1m
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 1m
static_configs:
- targets: ['http://**********:9090']
- job_name: "job_name"
scrape_interval: 10s
metrics_path: /metrics
scheme: https
basic_auth:
username: _user_******
password: ********
http_sd_configs:
- url: https://***************
refresh_interval: 60s
basic_auth:
username: _user_******
password: ********
In this configuration:
The global section defines how often Prometheus will scrape metrics (every 1 minute by default).
Under scrape_configs, Prometheus will scrape itself on http://localhost:9090.
Replace URL with the actual IP address or hostname and port of the service you want to monitor. The service must expose a /metrics endpoint that Prometheus can scrape.
Step 3: Running Docker Compose
Once you have the docker-compose.yml and prometheus.yml files set up, you can bring up the containers using Docker Compose.
Run the following command to start both the Prometheus and Grafana services:
docker-compose up -d
This command will bring up both the Prometheus and Grafana containers in detached mode. Prometheus will start scraping metrics from the specified target, and Grafana will be available to visualize the data.
Step 4: Setting Up Grafana
If your Grafana instance is running on a remote server with the public IP 68.98.7.193 just like this one, you should use that IP address in place of localhost.
Open a browser and navigate to http://68.98.7.193:3000 (or the IP address where Grafana is running). You’ll be prompted to log in. The default credentials are:
Username: admin
Password: admin
After logging in, Grafana will prompt you to change the password.
Add Prometheus as a data source:
Navigate to Configuration -> Data Sources.
Click Add Data Source and select Prometheus.
In the URL field, enter http://prometheus:9090.
Click Save & Test to verify the connection.
Step 5: Creating Dashboards in Grafana
Now that Prometheus is set up as a data source, you can create dashboards to visualize your data.
Navigate to Create -> Dashboard.
Click Add New Panel.
In the Query section, select Prometheus as the data source.
Enter the appropriate Prometheus query, customize your graph to show data in the way you want—adjust time ranges, labels, legends, etc.
Click Apply to save the panel, and repeat the process to create more panels if needed. You can now monitor the performance of your service through real-time visualizations in Grafana!
Conclusion:
By setting up Prometheus and Grafana with Docker Compose, you can easily monitor and visualize the metrics of your services. With this setup, Prometheus handles scraping and collecting data from your service, and Grafana allows you to visualize that data in dashboards. This setup can be extended to monitor various services, infrastructure components, or custom applications with minimal configuration.
Now you’re ready to start building dashboards that provide insights into your system's performance!