Running Prometheus as a Service on Ubuntu

Running Prometheus as a Service on Ubuntu: A Step-by-Step Guide

Prometheus, the open-source monitoring and alerting toolkit, has gained immense popularity for its ability to provide real-time insights into system performance. In this guide, we will walk you through the process of setting up Prometheus as a service on an Ubuntu system. By following these steps, you’ll be able to harness the power of Prometheus to monitor your infrastructure effectively.

Prerequisites

Before we begin, ensure you have the following:

  1. An Ubuntu system (version 18.04 or higher) with root access.
  2. Basic knowledge of the command line.

Step 1: Update System Packages

Start by updating your system’s package list to ensure you have the latest software available:

sudo apt update
sudo apt upgrade

Step 2: Download and Extract Prometheus

  1. Visit the Prometheus official download page to obtain the latest version’s URL: Prometheus Downloads.
  2. Use the wget command to download Prometheus. Replace <PROMETHEUS_URL> with the actual URL you obtained in the previous step:
wget <PROMETHEUS_URL>
  1. Extract the downloaded archive:
tar xvfz prometheus-*.tar.gz

4. Move the extracted directory to a suitable location. For example, /opt/prometheus:

sudo mv prometheus-* /opt/prometheus

Step 3: Create a Prometheus User

sudo useradd -M -r -s /bin/false prometheus

Step 4: Set Up Directories and Permissions

Create necessary directories and adjust permissions:

sudo mkdir -p /var/lib/prometheus/data
sudo chown prometheus:prometheus /var/lib/prometheus/data
sudo mkdir /etc/prometheus
sudo chown prometheus:prometheus /etc/prometheus

Step 5: Configuration

Create a Prometheus configuration file. You can use a simple configuration as a starting point:

sudo nano /etc/prometheus/prometheus.yml

Paste the following content into the file:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

Save and exit the text editor.

Let’s see what does this mean :

  • global: This block allows you to define global configuration options that apply to the entire Prometheus configuration.
  • scrape_interval: 15s: This line sets the interval at which Prometheus will scrape (collect) data from its targets. In this case, it’s set to 15 seconds (15s), meaning that Prometheus will retrieve data from its configured targets every 15 seconds.
  • scrape_configs: This block defines an array of scrape configurations. Each configuration specifies how Prometheus should collect metrics from specific targets.
  • - job_name: 'prometheus': This is the name you’re giving to the scrape job configuration. It’s a way to label and identify this specific job.
  • static_configs:: This block defines the list of static (unchanging) targets that Prometheus will scrape data from.
  • - targets: ['localhost:9090']: This line specifies the targets from which Prometheus should collect metrics. In this case, you have only one target defined: 'localhost:9090'. This means that Prometheus will scrape metrics from the local host at port 9090.

Step 6: Create a Systemd Service File

Create a systemd service file to manage Prometheus as a service:

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

Add the following content:

[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
ExecStart=/opt/prometheus/prometheus --config.file /etc/prometheus/prometheus.yml --storage.tsdb.path /var/lib/prometheus/data

[Install]
WantedBy=default.target

Save and exit the text editor.

Step 7: Start and Enable Prometheus

Start the Prometheus service and enable it to start on boot:

sudo systemctl daemon-reload
sudo systemctl start prometheus
sudo systemctl enable prometheus

Conclusion

Congratulations! You’ve successfully set up Prometheus as a service on your Ubuntu system. With Prometheus now actively monitoring your infrastructure, you can explore its rich set of features and delve into its data visualization and alerting capabilities. This setup ensures that Prometheus runs reliably, providing you with invaluable insights into your system’s performance and aiding in maintaining a robust and optimized environment.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *