How to Install Prometheus and Node Exporter on Rocky Linux

Prometheus is an open-source monitoring and alerting platform.

Originally, Prometheus is created by Soundcloud in 2012. Since then the Prometheus project adopted by some famous companies abe become a bigger project with very active developers and community. And In 2016, the Prometheus project is graduated under the Cloud Native Computing Foundation (CNCF).

Basic Concepts You Must Know

Basically, Prometheus collects data and metrics through HTTP endpoints from target servers, then stores all data as times series. In Prometheus, time-series data are identified by metric name and key/value pairs.

Prometheus provides flexibility through the Prometheus Query Language (PromQL). You can use PromQL to query the Prometheus time-series database.

On the target servers, you must install the 'exporter' application that exposes all data and metrics to Prometheus. 'Node Exporter' is a commonly used exporter to monitor Linux machines.

Node exporter exposes hardware and kernel-related matrics from Linux machines. It comes as a single binary file that will expose data end metrics to the Prometheus server.

Prerequisites

In this article, you will learn how to install the Prometheus monitoring tool and Node Exporter on a Rocky Linux 8 system.

Before you begin, ensure you have got the following requirement in place:

  • Operating System: Rocky Linux 8.5 (Green Obsidian)
  • Root privileges

For this example, we're using the Rocky Linux system with IP address '192.168.1.10'.

Now let's start.

Add New User and Directory

At first, you will be creating a new system user 'prometheus', then creating a new configuration directory and data directory for the Prometheus installation.

1. Execute the following command to create a new user 'prometheus'.

sudo adduser -M -r -s /sbin/nologin prometheus

2. After that, create a new configuration directory '/etc/prometheus' and the data directory '/var/lib/prometheus' using the following command.

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

All Prometheus configuration will be available at the '/etc/prometheus' directory, and all Prometheus data will automatically be saved to the directory '/var/lib/prometheus'.

Installing Prometheus on Rocky Linux

In this step, you will be installing the Prometheus monitoring system manually from the tarball or tar.gz file.

Choose the Prometheus version from this link. For this example, we're going to install Prometheus's latest version.

Download Prometheus tarball

1. Change the working directory to '/usr/src' and download the Prometheus binary using the following command.

cd /usr/src
wget https://github.com/prometheus/prometheus/releases/download/v2.31.1/prometheus-2.31.1.linux-amd64.tar.gz

After the download process completes, extract the Prometheus file.

tar -xf prometheus-2.31.1.linux-amd64.tar.gz

Now you will see a new directory 'prometheus-VERSION.OS'.

2. Next, copy all Prometheus configurations to the directory '/etc/prometheus' and the binary file 'prometheus' to the '/usr/local/bin' directory.

Setup environment variable 'PROM_SRC' to the directory '/usr/src/prometheus-*'.

export PROM_SRC=/usr/src/prometheus-*

Copy the prometheus configuration 'prometheus.yml' to the directory '/etc/prometheus.

sudo cp $PROM_SRC/prometheus.yml /etc/prometheus/

Copy the binary file 'prometheus' and 'promtool' to the directory '/usr/local/bin/'.

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

Copy Prometheus console templates and libraries to the '/etc/prometheus' directory.

sudo cp -r $PROM_SRC/consoles /etc/prometheus
sudo cp -r $PROM_SRC/console_libraries /etc/prometheus

3. Next, edit the Prometheus configuration '/etc/prometheus/prometheus.yml' using nano editor.

nano /etc/prometheus/prometheus.yml

On the 'scrape_configs' option, you may need to add monitoring jobs and how to scrape all data from the target.

The default configuration comes with the default monitoring job name 'prometheus' and the target server 'localhost' through the 'static_configs' option.

Change the target from 'localhost:9090' to the server IP address '192.168.1.10:9090' as below.

# 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: ["192.168.1.10:9090"]

Save the configuration and exit.

4. Now execute the following command to change the configuration and data directories to the user 'promethues'.

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

Now you've completed the basic prometheus installation.

Configure Prometheus

Setup Prometheus Service File

In this step, you will be setting up Prometheus as a systemd service.

1. Create a new systemd service file '/etc/systemd/system/prometheus.service' using nano editor.

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

Copy and paste the following 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

Save the configuration and exit.

2. Next, reload the systemd manager to apply a new configuration.

sudo systemctl daemon-reload

3. Now execute the following command to start and enable the Prometheus service, then check the current status of it.

sudo systemctl enable --now prometheus
sudo systemctl status prometheus

If your installation success, you will see the prometheus service is active and running as below.

