Skip to main content

Command Palette

Search for a command to run...

Prometheus Installation SystemD

Published
3 min readView as Markdown
Prometheus Installation SystemD
K

I am a developer and a DevOps engineer. I love to write about DevOps automation tools

Introduction

Prometheus monitoring tools were created by a team of SoundCloud, a music streaming service, in 2012. This team was looking for monitoring tools that were easy to use, highly scalable, and designed specifically for modern containerization environments. The team wanted a system that could store a large amount of time-series data, and provide flexible querying and alert capabilities.
The team found that the existing monitoring tools cannot do what they wanted, so they came together and built their own system to serve their main purpose and named it Prometheus. So since its initial release, Prometheus has become a popular open-source monitoring tool, and it is used by most companies and organizations worldwide.

Prometheus is an open-source monitoring tool that is commonly used by DevOps teams to collect metrics and scrap data from an application. Prometheus was designed to be highly scalable and work well in a cloud-native environment.
Prometheus collects metrics from different sources, including applications, servers, and other monitoring tools, and stores them in a time-series database. It allows the user to query and visualize the data collected using its built-in query language, PromQL. It also provides alert functionality that alerts users when a configured threshold is met.

Systemd Installation

Let's install Prometheus using systemd method so as to manage Prometheus as a system service. Systemd provides facilities for starting, stopping, restarting, and monitoring Prometheus processes. With systemd, Prometheus can automatically be started on system boot, ensuring it is always ready to collect and process metrics.

1. Create a new user on your Linux machine with no login access by typing or copying the below command.

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

2 Create the following directory on your Linux machine

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

3 Change the ownership of the above-created directory from root to Prometheus user by typing or copying the below command

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

4 Click this link Download | Prometheus to download Prometheus binaries or copy the below command to download it binaries directly.

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

5 Untar or extract the binary you downloaded by copying and pasting the below command and cd into the folder

 tar xvf prometheus-2.37.0.linux-amd64.tar.gz && cd prometheus-2.37.7.linux-amd64

6 Copy Prometheus executable files with ownership permission to the bin directory, to allow you to have easy access and run the tools from any location in the command line without specifying the full path to the executable.

sudo cp prometheus /usr/local/bin/
sudo cp promtool /usr/local/bin/

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

7 Copy or move the consoles, console_libraries, and prometheus.yaml file to /etc/prometheus directory

sudo cp -r consoles /etc/prometheus/consoles
sudo cp -r console_libraries /etc/prometheus/console_libraries
sudo cp prometheus.yaml /etc/prometheus/prometheus.yml

Permissions
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

8 Create a prometheus.service in systemd directory to define the configuration for running Prometheus as a system service. This directory contains the configuration used to manage the Prometheus service. It specifies such as executable path, command-line arguments, user and group settings, and dependencies.

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

9 Copy and paste the below Prometheus service configuration.

[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

10 Run the below command to reload the daemon, enable and, start Prometheus as a service.

Reload the daemon
sudo systemctl daemon-reload

Enable Prometheus to start service on-boot
sudo systemctl enable prometheus 

Start prometheus service
sudo systemctl start prometheus

Conclusion

In conclusion, installing Prometheus with systemd provide a convenient and reliable way to manage Prometheus as a service. It simplifies the deployment, management, and monitoring of Prometheus, making it an effective and efficient monitoring tool.