mariachiacero.com

Prometheus Monitoring Stack: Comprehensive Setup Guide with Docker

Written on

Chapter 1: Introduction to the Prometheus Monitoring Stack

In this guide, we will delve into the steps required to establish a complete Prometheus monitoring stack utilizing Docker Compose. Our focus will be on the configuration and deployment of Prometheus for metric collection, Grafana for data visualization, and Alertmanager for managing alerts. This tutorial is geared towards individuals who have a basic understanding of Docker and aim to implement an effective monitoring solution for their applications and infrastructure.

Overview of Prometheus Monitoring Stack Setup

Chapter 2: Understanding Prometheus

Prometheus is a widely-used open-source toolkit designed for monitoring and alerting. It collects and stores metrics from various sources and features a robust query language, PromQL, for analyzing and visualizing metrics. This tool is particularly effective for monitoring applications, services, and infrastructure.

For instance, consider a web application that exposes a metric resembling the following:

http_server_requests_seconds_count{exception="None", method="GET", outcome="SUCCESS", status="200", uri="/actuator/health"} 435

This metric indicates that the /actuator/health endpoint was successfully accessed 435 times via a GET request. Prometheus can also trigger alerts if certain thresholds are crossed, such as if the endpoint returns the status code 500 more than a hundred times within a five-minute window.

Section 2.1: Setting Up Prometheus Configuration

To configure Prometheus, we need to create three key files:

  1. prometheus/prometheus.yml — This file contains the Prometheus configuration.
  2. prometheus/alert.yml — This file defines the alerts that Prometheus will monitor.
  3. docker-compose.yml — This file orchestrates the deployment of our services.

We will begin by adding the following content to prometheus/prometheus.yml:

global:

scrape_interval: 30s

scrape_timeout: 10s

rule_files:

  • alert.yml

scrape_configs:

  • job_name: services

    metrics_path: /metrics

    static_configs:

    • targets:

      • 'prometheus:9090'
      • 'idonotexists:564'

In this configuration, scrape_configs specifies the locations of our applications. The first entry points to the Prometheus service itself, while the second serves as a placeholder for demonstration purposes.

The rule_files section indicates where to locate our alert rules, which we will cover shortly. The scrape_interval determines how frequently Prometheus checks for new metric values, and if a scrape exceeds the specified scrape_timeout, it is canceled.

Next, we will configure the alert.yml file with the following content:

groups:

  • name: DemoAlerts

    rules:

    • alert: InstanceDown

      expr: up{job="services"} < 1

      for: 5m

Here, up is a built-in metric from Prometheus that returns zero if the services were unreachable during the last scrape. The {job="services"} filter allows us to focus solely on metrics tagged with the service name defined in prometheus.yml.

To launch Prometheus, we will add the following to docker-compose.yml:

version: '3'

services:

prometheus:

image: prom/prometheus:v2.46.0

ports:

  • 9000:9090

volumes:

  • ./prometheus:/etc/prometheus
  • prometheus-data:/prometheus

command: --web.enable-lifecycle --config.file=/etc/prometheus/prometheus.yml

volumes:

prometheus-data:

This configuration mounts our Prometheus folder in the correct location for the image to utilize our configuration. The volume prometheus-data is designated to store the scraped data, preserving it after restarts.

To reload configuration files without restarting Prometheus, you can use the following command:

curl -X POST http://localhost:9000/-/reload

To start Prometheus, run:

docker-compose up -d

And access the UI at http://localhost:9000 in your browser, where you can run ad-hoc queries on your metrics.

Chapter 3: Visualizing Data with Grafana

Grafana is a powerful open-source platform that facilitates monitoring and observability, allowing you to create, explore, and share dashboards. It supports numerous data sources, including Prometheus, and offers various visualization options.

The first video explains how to set up a real-time monitoring stack with Prometheus, Grafana, Node Exporter, and Docker using Docker Compose. It provides a practical overview of the components involved and their interactions.

Section 3.1: Configuring Grafana

Grafana can function without configuration files; however, to streamline the setup, we will configure Prometheus as a data source. Create the file grafana/provisioning/datasources/prometheus_ds.yml and insert the following:

datasources:

This configuration informs Grafana about our Prometheus data source, with the URL pointing to the Prometheus server accessible within the Docker network.

Next, update the docker-compose.yml file to include the Grafana service:

