Setup Prometheus for Monitoring on Ubuntu

Prometheus_Cover


What is Prometheus?

Prometheus is one of the most powerful and most commonly used open-source monitoring and alerting tools. Originally built at SoundCloud in 2012, now this has been adopted by many organizations and monitoring specialists. It is a time-series database that collects data (metrics) from various exporters for different purposes and datatype. It uses PromQL as its query language to extract data which could be further used to create graphical visualizations using tools like Grafana.

Today's Agenda

In this post, we will learn how to install Prometheus databases on an Ubuntu system. We will run Prometheus as a service and enable it to run on every boot (system startup) to make it production-ready.

Prerequisite

This post has been prepared for the audience who :
  1. Have access to a Linux-based system although the steps are almost similar in other platforms like CentOS or MacOS.
  2. Have a very basic understanding of Linux based systems and their commands.
  3. And finally, are eager to learn and try such powerful tools.

Let's get started

Step 1: Download and Install Prometheus.

Firstly, we will configure a Linux user dedicated to Prometheus:
 

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

 
Then we shall create directories for Prometheus configuration and library files.
 

$ sudo mkdir /etc/prometheus
$ sudo mkdir /var/lib/prometheus

 
Give the ownership of these directories to the user "prometheus".
 

$ sudo chown -R prometheus:prometheus /etc/prometheus
$ sudo chown -R prometheus:prometheus /var/lib/prometheus


This command will update your system.
 
$ sudo apt update

Now, go to the official downloads page https://prometheus.io/download/ and get the link of latest stable version available. Copy the link and download it.
 

$ wget https://github.com/prometheus/prometheus/releases/download/

v2.19.0rc.0/prometheus-2.19.0-rc.0.linux-amd64.tar.gz


$ tar -xvzf prometheus-2.19.0-rc.0.linux-amd64.tar.gz


This will create one directory with the name prometheus-2.19.0-rc.0.linux-amd64

Copy these two binary files to the location /usr/local/bin/ to run these binaries.
 

$ sudo cp prometheus-2.19.0-rc.0.linux-amd64/prometheus /usr/local/bin/
$ sudo cp prometheus-2.19.0-rc.0.linux-amd64/promtool /usr/local/bin/


Provide the ownership of these files to Prometheus users.
 

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


Copy required directories configuration file to /etc/prometheus.
 

$ sudo cp -r prometheus-2.19.0-rc.0.linux-amd64/consoles /etc/prometheus/
$ sudo cp -r prometheus-2.19.0-rc.0.linux-amd64/console_libraries /etc/prometheus/
$ sudo cp prometheus-2.19.0-rc.0.linux-amd64/prometheus.yml /etc/prometheus/


Provide the ownership of these files as well to Prometheus users.
 

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


Now we can remove all those files that we have already copied to their destination.
 

$ rm -rf prometheus-2.19.0-rc.0.linux-amd64*



Step 2: Configure Prometheus to our requirements.

Note: Prometheus has its configuration file in .yml format and requires an indent of 2 spaces.
Its configuration file is present at /etc/prometheus/. Open this file and make the necessary changes.
 

$ sudo vi /etc/prometheus/prometheus.yml


Output


default_prometheus_yml
Default_prometheus.yml

We will look into the configuration in detail in our post Install Node Exporter and Run as Service.


Step 3: Run Prometheus as a Service.

Now, create a systemd service file for Prometheus.
 

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


Copy-paste this configuration to the above file created and save it.

 

[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

 


After creating a new systemd file, we need to reload systemd daemon to read this service file.

$ sudo systemctl daemon-reload


Finally, we will start the Prometheus service, check its status if it is running.
 

$ sudo systemctl start prometheus
$ sudo systemctl status prometheus

 

Output


Service Status
Service Status

 Enable it to run Prometheus on every system startup (boot).
 

$ sudo systemctl enable prometheus



Step 4: Start using Prometheus.

Run this command to check Prometheus is running on which port.

$ sudo netstat -ntplu


Prometheus on port 9090
Prometheus on port 9090


Open your browser and open port 9090 of your system and you will see Prometheus running on your system.
 
Browser View of Prometheus
Browser view of Prometheus



 
Continue your reading with us to learn how to install various exporters of Prometheus to scrape metrics and syntax for PromQL to extract information from metrics.







Comments