×


Install Prometheus on Ubuntu 20.04 LTS - Step by step process ?

Prometheus is an excellent open-source monitoring system that allows us to collect metrics from our applications and stores them in a database, especially a time-series-based DB. The biggest advantage of Prometheus is the query language it provides for data processing. It has a web interface that allows for easy monitoring from any device with an internet connection and also supports alerting via email and SMS messages in case the metrics surpass a configured threshold.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Software Installation queries.

In this context, we shall look into how to install Prometheus on Ubuntu 20.04 LTS.


Steps to Install Prometheus on Ubuntu 20.04 LTS Focal Fossa ?

1. Perform system update

First, make sure that all your system packages are up-to-date by running the following apt commands in the terminal:

$ sudo apt update
$ sudo apt upgrade


2. Install Nginx web server

Nginx is available in the default Ubuntu repositories. To install it run the following command:

$ sudo apt install nginx

Once the installation is completed, run the commands to enable Nginx to automatically startup when your server starts:

$ sudo systemctl stop nginx.service
$ sudo systemctl start nginx.service
$ sudo systemctl enable nginx.service


3. Create Prometheus Users

We have to create the user and group called prometheus and also the directory called Prometheus:

$ sudo useradd -s /sbin/nologin --system -g prometheus prometheus
sudo mkdir /var/lib/prometheus
for i in rules rules.d files_sd; do sudo mkdir -p /etc/prometheus/${i}; done


4. Install Prometheus on the system

Download the latest stable release of Prometheus using the wget command:

$ mkdir -p /tmp/prometheus && cd /tmp/prometheus
curl -s https://api.github.com/repos/prometheus/prometheus/releases/latest | grep browser_download_url | grep linux-amd64 | cut -d '"' -f 4 | wget -qi -
tar xvf prometheus*.tar.gz

Once file extraction was completed, then move the prometheus and promtool binaries under the extracted Prometheus archive folder to/usr/local/bindirectory:

$ sudo mv prometheus promtool /usr/local/bin/


5. Configure Prometheus

We will create the configuration file named prometheus.yml in the /etc/prometheus directory:

$ sudo mv prometheus.yml /etc/prometheus/prometheus.yml
$ sudo mv consoles/ console_libraries/ /etc/prometheus/
$ sudo nano /etc/prometheus/prometheus.yml

The content of prometheus.yml is as follow:

my global config

global:

scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
Alertmanager configuration
alerting:
alertmanagers:
static_configs: targets:
# - alertmanager:9093

Load rules once and periodically evaluate them according to the global 'evaluation_interval'.

rule_files:

# - "first_rules.yml"
# - "second_rules.yml"

A scrape configuration containing exactly one endpoint to scrape:

Here it's Prometheus itself.

scrape_configs:
# The job name is added as a label job= to any timeseries scraped from this config.
job_name: 'prometheus'
metrics_path defaults to '/metrics'
scheme defaults to 'http'.
static_configs:
targets: ['localhost:9090']


6. Create a Prometheus Systemd Service

Now, we will create a file for the systemd service:

$ sudo nano /etc/systemd/system/prometheus.service

Add the following file:

[Unit]
Description=Prometheus
Documentation=https://prometheus.io/docs/introduction/overview/
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
User=prometheus
Group=prometheus
ExecReload=/bin/kill -HUP \$MAINPID
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 \
  --web.listen-address=0.0.0.0:9090 \
  --web.external-url=
SyslogIdentifier=prometheus
Restart=always
[Install]
WantedBy=multi-user.target

Then, change the ownership of these directories to Prometheus user and group:

for i in rules rules.d files_sd; do sudo chown -R prometheus:prometheus /etc/prometheus/${i}; done
for i in rules rules.d files_sd; do sudo chmod -R 775 /etc/prometheus/${i}; done
sudo chown -R prometheus:prometheus /var/lib/prometheus/

Once you are done with the above configurations, reload systemd using the following command:

$ sudo systemctl daemon-relaod
$ sudo systemctl start prometheus
$ sudo systemctl enable prometheus


7. Configure Firewall

Meanwhile, you need to make sure that your firewall is configured to allow traffic on HTTP (80), HTTPS (443) and 9090 ports. Nginx registers itself as a service with ufw :

$ sudo ufw allow in "Nginx Full"
$ sudo ufw allow 9090/tcp


8. Access Prometheus Web Interface

Prometheus will be available on HTTP port 9090 by default. 

Open your favorite browser and navigate to http://your-domain.com:9090 or http://server-ip-addreess:9090 and complete the required steps to finish the installation.


[Need assistance in installing any Software on your Linux system ? We can help you. ]


Conclusion

This article covers the process of installing Prometheus open-source monitoring Software on your Ubuntu 20.04 LTS (Focal Fossa). In fact, Prometheus uses visualization tools like Grafana, which uses PromQL, a flexible query language to fetch data for monitoring and analysis.

To learn more about Prometheus, please see the official documentation.