Lab: Setting Up Prometheus and Grafana for Monitoring
Objective:
This lab will guide you through the process of setting up Prometheus and Grafana for
monitoring system metrics. Prometheus will be used to collect and store metrics, while
Grafana will be used to visualize the data.
Prerequisites:
• Docker and Docker Compose installed on your machine.
• Basic knowledge of Linux commands and system monitoring.
• Internet access to pull Docker images.
Steps:
Step 1: Set Up Prometheus
1. Create a directory for Prometheus:
First, create a working directory for Prometheus and Grafana.
bash
mkdir prometheus-grafana
cd prometheus-grafana
2. Create a Prometheus configuration file:
In this directory, create a file named prometheus.yml. This will contain the configuration for
Prometheus to scrape metrics.
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
This configuration tells Prometheus to scrape metrics from itself (localhost:9090) every 15
seconds.
3. Set Up Docker Compose for Prometheus and Grafana:
Create a docker-compose.yml file to define the Prometheus and Grafana services.
version: '3'
services:
prometheus:
image: prom/prometheus
container_name: prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
grafana:
image: grafana/grafana
container_name: grafana
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
This file specifies two services:
o Prometheus: Exposes metrics on port 9090.
o Grafana: Exposes the dashboard on port 3000 and sets the admin password
to admin.
4. Start Prometheus and Grafana containers:
Now, start the services by running the following Docker Compose command:
bash
docker-compose up -d
This command will download the necessary Docker images and start the containers in
detached mode.
Step 2: Access Prometheus and Grafana
1. Access Prometheus:
Open your browser and navigate to http://localhost:9090. You should see the Prometheus
web interface.
o Try querying up to check if Prometheus is working correctly and scraping
metrics.
2. Access Grafana:
Open your browser and go to http://localhost:3000. Log in using the default credentials:
o Username: admin
o Password: admin (you can change this after the first login).
Step 3: Configure Grafana to Use Prometheus as a Data Source
1. Add Prometheus as a Data Source in Grafana:
o After logging into Grafana, click the gear icon ( ) on the left sidebar and
select Data Sources.
o Click Add data source, and choose Prometheus from the list.
o In the URL field, enter http://prometheus:9090.
o Click Save & Test to confirm that Grafana can connect to Prometheus.
Step 4: Create a Dashboard in Grafana
1. Create a New Dashboard:
o Click the + button on the left sidebar and select Dashboard.
o Click Add new panel.
2. Add a Prometheus Query:
In the Query section, select Prometheus as the data source. For example, to display
system metrics like CPU usage, you can enter the following Prometheus query:
rate(node_cpu_seconds_total{mode="idle"}[5m])
This query retrieves the idle CPU time rate over the last 5 minutes.
3. Customize the Panel:
You can adjust the visualization type (e.g., Graph, Gauge, or Bar) depending on how you
want the data to appear. For example, you can choose a Graph to see the time-series data
of the CPU usage.
4. Save the Dashboard:
Once you're satisfied with the panel, click Apply to add it to the dashboard. Then, click the
disk icon in the upper-right corner to save the dashboard.
Step 5: Explore Metrics
• You can add multiple panels to visualize different types of metrics. Some common
queries include:
o node_memory_MemAvailable_bytes for available memory.
o node_disk_io_time_seconds_total for disk I/O.
o node_network_receive_bytes_total for network traffic.
• Experiment with different queries and visualizations to create a comprehensive
monitoring dashboard for your system.
Conclusion:
You've successfully set up Prometheus and Grafana on your machine. Prometheus is
collecting system metrics, and Grafana is visualizing them in real-time. You can continue to
expand your dashboard by adding more panels, alerts, and different types of metrics.