How To install Prometheus on Ubuntu 20.04 LTS

How To Install Prometheus On Ubuntu 20.04 LTS

Introduction

Prometheus is an open-source monitoring and alerting tool software designed for recording real-time metrics in a time-series database. The Prometheus metrics are collected using HTTP pulls, allowing for higher performance and scalability. The application is written in Go and licensed under the Apache 2 License. Prometheus was developed at SoundCloud starting in 2012, the official public announcement was made in January 2015. On this article we will discuss how to install Prometheus application on Ubuntu 20.04 LTS.

Features

The Prometheus has several awesome features which makes users more convenient in using this application. As mentioned on the Prometheus official page, here is the Prometheus main features :

  • a multi-dimensional data model with time series data identified by metric name and key/value pairs
  • PromQL, a flexible query language to leverage this dimensionality
  • no reliance on distributed storage; single server nodes are autonomous
  • time series collection happens via a pull model over HTTP
  • pushing time series is supported via an intermediary gateway
  • targets are discovered via service discovery or static configuration
  • multiple modes of graphing and dashboarding support

Prometheus Installation On Ubuntu 20.04

There will be several steps that must be done during the Prometheus installation. However, there may be several different methods with what we are working here.

  1. Prerequisite
  2. Download and Extract Promotheus Binary Files
  3. Create Prometheus Configuration file
  4. Running Prometheus
  5. Accessing Prometheus from Web Interface

Prerequisite

  • Ubuntu 20.04 LTS Focal Fossa system with root or user account with sudo privilege.
  • Sufficient space and good internet connection.
  • Prometheus System User and Group. We have to create the user and group called as prometheus and also the directory called as prometheus. For this purpose we submit the command line below:
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
create prometheus user and group

Download and Extract Prometheus Binary Files

The next step is to download the latest Prometheus binary file. The files is located in the github ‘s Prometheus. The file will be temporary located on the /tmp/promethes directory (so we have to create it first). After the files was downloaded, then we have to extract it. The command line is as below :

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
prometheus download and extract binary files

We can verify the promethues and promtool version by querying its version, with prometheus --version and promtool --version command line.

prometheus --version and prmtool --version

After the file extraction was completed, then move the prometheus and promtool binaries under the extracted Prometheus archive folder to /usr/local/bin directory.

sudo mv prometheus promtool /usr/local/bin/

Create Prometheus Configuration file

The prometheus configuration file is call ed as prometheus.yml. And the sample Prometheus configuration file has been prepared and available on the extracted archive folder, just copy it to Prometheus configuration directory (/etc/prometheus).

sudo mv prometheus.yml /etc/prometheus/prometheus.yml
sudo mv consoles/ console_libraries/ /etc/prometheus/
sudo vi /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=<job_name> to any timeseries scraped from this config.
job_name: 'prometheus'
metrics_path defaults to '/metrics'
scheme defaults to 'http'.
static_configs:
targets: ['localhost:9090']

By default, at the configuration file there is only a single job, called prometheus , which scrapes the time series data exposed by the Prometheus server. The job contains a single, statically configured, target, the localhost on port 9090. We can edit the file to the requirement parameters that suitable wiht our need.

Setting Prometheus systemd Service unit file

For enbaling Prometheus service management by systemd, we need to explicitly define this unit file. For this purpose we create/edit the prometheus.service file which is located on the /etc/systemd/system directory.

sudo tee /etc/systemd/system/prometheus.service<<EOF

Change Directory Ownership And Reload Services

After setting the Prometheus systemd, then we have to change the directory ownership permission of Prometheus files. This is done by submitting command line below.

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/

The nex step is to reload the system daemon and check its status, these steps by submitting command lines as follow:

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

We could query the Prometheus daemon status by submitting command line below.

sudo systemctl status prometheus
Prometheus services

Accessing Prometheus

We can access the Prometheus service by accessing it via web inteface. To enable it, make sure if the port firewall for Prometheus is opened. As configure before, the Prometheus service is binding on port 9090. This could be done by submitting command line :

ufw allow 9090/tcp

The Prometheus application is now has been ready to receive web requests. We can access it via web browser using the address, http://server-IP-or-Hostname:9090.

Prometheus web interface
Prometheus web interface

Conculsion

So far I have shown you, the simple way to install Prometheus monitoring and alerting tools on Ubuntu 20.04 LTS, an the process was quite OK.

Share this article via :

Leave a Reply

Your email address will not be published. Required fields are marked *