Prometheus is a powerful monitoring and alerting toolkit commonly used for monitoring systems and applications, especially in microservices environments. It collects and stores metrics as time-series data, which is ideal for tracking the performance of servers, applications, databases, and services. In this tutorial, you will learn how to install and configure Prometheus on your AnonVM server.
Table of Contents
- Prerequisites
- What is Prometheus?
- Installing Prometheus on AnonVM
- Configuring Prometheus
- Starting Prometheus and Verifying the Installation
- Conclusion
1. Prerequisites
Before installing Prometheus, ensure your system meets the following prerequisites:
- Operating System: Ubuntu/Debian or CentOS/RHEL-based systems.
- Root or Sudo Access: Root privileges are required for installing and configuring Prometheus.
- System Resources: At least 1 GB of RAM and 2 GB of disk space (depending on the scale of your monitoring).
- Internet Connection: Required to download Prometheus and its dependencies.
2. What is Prometheus?
Prometheus is an open-source monitoring and alerting toolkit that is highly scalable and designed for reliability. It collects time-series data, which consists of metrics collected over time, from configured targets at specified intervals. Prometheus can handle a wide range of metrics, from system-level metrics like CPU usage to application-level metrics.
Key features of Prometheus include:
- Time-Series Data Storage: Prometheus stores metrics data in a time-series database.
- Flexible Query Language: Prometheus uses PromQL (Prometheus Query Language) to query time-series data.
- Alerting: It can trigger alerts based on defined conditions using Alertmanager.
- Integration with Grafana: For visualization of metrics and dashboards.
Prometheus is widely used in cloud-native and containerized environments like Kubernetes, where it integrates well with cAdvisor, node_exporter, and other exporters.
3. Installing Prometheus on AnonVM
Step 1: Update the System
Ensure your system is up-to-date. For Ubuntu/Debian:
sudo apt update && sudo apt upgrade -y
For CentOS/RHEL:
sudo yum update -y
Step 2: Download Prometheus
Prometheus provides precompiled binaries for different operating systems. Visit the official Prometheus download page for the latest version. You can also download Prometheus directly using wget.
For Ubuntu/Debian:
wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz
For CentOS/RHEL:
wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz
Step 3: Extract the Files
Extract the downloaded tar file:
tar -xvzf prometheus-2.45.0.linux-amd64.tar.gz
Move into the extracted directory:
cd prometheus-2.45.0.linux-amd64
Step 4: Install Prometheus
Move the Prometheus binary and related files to /usr/local/bin
for easier execution:
sudo mv prometheus /usr/local/bin/
sudo mv promtool /usr/local/bin/
sudo mv consoles /etc/prometheus
sudo mv console_libraries /etc/prometheus
Step 5: Create Prometheus User and Set Permissions
It’s a best practice to run Prometheus under a non-root user. Create a prometheus user and group:
sudo useradd --no-create-home --shell /bin/false prometheus
Set proper ownership for the Prometheus directories:
sudo chown -R prometheus:prometheus /usr/local/bin/prometheus
sudo chown -R prometheus:prometheus /etc/prometheus
4. Configuring Prometheus
Prometheus configuration is defined in the prometheus.yml
file. The default configuration file is located in /etc/prometheus/prometheus.yml
. Let’s configure Prometheus to monitor your local machine (or any other server).
Step 1: Edit the Configuration File
Open the prometheus.yml
file for editing:
sudo nano /etc/prometheus/prometheus.yml
Here is a simple example configuration for monitoring the local machine using the node_exporter
:
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node'
static_configs:
- targets: ['localhost:9100']
- scrape_interval: Defines the frequency at which Prometheus scrapes the target servers (set to 15 seconds in this example).
- targets: The servers from which Prometheus will collect data. Here,
localhost:9090
refers to the Prometheus server itself, andlocalhost:9100
refers to the node_exporter for system metrics.
Step 2: Create Prometheus Systemd Service File
To run Prometheus as a service, create a systemd service file. First, create a file named /etc/systemd/system/prometheus.service
:
sudo nano /etc/systemd/system/prometheus.service
Add the following content to the file:
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/docs/introduction/overview/
After=network.target
[Service]
User=prometheus
Group=prometheus
ExecStart=/usr/local/bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus/data
Restart=always
[Install]
WantedBy=multi-user.target
This configuration tells systemd to run Prometheus under the prometheus
user and automatically restart it if it stops.
Step 3: Reload systemd and Start Prometheus
Reload the systemd daemon and start the Prometheus service:
sudo systemctl daemon-reload
sudo systemctl start prometheus
To enable Prometheus to start on boot:
sudo systemctl enable prometheus
5. Starting Prometheus and Verifying the Installation
Step 1: Verify Prometheus Status
Check the status of Prometheus to ensure it is running properly:
sudo systemctl status prometheus
You should see output indicating that Prometheus is active and running.
Step 2: Access Prometheus Web Interface
Prometheus provides a web interface for querying metrics and monitoring the system. Open a web browser and navigate to:
http://<your-server-ip>:9090
You should see the Prometheus web UI, where you can query time-series data, view graphs, and configure alerting rules.
Step 3: Verify Metrics Collection
Prometheus is configured to scrape local machine metrics using the node_exporter
. To verify that Prometheus is collecting data, visit the Targets
page:
http://<your-server-ip>:9090/targets
This page will show you the status of all configured targets. If localhost:9100
(node_exporter) is listed and last scrape
is showing recent data, then Prometheus is correctly scraping metrics.
6. Conclusion
In this tutorial, you have successfully installed and configured Prometheus on your AnonVM server. You learned how to configure it to scrape local machine metrics and how to set it up as a system service. With Prometheus, you can now monitor your servers, gather metrics, and set up alerting for system and application health.
Prometheus is a powerful tool that can scale with your infrastructure, making it ideal for monitoring systems, applications, and even containers. If you want to extend your setup, consider integrating Prometheus with Grafana for better data visualization or adding exporters to monitor additional services.