Monday, May 6, 2024

How to Set Up Node Exporter as a System Service for Prometheus Monitoring

How to Set Up Node Exporter as a System Service for Prometheus Monitoring

Running the Node Exporter as a service in Prometheus involves setting up the Node Exporter on your machine and configuring it to run as a system service so that it starts automatically at system boot. The Node Exporter is a Prometheus exporter that collects hardware and OS metrics exposed by *NIX kernels, then exports them over HTTP for Prometheus to scrape.

Here's how to set up and run Node Exporter as a service on a Linux system using systemd (which is commonly used in distributions like Ubuntu, CentOS, Debian, etc.):

Step 1: Download and Install Node Exporter

  1. Go to the Prometheus downloads page: Navigate to the official Prometheus downloads page and find the latest version of Node Exporter.

  2. Download and unpack Node Exporter:

    bash
    cd /tmp wget https://github.com/prometheus/node_exporter/releases/download/v*/node_exporter-*.*-amd64.tar.gz tar xzf node_exporter-*.*-amd64.tar.gz

    Replace *.* with the actual version number of Node Exporter.

  3. Move the Node Exporter binary to /usr/local/bin:

    bash
    sudo cp node_exporter-*/node_exporter /usr/local/bin/

Step 2: Create a Systemd Service File

  1. Create a new systemd service file:

    bash
    sudo nano /etc/systemd/system/node_exporter.service
  2. Add the following content to the service file:

    ini
    [Unit] Description=Node Exporter Wants=network-online.target After=network-online.target [Service] User=prometheus Group=prometheus Type=simple ExecStart=/usr/local/bin/node_exporter [Install] WantedBy=multi-user.target

    Make sure to replace User and Group with the user and group you want to run Node Exporter under. If the prometheus user does not exist, create it using sudo useradd -rs /bin/false prometheus.

Step 3: Enable and Start the Service

  1. Reload systemd to read the new unit file:

    bash
    sudo systemctl daemon-reload
  2. Enable Node Exporter to start at boot:

    bash
    sudo systemctl enable node_exporter
  3. Start the Node Exporter service:

    bash
    sudo systemctl start node_exporter

Step 4: Verify the Service

  1. Check the status of the Node Exporter service:

    bash
    sudo systemctl status node_exporter

    This command shows you whether the service is active and running without any issues.

  2. Check if Node Exporter is serving metrics: Visit http://<your-server-ip>:9100/metrics in a web browser or use curl:

    bash
    curl http://localhost:9100/metrics

    This should display a page full of system metrics, indicating that Node Exporter is working correctly.

By following these steps, Node Exporter will be set up and running as a systemd service, automatically starting on boot and ready to be scraped by Prometheus. This setup ensures that you continuously collect and monitor the metrics from your system.


No comments:

Post a Comment