Configure Prometheus as systemd service

The Prometheus monitoring tool is now accessible on the TCP port '9090.

4. Open your web browser and type the server IP address with port '9090' on the address bar.

http://192.168.1.10:9090/

And you will see the prometheus dashboard query below.

Prometheus query dashboard

Now you've completes the prometheus installation. Move to the next step for installing Node Exporter.

Installing Node Exporter on Rocky Linux

Node exporter is part of the prometheus project. You can use node exporter to export metrics of Linux servers to the Prometheus server.

In this step, you will be installing the node exporter on the Rocky Linux system from the tar.gz file.

Check this link to get the latest version of node exporter. In this example, we will install node exporter version 1.3.0.

Download Node Exporter

1. Change the working directory to '/usr/src' and download the node exporter binary file using the following command.

cd /usr/src/
wget https://github.com/prometheus/node_exporter/releases/download/v1.3.0/node_exporter-1.3.0.linux-amd64.tar.gz

If the download process completes, extract the node exporter file.

tar -xf node_exporter-1.3.0.linux-amd64.tar.gz

Now you will get a new directory 'node_exporter-VERSION.OS'.

2. Next, move the node exporter binary file to the directory '/usr/local/bin' using the following command.

mv node_exporter-*/node_exporter /usr/local/bin

Move to the next step to set up node exporter as a systemd service.

Setup Node_exporter as a service

Before creating a node exporter service file, create a new system user 'node_exporter'.

1. Execute the following command to create a new system user.

sudo adduser -M -r -s /sbin/nologin node_exporter

2. Next, create a new service file for node exporter '/etc/systemd/system/node_exporter.service' using nano editor.

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

Copy and paste the following configuration.

[Unit]
Description=Node Exporter
After=network.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target

Save the configuration and exit.

3. Now reload the systemd manager to apply the new configuration.

sudo systemctl daemon-reload

4. Start and enable the service 'node_exporter' using the following command.

sudo systemctl enable --now node_exporter

Configure Node Exporter

After that, check the current status of service 'node_exporter'.

sudo systemctl status node_exporter

Ensure the 'node_exporter' service is active and running.

Configure Node Exporter as a systemd service

5. The node exporter is running on default port '9100'. Verify using the ss command below.

ss -aplnt | grep node

You will see a similar output as below.

State    Recv-Q   Send-Q     Local Address:Port     Peer Address:Port   Process
LISTEN   0        128              0.0.0.0:22            0.0.0.0:*       users:(("sshd",pid=683,fd=4))
LISTEN   0        128                    *:9090                *:*       users:(("prometheus",pid=709,fd=8))
LISTEN   0        128                    *:9100                *:*       users:(("node_exporter",pid=5786,fd=3))
LISTEN   0        128                 [::]:22               [::]:*       users:(("sshd",pid=683,fd=6))

And you've completed the installation of node exporter on the Rocky Linux system.

Add node_exporter to Prometheus

After you complete the Prometheus and node exporter installation, add the node exporter to the Prometheus server.

1. Edit the Prometheus configuration using nano editor.

sudo nano /etc/prometheus/prometheus.yml

Under the 'scrape_config' section, add a new Prometheus job using the following configuration. And make sure to change the server IP address with your target server.

  - job_name: 'node_exporter_metrics'
    scrape_interval: 5s
    static_configs:
      - targets: ['SERVER-IP:9100']

Save the configuration and exit.

2. Now restart the Prometheus service to apply a new configuration.

sudo systemctl restart prometheus

And you've added the node exporter to the Prometheus server.

Verify Prometheus and Node Exporter Installation

1. Back to the Prometheus dashboard, click the menu 'Status' and select 'Targets'.

You will see two different endpoints, the 'prometheus' and 'node_exporter_metrics'.

Prometheus target metrics

2. Next, move to the menu 'graph' and type the PromQL query on the search bar. For this example, check the detailed operating system using the query 'node_os_info'.

And you will see detailed information about your current operating system.

prometheus examples PromQL query

3. Another example, check network speeds using the query 'node_network_speed_bytes', then move to the tab 'Graph'. And you will see a similar output as below.

prometheus examples PromQL query

Now you've successfully installed the Prometheus and node exporter on the Rocky Linux system.

Conclusion

Congratulations! You've learned the basic installation and configuration of the Prometheus monitoring system and node exporter on the Rocky Linux system.

For the next step, you may explore another Prometheus exporter for other applications and Grafana as the monitoring dashboard.

Share this page:

0 Comment(s)