Prometheus Server Tutorial
Introduction to Prometheus Server
Prometheus is an open-source systems monitoring and alerting toolkit originally built at SoundCloud. It has become a popular choice for cloud-native applications and microservices due to its flexibility and powerful querying language. The Prometheus server collects and stores metrics as time series data, enabling users to monitor the performance and health of applications in real-time.
Installation of Prometheus Server
To install Prometheus on your system, follow the steps below. This example will demonstrate how to install Prometheus on a Linux machine.
Step 1: Download Prometheus
Visit the official Prometheus download page and download the latest version. You can use the following command:
Step 2: Extract the Tarball
Step 3: Navigate to the Directory
Step 4: Start Prometheus
After running the last command, your Prometheus server should be up and running. By default, it listens on port 9090.
Prometheus Configuration
The configuration file for Prometheus is typically named prometheus.yml
. This file defines the scrape configurations and other settings for your Prometheus server. Below is a sample configuration:
Sample prometheus.yml:
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
In this sample, we define a scrape configuration for Prometheus itself. The server will scrape metrics from itself at the specified target.
Querying Metrics in Prometheus
Once your Prometheus server is running and collecting metrics, you can query these metrics using the Prometheus Query Language (PromQL). You can access the Prometheus web interface by navigating to http://localhost:9090
in your web browser.
Here are some basic examples of queries you can run:
Get the current CPU usage:
Get memory usage:
Setting Up Alerts
Alerts in Prometheus can be set up using the Alertmanager component. Alerts are defined in the prometheus.yml
file under the alerting
section. Here’s an example of how to set up a basic alert for high CPU usage:
Sample alert configuration:
groups:
- name: example_alert
rules:
- alert: HighCPUUsage
expr: rate(node_cpu_seconds_total[5m]) > 0.9
for: 5m
labels:
severity: critical
annotations:
summary: "High CPU Usage Detected"
description: "CPU usage is above 90% for more than 5 minutes."
Conclusion
Prometheus is a powerful monitoring tool that allows you to collect, store, and query metrics efficiently. In this tutorial, we covered the installation of Prometheus Server, its configuration, querying metrics with PromQL, and setting up alerts. With these foundations, you can now monitor your applications and systems effectively.