This guide explains how to install and configure Prometheus on an Ubuntu system for real-time monitoring and alerting. It includes detailed installation steps, configuration instructions, and a practical use case to monitor system metrics like CPU and memory usage, providing comprehensive visibility into server performance.
7 min
Edited:13-10-2024
Prometheus is an open-source monitoring and alerting toolkit designed to provide real-time metrics and time-series data. Widely used for infrastructure and application monitoring, Prometheus offers a flexible querying language and integrates well with visualization tools like Grafana. This guide walks you through the process of installing Prometheus on Ubuntu, along with a practical use case for monitoring server performance.
As a best practice, begin by updating your system packages:
1. sudo apt update && sudo apt upgrade -y
It's a good idea to run Prometheus under a non-root user for security purposes. Create a user specifically for Prometheus:
1. sudo useradd --no-create-home --shell /bin/false prometheus
Visit the Prometheus download page and find the latest version. At the time of writing, you can download and extract the latest Prometheus version with the following commands:
1. cd /tmp
2. wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz
3. tar -xvf prometheus-2.45.0.linux-amd64.tar.gz
Move the necessary files to appropriate directories for better management:
1. sudo mv prometheus-2.45.0.linux-amd64/prometheus /usr/local/bin/
2. sudo mv prometheus-2.45.0.linux-amd64/promtool /usr/local/bin/
sudo mv prometheus-2.45.0.linux-amd64/prometheus /usr/local/bin/ sudo mv prometheus-2.45.0.linux-amd64/promtool /usr/local/bin/
1. sudo mv prometheus-2.45.0.linux-amd64/consoles /etc/prometheus
2. sudo mv prometheus-2.45.0.linux-amd64/console_libraries /etc/prometheus
Create a configuration file for Prometheus. First, create the required directories:
1. sudo mkdir /etc/prometheus
2. sudo mkdir /var/lib/prometheus
sudo mkdir /etc/prometheus sudo mkdir /var/lib/prometheus
1. sudo nano /etc/prometheus/prometheus.yml
Paste the following basic configuration into the file:
1. global:
2. scrape_interval: 15s
3.
4. scrape_configs:
5. - job_name: 'prometheus'
6. static_configs:
7. - targets: ['localhost:9090']
This tells Prometheus to scrape its own metrics at an interval of 15 seconds.
To make sure Prometheus starts at boot and runs as a service, create a systemd service file:
1. sudo nano /etc/systemd/system/prometheus.service
sudo nano /etc/systemd/system/prometheus.service
1. [Unit]
2. Description=Prometheus
3. Wants=network-online.target
4. After=network-online.target
5.
6. [Service]
7. User=prometheus
8. Group=prometheus
9. Type=simple
10. 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
11.
12. [Install]
13. WantedBy=multi-user.target
Set appropriate ownership for the Prometheus directories:
1. sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus
Reload systemd to apply the new service:
1. sudo systemctl daemon-reload
2. sudo systemctl start prometheus
3. sudo systemctl enable prometheus
You can verify that Prometheus is running by checking its status:
1. sudo systemctl status prometheus
Prometheus will now be accessible at http://localhost:9090.
If you're running Prometheus on a server with a firewall enabled, open port 9090:
1. sudo ufw allow 9090/tcp
Once Prometheus is running, open a web browser and navigate to http://<server-ip>:9090. You will be greeted by the Prometheus dashboard.
To monitor system metrics like CPU and memory usage, install the Node Exporter, which is a Prometheus exporter designed for hardware and OS metrics:
1. cd /tmp
2. wget https://github.com/prometheus/node_exporter/releases/download/v1.6.0/node_exporter-1.6.0.linux-amd64.tar.gz
3. tar -xvf node_exporter-1.6.0.linux-amd64.tar.gz
4. sudo mv node_exporter-1.6.0.linux-amd64/node_exporter /usr/local/bin/
Create a systemd service file for Node Exporter:
1. sudo nano /etc/systemd/system/node_exporter.service
Add the following:
1. [Unit]
2. Description=Node Exporter
3. Wants=network-online.target
4. After=network-online.target
5.
6. [Service]
7. User=prometheus
8. Group=prometheus
9. Type=simple
10. ExecStart=/usr/local/bin/node_exporter
11.
12. [Install]
13. WantedBy=multi-user.target
Reload systemd and start Node Exporter:
1. sudo systemctl daemon-reload
2. sudo systemctl start node_exporter
3. sudo systemctl enable node_exporter
Edit your Prometheus configuration file to add Node Exporter as a target:
1. sudo nano /etc/prometheus/prometheus.yml
Add the following under scrape_configs:
1. - job_name: 'node_exporter'
2. static_configs:
3. - targets: ['localhost:9100']
You can now access system metrics from Node Exporter by navigating to http://<server-ip>:9090/targets. Prometheus should be scraping metrics from localhost:9100. To query specific metrics (e.g., CPU usage), go to http://<server-ip>:9090/graph and enter a query like:
1. node_cpu_seconds_total
This will show you CPU usage data over time.
Prometheus is an excellent tool for real-time monitoring and alerting. By following this guide, you've installed Prometheus and Node Exporter on Ubuntu, configured it to monitor system metrics, and learned how to query and visualize these metrics through its web interface.
14-10-2024
This article offers a detailed guide on installing and configuring IPTables on an Ubuntu VPS. IPTables is a powerful firewall tool that helps secure your server by controlling inbound and outbound traffic. Learn how to set up rules for traffic filtering, configure basic security policies, and apply custom rules to protect your VPS.
IPtables
security
12 min
This article offers a comprehensive guide on installing and configuring ModSecurity, a powerful web application firewall (WAF), on an Ubuntu VPS. Learn how to secure your server by filtering and monitoring HTTP requests, set up ModSecurity with Nginx or Apache, and apply rules to protect against common web attacks.
Modsecurity
security
10 min
14-10-2024
This article provides a comprehensive guide on installing and configuring PHP-FPM (FastCGI Process Manager) on an Ubuntu VPS. Learn how to optimize PHP performance for your web applications by configuring PHP-FPM with Nginx or Apache, managing pools, and fine-tuning settings for efficient processing of PHP scripts.
PHP-FPM
speed
optimise
12 min