How to Install Prometheus as a Service on Linux: A Step-by-Step Guide
Prometheus is an open-source monitoring and alerting system that is widely used in the DevOps community to collect and analyze metrics from various systems and applications. It was initially developed by SoundCloud in 2012 and was later donated to the Cloud Native Computing Foundation (CNCF) in 2016.
Prometheus is designed to be highly scalable and flexible, making it an ideal solution for monitoring large-scale distributed systems. It uses a pull-based model for collecting metrics, where clients periodically scrape metrics from endpoints exposed by the systems being monitored. The scraped metrics are then stored in a time-series database, where they can be queried and analyzed using the PromQL query language.
Prometheus has a wide range of use cases, including monitoring microservices architectures, containerized environments, and cloud-native systems. It can also be used for monitoring infrastructure components such as databases, message brokers, and web servers.
I’ll show you how to install Prometheus as a service on Linux using a systemd unit file. We’ll also create a dedicated user for the Prometheus service and configure the necessary permissions.
Step 1: Download Prometheus
First, head over to the official Prometheus website and download the latest version of the binary file for Linux. You can do this using the following command:
$ wget https://github.com/prometheus/prometheus/releases/download/v2.42.0/prometheus-2.42.0.linux-amd64.tar.gz
Replace the version number in the URL with the latest version available.
Step 2: Extract the Prometheus Archive
Once you have downloaded the Prometheus binary, extract the archive to a directory of your choice. For example, you can use the following command:
$ tar xvfz prometheus-*.tar.gz
This will create a new directory named prometheus-2.42.0.linux-amd64
, which contains the Prometheus binary and other files.
Step 3: Copy Binaries
Copy the Prometheus binary and other files to the /usr/local/bin
directory:
$ sudo cp prometheus-2.42.0.linux-amd64/prometheus /usr/local/bin/
$ sudo cp prometheus-2.42.0.linux-amd64/promtool /usr/local/bin/
Step 4: Configure Prometheus
Create a new configuration file for Prometheus in the /etc/prometheus
directory:
$ sudo nano /etc/prometheus/prometheus.yml
Add the following configuration to the file:
# 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"# 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).
This configuration sets the scrape interval to 15 seconds and instructs Prometheus to scrape metrics from its own HTTP endpoint running on port 9090.
Step 5: Create a Dedicated User for Prometheus
Create a new user named prometheus
with the following command:
$ sudo useradd --no-create-home --shell /bin/false prometheus
This creates a system user with no home directory and a disabled login shell.
Step 6: Set Permissions
Change the ownership of the Prometheus binary and other files to the prometheus
user:
$ sudo chown prometheus:prometheus /usr/local/bin/prometheus
$ sudo chown prometheus:prometheus /usr/local/bin/promtool
$ sudo chown -R prometheus:prometheus /etc/prometheus
$ sudo chown -R prometheus:prometheus /var/lib/prometheus
This ensures that the prometheus
user has the necessary permissions to read and write to the Prometheus configuration and data directories.
Step 7: Create a Systemd Unit File
Create a new systemd unit file in the /etc/systemd/system
directory:
$ sudo nano /etc/systemd/system/prometheus.service
Add the following configuration to the file:
[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
This configuration sets the prometheus
user as the owner of the service and specifies the location of the Prometheus configuration file, time-series database, and web console templates and libraries.
Step 8: Enable and Start the Prometheus Service
Reload the systemd daemon and enable the Prometheus service:
$ sudo systemctl daemon-reload
$ sudo systemctl enable prometheus.service
$ sudo systemctl start prometheus.service
This will start the Prometheus service and configure it to automatically start on system boot.
Step 9: Verify the Status of the Prometheus Service
To check the status of the Prometheus service, use the following command:
$ sudo systemctl status prometheus.service
If the service is running correctly, you should see output similar to the following:
● prometheus.service - Prometheus
Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; preset: enabled)
Active: active (running) since Tue 2023-03-21 08:38:15 -03; 12h ago
Main PID: 61092 (prometheus)
Tasks: 14 (limit: 18796)
Memory: 41.5M
CPU: 3.500s
CGroup: /system.slice/prometheus.service
└─61092 /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
mar 21 08:38:15 UX371E prometheus[61092]: ts=2023-03-21T11:38:15.825Z caller=main.go:1014 level=info fs_type=EXT4_SUPER_MAGIC
mar 21 08:38:15 UX371E prometheus[61092]: ts=2023-03-21T11:38:15.825Z caller=main.go:1017 level=info msg="TSDB started"
mar 21 08:38:15 UX371E prometheus[61092]: ts=2023-03-21T11:38:15.825Z caller=main.go:1197 level=info msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
mar 21 08:38:15 UX371E prometheus[61092]: ts=2023-03-21T11:38:15.825Z caller=main.go:1234 level=info msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDuration=396.795µs db_storage=1.26µs remote_stor>
mar 21 08:38:15 UX371E prometheus[61092]: ts=2023-03-21T11:38:15.825Z caller=main.go:978 level=info msg="Server is ready to receive web requests."
mar 21 08:38:15 UX371E prometheus[61092]: ts=2023-03-21T11:38:15.825Z caller=manager.go:974 level=info component="rule manager" msg="Starting rule manager..."
mar 21 20:20:00 UX371E prometheus[61092]: ts=2023-03-21T23:20:00.343Z caller=compact.go:519 level=info component=tsdb msg="write block" mint=1679398483422 maxt=1679400000000 ulid=01GW37HTHSHDCXPNSZ6V81DE25 duration=30.773563ms
mar 21 20:20:00 UX371E prometheus[61092]: ts=2023-03-21T23:20:00.346Z caller=head.go:1219 level=info component=tsdb msg="Head GC completed" caller=truncateMemory duration=1.79917ms
mar 21 20:20:00 UX371E prometheus[61092]: ts=2023-03-21T23:20:00.373Z caller=compact.go:519 level=info component=tsdb msg="write block" mint=1679400013425 maxt=1679407200000 ulid=01GW37HTJT4SC3005JMDYAT85H duration=26.827023ms
mar 21 20:20:00 UX371E prometheus[61092]: ts=2023-03-21T23:20:00.375Z caller=head.go:1219 level=info component=tsdb msg="Head GC completed" caller=truncateMemory duration=1.502175ms
This indicates that the Prometheus service is running correctly.
Conclusion
I have shown you how to install Prometheus as a service on Linux using a systemd unit file. We have also demonstrated how to create a dedicated user for the Prometheus service and configure the necessary permissions. By following these steps, you should now have Prometheus up and running as a service on your Linux system, ready to collect and analyze metrics.