How to Install Prometheus on Ubuntu 20.04 LTS

In this article we are going to cover How to Install Prometheus on Ubuntu 20.04 LTS.

What is Prometheus ?

  • Prometheus is a open source Linux Server Monitoring tool mainly used for metrics monitoring, event monitoring, alert management, etc.
  • Prometheus has changed the way of monitoring systems and that is why it has become the Top-Level project of Cloud Native Computing Foundation (CNCF).
  • Prometheus uses a powerful query language i.e. “PromQL”.
  • In Prometheus tabs are on and handles hundreds of services and microservices.
  • Prometheus use multiple modes used for graphing and dashboarding support.

Prometheus Architecture

How to Install Prometheus on Ubuntu 20.04 LTS 1
  • As above we can see an architecture of Prometheus monitoring tool.
  • We made a basic design to understand it easily for you people.

Now lets understand the Prometheus components one-by-one

Prometheus Components

1. Prometheus Server

  • Prometheus server is a first component of Prometheus architecture.
  • Prometheus server is a core of Prometheus architecture which is divided into several parts like Storage, PromQL, HTTP server, etc.
  • In Prometheus server data is scraped from the target nodes and then stored int the database.

1.a. Storage

  • Storage in Prometheus server has a local on disk storge.
  • Prometheus has many interfaces that allow integrating with remote storage systems.

1.b. PromQL

  • Prometheus uses its own query language i.e. PromQL which is very powerful querying language.
  • PromQL allows the user to select and aggregate the data.

2. Service Discovery

  • Next and very important component of Prometheus Server is the Service Discovery.
  • With the help of Service discovery the services are identified which are need to scraped.
  • To Pull metrics, identification of services and finding the targets are compulsory needed.
  • Through Service discovery we monitor the entities and can also locate its targets.

3. Scrape Target

  • Once the services are identified and the targets are ready then we can pull metrics from it and can scrape the target.
  • We can export the data of end point using node exporters.
  • Once the metrics or other data is pulled, Prometheus stores it in a local storage.

4. Alert Manager

  • Alert Manager handles the alerts which may occurs during the session.
  • Alert manager handles all the alerts which are sent by Prometheus server.
  • Alert manager is one of the very useful component of Prometheus tool.
  • If in case any big error or any issue occurs, alert manager manage those alerts and contact with human via E-mail, Text Messages, On-call, or any other chat application service.

5. User Interface

  • User interface is also a important component as it builds a bridge between the user and the system.
  • In Prometheus, user interface are note that much user friendly and can be used till graph queries.
  • For good exclusive dashboards Prometheus works together with Grafana (visualization tool).
  • Using Grafana over Prometheus to visualize properly we can use custom dashboards.
  • Grafana dashboards displays via pie charts, line charts, tables, good data graphs of CPU usage, RAM utilization, network load, etc with indicators.
  • Grafana supports and run with Prometheus by querying language i.e. PromQL.
  • To fetch data from Prometheus and to display the results on Grafana dashboards PromQL is used.

Prerequisite

  • Ubuntu with 20.04 Version
  • Root user account with sudo  privilege.
  • Prometheus system user and group.
  • Sufficient storage on your system and good internet connectivity.

We will update the system repository index by using the following command.

sudo apt update -y

switch to root user

sudo su -

Export the release of Prometheus

export RELEASE="2.2.1"

#1. Creating Prometheus System Users and Directory

We will have to create a Prometheus user named Prometheus and a Prometheus directory named as Prometheus.

Using below commands we can create a user and directory.

sudo useradd --no-create-home --shell /bin/false Prometheus  
sudo useradd --no-create-home --shell /bin/false node_exporter
sudo mkdir /etc/prometheus
 sudo mkdir /var/lib/prometheus

#2. Update Prometheus user

As user groups and directories are created successfully which store the Prometheus data and files.

Now we will have to update the group and user ownership on the newly created directories.

By using the below command we update the ownership.

sudo chown prometheus:prometheus /etc/prometheus
sudo chown prometheus:prometheus /var/lib/Prometheus

#3. Download Prometheus Binary File

Now we will download the latest version of Prometheus. We can copy the download link as per our Operating System from Prometheus download page

Using below command we can download Prometheus, here we are downloading Prometheus 2.26 version, you use above link to download specific version.

Navigate to /opt directory

 cd /opt/

Download the Prometheus setup using wget