grafana:

image: grafana/grafana:10.0.0

ports:

  • 3000:3000

restart: unless-stopped

volumes:

  • grafana-data:/var/lib/grafana
  • ./grafana/provisioning:/etc/grafana/provisioning

volumes:

grafana-data:

The first volume mounts our data source configuration, while the second persists Grafana's data, including dashboards and user settings. To start both Prometheus and Grafana, run:

docker-compose up -d

Access Grafana at http://localhost:3000 in your browser, using admin as both the username and password.

After logging in, you can create your first dashboard by clicking the "+" icon and selecting "New Dashboard." Click "Add visualization" to begin creating visualizations using the Prometheus data source.

To visualize metrics, you can enter PromQL queries. For instance, to display the total number of HTTP requests over time, enter:

increase(prometheus_http_requests_total[1m])

To learn more about PromQL, refer to the official documentation. You can also import pre-existing dashboards from the Grafana Dashboards page.

Chapter 4: Managing Alerts with Alertmanager

Alertmanager is an essential tool that manages alerts sent by Prometheus, handling deduplication, grouping, and routing of alerts to the appropriate notification channels, such as email or Slack.

The second video demonstrates how to effortlessly install Grafana, Prometheus, and Node Exporter with Docker, providing an easy approach to server monitoring.

Section 4.1: Configuring Alertmanager

To integrate Alertmanager, add the following configuration to docker-compose.yml:

alertmanager:

image: prom/alertmanager:v0.25.0

ports:

  • 9093:9093

volumes:

  • ./alertmanager:/config
  • alertmanager-data:/data

command: --config.file=/config/alertmanager/alertmanager.yml

volumes:

alertmanager-data:

Alertmanager will save silence configurations to the specified volume. Next, create an alertmanager.yml file in the alertmanager folder with the following content:

receivers:

This configuration details the channels for sending alerts, with email as the primary method. The route section specifies which alerts to send, and you can filter them based on tags.

To inform Prometheus about Alertmanager, add the following to prometheus/prometheus.yml:

alerting:

alertmanagers:

  • static_configs:

    • targets:

      • 'alertmanager:9093'

Section 4.2: Testing and Using Alertmanager

Run docker-compose up to start all services. Access the Alertmanager UI at http://localhost:9093 to monitor alerts. After a few minutes, test alerts will trigger, and you can verify them in both the Prometheus and Alertmanager interfaces.

The Alertmanager UI allows you to view active and silenced alerts, manage notification receivers, and configure alert routing. To silence an alert, navigate to the "Silences" tab, click "New Silence," and specify the alert and duration for the silence.

Conclusion

In summary, the Prometheus monitoring stack offers a comprehensive solution for monitoring both applications and infrastructure. By utilizing Docker Compose, we can efficiently deploy and manage the key components: Prometheus, Grafana, and Alertmanager. This setup empowers users to collect metrics, visualize data, and manage alerts effectively, making it suitable for both small applications and large-scale infrastructures.

Your Feedback Matters

If this guide proved helpful, please leave a comment or give it a clap. Your feedback is invaluable and aids in enhancing future content. Thank you!

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

The Fleeting Nature of Human Achievement: A Reflection

A contemplation on the transient impact of our actions and the significance of serving others.

Harnessing ChatGPT for Rapid Programming: A 24-Hour Journey

Discover how I developed a Chrome extension using ChatGPT in under 24 hours, transforming ideas into reality with AI assistance.

Investing in Yourself: The Key to Unlocking Success and Growth

Discover how investing in your skills and talents can lead to personal and professional growth while enhancing your self-worth.

Understanding the

Discover how the wealthy leverage assets to maintain and grow their wealth through strategic borrowing and estate planning.

Embracing Self-Acceptance: Redefining Sexy Beyond the Norm

Exploring the concept of being sexy beyond societal expectations and embracing self-acceptance.

Exploring the Mysteries of Death: What Happens After We Die?

Delve into the observable changes our bodies undergo after death, exploring the mysteries and philosophies surrounding this ultimate question.

Embracing the Challenges of My Medium Writing Journey

Discover the pride and challenges in my Medium writing journey amidst daily life distractions.

Mindful Eating: How Your Choices Shape Your Well-Being

Explore how mindful eating and consumption influence your overall well-being and life experience.