Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

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:

wget https://github.com/prometheus/prometheus/releases/download/v2.32.1/prometheus-2.32.1.linux-amd64.tar.gz

Step 2: Extract the Tarball

tar xvfz prometheus-2.32.1.linux-amd64.tar.gz

Step 3: Navigate to the Directory

cd prometheus-2.32.1.linux-amd64

Step 4: Start Prometheus

./prometheus --config.file=prometheus.yml

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:

rate(node_cpu_seconds_total[5m])

Get memory usage:

node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100

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.