wget https://github.com/prometheus/prometheus/releases/download/v2.26.0/prometheus-2.26.0.linux-amd64.tar.gz

Now we have successfully downloaded the Prometheus file and now we will extract that file.

#4. Install Prometheus on Ubuntu 20.04 LTS

We can use sha256sum command line to generate a checksum of the Prometheus downloaded file.

We will also extract the downloaded file using the tar command.

sha256sum prometheus-2.26.0.linux-amd64.tar.gz

Output:

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

We have verify that the output from above command with checksum matches the sha256sum checksum which is on official Prometheus download page.

It will ensure that our downloaded file is not a corrupted file.

Now we will extract the Prometheus setup file using the following commands.

tar -xvf prometheus-2.26.0.linux-amd64.tar.gz

navigate to prometheus extracted folder

cd prometheus-2.26.0.linux-amd64

To check list of setup files

ls

#5. Copy Prometheus Binary files

Now we have two libraries in our directory i.e. Prometheus and promtool. We will have to copy that both libraries to our /usr/local/bin directory.

By following below commands we will perform the copy operation.

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

#6. Update Prometheus user ownership on Binaries

Now we will update the user and group ownership on the binaries of Prometheus.

Using following commands we will update the user and group ownership.

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

#7. Copy Prometheus Console Libraries

We will need to copy the console and console_libraries directories to /etc/Prometheus/.

Use below commands to copy console and console_libraries.

sudo cp -r /opt/prometheus-2.26.0.linux-amd64/consoles /etc/prometheus
sudo cp -r /opt/prometheus-2.26.0.linux-amd64/console_libraries /etc/prometheus
sudo cp -r /opt/prometheus-2.26.0.linux-amd64/prometheus.yml /etc/prometheus

#8. Update Prometheus ownership on Directories

Now we will update the user and group ownership on the directories  to Prometheus user using -R.

By executing this commands ownership is set on is ensured. Execute the following commands.

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

#9. Check Prometheus Version

Now the Prometheus is successfully installed on our system. We will check the version of Prometheus and promtool to configure it.

Follow the commands to verify prometheus version.

prometheus --version
promtool --version

Output:

How to Install Prometheus on Ubuntu 20.04 LTS 2

#10. Prometheus configuration file

We have already copied /opt/prometheus-2.26.0.linux-amd64/prometheus.yml file /etc/prometheus directory, verify if it present and should look like below and modify it as per your requirement.

cat /etc/prometheus/prometheus.yml
# 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']

#11. Creating Prometheus Systemd file

To run Prometheus as service we have to setting up prometheus, We will provide a path for both configuration file and data directory. We will start it with the Prometheus user using the following command.

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

Now we will create a system service file in /etc/systemd/system location.

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

After creating file successfully, copy the below files and it to the newly created file.

/etc/systemd/system/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

After adding the program save the file with Ctrl+O and exit with Ctrl+X.

To use the newly created service we will have to reload the daemon services, Use the below command to reload daemon services.

sudo systemctl daemon-reload

start and enable prometheus service using below commands

sudo systemctl start prometheus
sudo systemctl enable prometheus

We will check the Prometheus status weather it is running or not

sudo systemctl status prometheus

Output :-

How to Install Prometheus on Ubuntu 20.04 LTS 3

Prometheus installation and configuration is set up, We can see status Active:  active(running)

#12. Accessing Prometheus

Now as Prometheus installation and configuration is set up and it is ready to use we can access  its services via web interface.Also check weather port 9090 is UP in firewall.

Use below command to enable prometheus service in firewall

 sudo ufw allow 9090/tcp

Now Prometheus service is ready to run and we can access it from any web browser.

http://server-IP-or-Hostname:9090.
How to Install Prometheus on Ubuntu 20.04 LTS 4

As we can see the Prometheus dashboards, we can also check the target.As we can observe Current state is UP and we can also see the last scrape.

How to Install Prometheus on Ubuntu 20.04 LTS 5

Here are some snapshots of Prometheus dashboards and graphs.

How to Install Prometheus on Ubuntu 20.04 LTS 6
How to Install Prometheus on Ubuntu 20.04 LTS 7

Conclusion:

We have covered How to Install Prometheus on Ubuntu 20.04 LTS

Related Articles:

Akash Padwal

I am Akash Padwal working as a DevOps Engineer Intern in Product Company. Likes to explore new DevOps tools and share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link