How to Setup Prometheus
Introduction Prometheus is an open-source monitoring and alerting toolkit designed specifically for reliability and scalability in modern cloud-native environments. Originally developed by SoundCloud, Prometheus has become a cornerstone in the observability landscape, helping developers, system administrators, and DevOps teams collect, store, and query metrics from various systems and applications
Introduction
Prometheus is an open-source monitoring and alerting toolkit designed specifically for reliability and scalability in modern cloud-native environments. Originally developed by SoundCloud, Prometheus has become a cornerstone in the observability landscape, helping developers, system administrators, and DevOps teams collect, store, and query metrics from various systems and applications.
Setting up Prometheus effectively is crucial for gaining deep insights into application performance, system health, and infrastructure behavior. This tutorial provides a comprehensive guide to help you install, configure, and optimize Prometheus monitoring in your environment. Whether you're new to Prometheus or looking to refine your setup, this detailed walkthrough will equip you with the knowledge needed to leverage Prometheus for robust monitoring solutions.
Step-by-Step Guide
1. Prerequisites
Before beginning the setup, ensure you have the following:
- A Linux-based server or local machine (Ubuntu/Debian recommended)
- Basic knowledge of command-line interface (CLI) operations
- Access rights to install software and modify configuration files
- Optional: Docker installed if you want to run Prometheus in containers
2. Installing Prometheus
There are multiple ways to install Prometheus; the two most common are via direct binary download or using Docker.
Installing via Binary
Follow these commands to download and install Prometheus directly:
sudo useradd --no-create-home --shell /bin/false prometheus
sudo mkdir /etc/prometheus
sudo mkdir /var/lib/prometheus
cd /tmp
wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz
tar xvf prometheus-2.45.0.linux-amd64.tar.gz
cd prometheus-2.45.0.linux-amd64
sudo cp prometheus /usr/local/bin/
sudo cp promtool /usr/local/bin/
sudo cp -r consoles /etc/prometheus
sudo cp -r console_libraries /etc/prometheus
sudo cp prometheus.yml /etc/prometheus/
Adjust permissions accordingly:
sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus /usr/local/bin/prometheus /usr/local/bin/promtool
Running Prometheus as a Service
Create a systemd service file for Prometheus to manage the daemon:
sudo nano /etc/systemd/system/prometheus.service
Insert the following configuration:
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries
[Install]
WantedBy=multi-user.target
Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable prometheus
sudo systemctl start prometheus
sudo systemctl status prometheus
3. Configuring Prometheus
The main configuration file prometheus.yml controls which targets Prometheus scrapes metrics from.
Example minimal prometheus.yml:
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
This configuration scrapes Prometheuss own metrics every 15 seconds.
Adding Additional Targets
To monitor other applications or exporters, add their endpoints under scrape_configs. For example, to monitor a Node Exporter:
- job_name: 'node_exporter'
static_configs:
- targets: ['192.168.1.10:9100']
Restart Prometheus to apply changes:
sudo systemctl restart prometheus
4. Accessing Prometheus Web UI
Prometheus runs a web interface by default on port 9090. Open your browser and navigate to:
http://your-server-ip:9090
Use this UI to query metrics, explore targets, and visualize data.
5. Setting up Exporters
Prometheus collects data by scraping exporters that expose metrics in Prometheus format. Common exporters include:
- Node Exporter: Exposes OS and hardware metrics
- Blackbox Exporter: Monitors endpoints via probes
- MySQL Exporter: Exposes MySQL server metrics
Install and configure exporters on your target systems and add their endpoints to prometheus.yml.
6. Basic Querying with PromQL
Prometheus Query Language (PromQL) is a powerful tool to extract and manipulate metrics.
Examples:
- CPU Usage:
100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) - Memory Usage:
node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes
Best Practices
1. Organize Scrape Targets Logically
Group related targets under specific job names and labels to simplify querying and alerting.
2. Use Relabeling for Efficient Target Management
Leverage relabeling rules in prometheus.yml to dynamically modify labels or filter targets.
3. Monitor Prometheus Itself
Scrape Prometheuss own metrics to understand its health and performance.
4. Set Up Alerting Early
Integrate Alertmanager with Prometheus to create actionable alerts and avoid missing critical issues.
5. Use Persistent Storage
Configure Prometheus to use persistent storage to avoid losing data on restarts. Adjust retention policies based on your monitoring needs.
6. Secure Your Installation
Restrict access to the Prometheus web UI and endpoints using firewalls or authentication proxies.
7. Regularly Update Prometheus
Keep Prometheus and exporters updated to benefit from the latest features and security patches.
Tools and Resources
1. Official Prometheus Documentation
The best resource for detailed configuration and advanced topics:
2. Prometheus Exporters
Explore the list of community and official exporters here:
https://prometheus.io/docs/instrumenting/exporters/
3. PromQL Tutorial
Interactive tutorials to master Prometheus Query Language:
https://promlabs.com/promql-tutorial/
4. Grafana
Grafana is a popular visualization tool that integrates seamlessly with Prometheus:
5. Alertmanager
For advanced alerting capabilities, use Prometheus Alertmanager:
https://prometheus.io/docs/alerting/latest/alertmanager/
Real Examples
Example 1: Monitoring a Linux Server with Node Exporter
Install Node Exporter on your Linux server:
wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz
tar xvf node_exporter-1.6.1.linux-amd64.tar.gz
cd node_exporter-1.6.1.linux-amd64
./node_exporter &
Add this to your prometheus.yml:
- job_name: 'node_exporter'
static_configs:
- targets: ['your-server-ip:9100']
Restart Prometheus and navigate to the web UI to query metrics like node_cpu_seconds_total.
Example 2: Setting Up Blackbox Exporter for HTTP Monitoring
Install Blackbox Exporter:
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.24.0/blackbox_exporter-0.24.0.linux-amd64.tar.gz
tar xvf blackbox_exporter-0.24.0.linux-amd64.tar.gz
cd blackbox_exporter-0.24.0.linux-amd64
./blackbox_exporter &
Add this scrape config:
- job_name: 'blackbox'
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets:
- https://example.com
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 127.0.0.1:9115
Blackbox Exporter address
This configuration probes the HTTP endpoint at https://example.com and collects status metrics.
FAQs
Q1: Can Prometheus monitor Windows systems?
Yes, by using exporters such as the Windows Exporter (formerly WMI Exporter), you can scrape metrics from Windows hosts.
Q2: What is the difference between Prometheus and other monitoring tools?
Prometheus is designed for multi-dimensional data collection with a powerful query language (PromQL). It is optimized for cloud-native environments and offers native integration with Kubernetes.
Q3: How much storage does Prometheus require?
Storage needs depend on scrape intervals, retention period, and number of metrics. Prometheus uses a time-series database optimized for efficient storage, but you should size storage based on your usage.
Q4: Is Prometheus suitable for long-term storage?
By default, Prometheus is optimized for medium-term storage (weeks to months). For long-term storage, integrate with remote storage solutions such as Thanos or Cortex.
Q5: How do I secure Prometheus?
Prometheus does not provide built-in authentication or encryption. Use reverse proxies with TLS and authentication or network policies to secure access.
Conclusion
Setting up Prometheus is an essential step for organizations aiming to implement effective monitoring in their infrastructure and applications. This tutorial covered the installation, configuration, and best practices for Prometheus deployment, along with real-world examples to help you get started quickly.
By following these guidelines, you can ensure reliable metric collection, insightful querying, and proactive alerting within your systems. Prometheuss flexibility and powerful ecosystem make it a critical tool in the modern observability stack, helping you maintain high availability and performance for your infrastructure.