How to install & Configure Prometheus on Ubuntu 20.04 LTS

What is Prometheus?

Prometheus is an opensource monitoring and alerting tool for recording real-time metrics in time-series database. It was developed at SoundCloud starting in 2012 but officially announced in January 2015. Prometheus collects metrics using HTTP pulls, allowing for higher performance and scalability. The application is written in Go language and licensed under the Apache 2 License. Here we will discuss how to install & configure Prometheus  on Ubuntu 20.04 LTS.

Prometheus Features

The Prometheus has various awesome features. Here are the main features of Prometheus :

  • A multi-dimensional data model with time series data enabled identified by metric names 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 protocol
  • 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 are several steps required in Prometheus installation.

Prerequisite

  • Ubuntu 20.04 LTS Focal Fossa system
  • Root user or user account with sudo privilege.
  • Sufficient space and good internet connection.

Creating system Users and Directory

Prometheus requires system user and group. We have to create the user and group called as prometheus and also the directory called as prometheus. You can use below commands to accomplish this.

$  sudo useradd --no-create-home --shell /bin/false prometheus

$  sudo useradd --no-create-home --shell /bin/false node_exporter

Before we proceed Prometheus installation, we need to create directories for storing Prometheus files and data. We’ll create a directory in /etc for Prometheus configuration files and a directory in /var/lib for its data.

$  sudo mkdir /etc/prometheus

$  sudo mkdir /var/lib/prometheus

Next, update the user and group ownership on the new directories to the prometheus user.

$  sudo chown prometheus:prometheus /etc/prometheus

$  sudo chown prometheus:prometheus /var/lib/prometheus

Download and Extract Prometheus Binary

Now download the latest Prometheus binary file. The file is located in the github ‘s Prometheus or you can download it from Prometheus official page. After the files was downloaded, then you have to extract it as follows:

$  cd /opt/
$  wget https://github.com/prometheus/prometheus/releases/download/v2.21.0/prometheus-2.21.0.linux-amd64.tar.gz

Now, You can use the sha256sum command line utility to generate a checksum of the downloaded file:

 $  sha256sum prometheus-2.21.0.linux-amd64.tar.gz

f1f2eeabbf7822572dce67565dc96ffaa2dd1897dd1d844562552b11123f151a prometheus-2.21.0.linux-amd64.tar.gz

prometheus on ubuntu 20.04

Make sure the output from this command with the checksum should match SHA256 Checksum on the Prometheus offical download page. It will ensure us that our file is genuine and not corrupted.  If the checksums doesn’t match, remove the downloaded file and re-download the file.

Next, unpack the downloaded archive.

$  tar -xvf prometheus-2.21.0.linux-amd64.tar.gz

$  cd prometheus-2.21.0.linux-amd64

$  ls 

console_libraries consoles LICENSE NOTICE prometheus prometheus.yml promtool

You will get a directory called prometheus-2.21.0.linux-amd64 containing two binary files (prometheus and promtool), one prometheus.yml, two consoles and console_libraries directories containing the web interface files, a license, a notice, and several example files.

Copy the two binaries to the /usr/local/bin directory.

$  sudo cp /opt/prometheus-2.21.0.linux-amd64/prometheus /usr/local/bin/

$  sudo cp /opt/prometheus-2.21.0.linux-amd64/promtool /usr/local/bin/

Update the user and group ownership on the binaries to the prometheus user.

$  sudo chown prometheus:prometheus /usr/local/bin/prometheus

$  sudo chown prometheus:prometheus /usr/local/bin/promtool

After this copy the consoles and console_libraries directories to /etc/prometheus.

$  sudo cp -r /opt/prometheus-2.21.0.linux-amd64/consoles /etc/prometheus

$  sudo cp -r /opt/prometheus-2.21.0.linux-amd64/console_libraries /etc/prometheus

$  sudo cp -r /opt/prometheus-2.21.0.linux-amd64/prometheus.yml /etc/prometheus

Update the user and group ownership on the directories to the prometheus user using the -R recursive flag. This will ensure that ownership is set on the files inside the directory as well.

$  sudo chown -R prometheus:prometheus /etc/prometheus/consoles

$  sudo chown -R prometheus:prometheus /etc/prometheus/console_libraries

$  sudo chown -R prometheus:prometheus /etc/prometheus/prometheus.yml

Optionally, remove the files from /opt/ directory as they are no longer needed. This process will free some space on your machine.

$  rm -rf prometheus-2.21.0.linux-amd64 prometheus-2.21.0.linux-amd64.tar.gz

Finally Prometheus is installed on the system, now we’ll create its configuration and service files. You can verify the prometheus and promtool version with prometheus –version and promtool –version commands in shell.

$  prometheus --version

$  promtool --version

prometheus on ubuntu 20.04

Creating Prometheus Configuration file

In the /etc/prometheus directory, use your favorite text editor to create a configuration file named prometheus.yml. We have copied sample file here.

$ cat /opt/prometheus-2.21.0.linux-amd64/prometheus.yml

The contents 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, in 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 tcp port 9090. You can edit the file as per requirement parameters that suitable with your need.

Note : Prometheus configuration file uses the YAML format, which requires two spaces for indentation rather that tabs. Prometheus fails to start if the configuration file is incorrectly formatted.

Setting Prometheus systemd Service file

You can start up Prometheus as the prometheus user, by providing the path to both the configuration file and the data directory in the command line.

$  sudo -u prometheus /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

The command result contains information about Prometheus loading progress, configuration file, and related services. It confirms that Prometheus is listening on port 9090.

prometheus on ubuntu 20.04

If you get any error message, double-check that you’ve used YAML syntax in your configuration file and permission on each file in /etc/prometheus and /var/lib/prometheus directory.

You have to halt Prometheus by pressing CTRL+C. We will create systemd service file in /etc/systemd/system/ location.

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

The service file asks systemd to run Prometheus as the prometheus user, with the configuration file located in the /etc/prometheus/prometheus.yml directory and to store its data in the /var/lib/prometheus directory. Now Copy the below content into the prometheus.service.

[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
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

[Install]
WantedBy=multi-user.target

Finally, save the file and exit text editor. To use the newly created service, reload systemd.

$  sudo systemctl daemon-reload

You can now start and enable Prometheus using the following command.

$  sudo systemctl start prometheus

$  sudo systemctl enable prometheus

You can check Prometheus service’s status to make sure it is running by using below command.

$  sudo systemctl status prometheus

The following status shows that Prometheus is running. Finally you have configured it on your system successfully.

prometheus on ubuntu 20.04

Accessing Prometheus

You can access the Prometheus service via web interface. Make sure it’s port 9090 is opened in the firewall.

$  sudo ufw allow 9090/tcp

The Prometheus service has been ready to receive web requests. You can access it via your favourite web browser using the Ip address, http://server-IP-or-Hostname:9090.

prometheus on ubuntu 20.04

prometheus on ubuntu 20.04

 

prometheus running on ubuntu 20.04

Conclusion

So far you have learnt how to install Prometheus on Ubuntu 20.04 LTS. We will show you how to install node_exporter and grafana in our upcoming tutorials. Stay tuned.

Read Also : How to Install Zabbix Server 5.0 on Ubuntu 20.04

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

Leave a Comment