How to Install and Configure Prometheus as a Systemd Service — Part 1

Meher Askri
5 min readAug 2, 2024

--

Hello folks and Welcome again ,

Today , we’re going to discuss one of the famous monitoring tools that has gained widespread recognition in the DevOps world. Can you guess which one ?

Yes , it’s Prometheus ( not the Titan god of Forethought from God Of War game 😂 ) .

So , what is Prometheus and why is it so popular ?

Prometheus is an open-source monitoring and alerting tool initially designed for monitoring highly dynamic containerized environments at the first place ( While its true, the primary focus is cloud native environment, however it’s also versatile enough to monitor legacy applications running on virtual machines (VMs) in an on-premises data center, which we’ll explore later) .

Like Nagios and Zabbix, Prometheus also follows a pull-based model for data collection, where it retrieve metrics from applications and services at regular intervals .

So , what’s the twist ?

Well, in simple terms , Prometheus is service-based monitoring tool means it was designed to monitor and manage services and applications . On the other hand, tools like Zabbix and Nagios are traditionally machine-based focusing on monitoring individual servers and devices in relatively static environments .

I hope you get the idea of why Prometheus is special compared to other tools.

Now, I’m not going to show you how to deploy Prometheus as a pod in Kubernetes using Helm ( K8s package manager) otherwise, this article will be over in the next line 😄 . Instead will focus on installing it on Linux for legacy applications ( After all, who will deliver the voice of this forgotten group 😁 ) .

Alright, let’s jump into the terminal:

The first step is to download the Prometheus package from the GitHub page ( this is the most recent release , note that by the time you check this article, the release may change) .

Let’s download the Prometheus package using the wget tool :

And let’s extract it ( You can delete the tarball after the extraction if you want ) :

Next , let’s create two directories :

  • The “/etc/prometheus” : this directory will store the Prometheus configuration files.
  • The/var/lib/prometheus" : This directory will hold Prometheus data .

The location actually is not that important , but we’ll stick to the standards, “/etc/” for configuration files and “/var/lib” for storing variable data .

Next, let’s move the prometheus and promtool binary files to the /usr/local/bin/ directory ( this will makes Prometheus accessible to all users ) .

Next , let’s move the prometheus.yml YAML configuration file and the consoles and console_libraries directories to the /etc/prometheus . The consoles and console_libraries directories contain the resources necessary to create customized consoles ( JavaScript libraries and console templates , this is a more advanced feature and beyond the scope of this article ) .

Congratulations, Prometheus is now successfully installed ( you can remove all the reset if you want too ) .

Now, while Prometheus can be started and stopped from the command line, it’s not the most elegant way. I prefer to run it as a service using the systemctl utility. I don’t know about you, but for me, this is more convenient .

To allow Prometheus to run as a service, let’s create a prometheus.service file :

Here’s a breakdown of the sections and options in this service file:

  • [Unit]: This section provides metadata about the service, a description of the service and a definition of the dependencies (Wants and After network-online.target means that the service is started only after network connectivity is established) .
  • [Service]: This section defines how the service should be executed. we specify the user and group under which the service will run ( we’ll create them next) , the executable to run (ExecStart): In our case, we specify the path to the Prometheus binary (/usr/local/bin/prometheus) , the configuration file ( /etc/prometheus/prometheus.yaml), the storage path ( /var/lib/prometheus) , listen address ( all interfaces and port 9090) , log level ( info ) , the path to template and libraries directories and we also specify that the service should be restarted if it encounters a failure .
  • [Install]: This section defines the target units that this service should be enabled for when the system boots, in our case into multi-user mode (WantedBy=multi-user.target) this is equivalent to the run-level 3 in the old SysVinit system .

Next let’s move the file under the /etc/systemd/system/ and create the user and group Prometheus :

Finally, let’s assign the correct ownership and start the service ( And of course, without forgetting to restore the selinux context, otherwise the service will fail to start ) :

Now, that’s looks better.

Remember that Prometheus listens on port 9090/tcp. so , let’s open the port on the firewall and acces the Access the Prometheus web interface and dashboard externally .

Et Voila :

As you can see Prometheus doesn’t display much information yet and that’s because it’s using the default config file .

And that’s it for today. Next time, I will show you how to install the Node Exporter on client VMs and how to visualize the data with Grafana . We’ll see all of that in action in the next article .

Thank you for taking the time to read my article , Don’t forget to like and share and I’ll see you in the next one .

--

--

Meher Askri
Meher Askri

Written by Meher Askri

Linux System Engineer || RHCA

No responses yet