Setting Up Prometheus, Node Exporter, and Grafana

Performance monitoring and alerting are very crucial to measure the performance metrics of an application running in a production environment. In this blog post, we are going to talk about two popular open-source tools Prometheus and Grafana.

What is Prometheus?

Prometheus is an open-source tool for scraping the performance metrics of any given hardware resource (CPU, VM, Cloud Virtual Machine, Router, etc.). Prometheus scrapes the data with a timestamp, which can then be stored on a server and accessed using PromQL.
Prometheus has superb support APIs, enabling seamless integration with resources present in the data center. Its scalable architecture, coupled with third-party libraries, makes it even more powerful.

What is Grafana?

In layman's terms, Grafana is a user interface for viewing the metrics scraped by Prometheus from various resources. Grafana is an open-source analytics and visualization tool. While Grafana itself does not store data, it relies on Prometheus to send data, allowing dashboards to be prepared.

Grafana also provides notification and mail alert capabilities based on various thresholds. One of its standout features is Grafana Labs, where you can download dashboards created by other developers, saving time and effort.

1. Installing Prometheus

Prometheus offers various installation methods, including pre-compiled binaries, source builds, and Docker. To keep things simple, we’ll use pre-compiled binaries for installation on an Ubuntu Linux machine.

Steps for Installing Prometheus:

Go to the Prometheus Download Page of Prometheus and download the file prometheus-x.xx.x.linux-amd64.tar.gz (replace x.xx.x with the version number).

Extract the downloaded file:

tar xvfz prometheus-*.*-amd64.tar.gz

Navigate to the extracted directory:

cd prometheus-x.xx.x.linux-amd64.tar.gz

Start the Prometheus server:

./prometheus

The Prometheus server will start on port 9090.

Access the Prometheus Graph UI at http://localhost:9090/graph.

Access the Prometheus Metrics UI at http://localhost:9090/metrics.

2. How to Create and Install Node Exporter

Node Exporter fetches statistics from various hardware and virtual resources in a format that Prometheus can understand. Using the Prometheus server, these statistics can then be exposed on port 9100.
Here are the key metrics Node Exporter tracks:

  • CPU Usage

  • Memory Usage

  • Disk Usage

  • Network Usage

2.1 Installing Node Exporter

Download Node export er binary for your operating system (e.g., Ubuntu Linux).

Extract the downloaded file:

tar xvfz node_exporter-*.*-amd64.tar.gz

Navigate to the extracted directory:

cd node_exporter-*.*-amd64

Start the Node Exporter:

./node_exporter

Access Node Exporter metrics at http://localhost:9100.

2.2 Adding Node Exporter scrape_configs to Prometheus (YAML Configuration)

If Node Exporter and Prometheus are on the same server, you can define scrape_configs with target hosts in the YAML configuration.
Here’s an example configuration:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: node
    static_configs:
      - targets: ['localhost:9100', '100.0.0.3:9100']
  • Replace 100.0.0.3 with your host IP or hostname.

  • Save the configuration file as exporter.yml.

  • Start Prometheus with the updated configuration:

./prometheus --config.file=exporter.yml

2.3 Enable and Disable Collectors in Node Exporter

Node Exporter allows enabling or disabling specific collectors with runtime arguments:

  • To disable a collector:

      node_exporter --no-collector.<COLLECTOR-NAME>
    
  • To enable a collector:

      node_exporter --collector.<COLLECTOR-NAME>
    

3. Installing Grafana

With Prometheus and Node Exporter installed, we’ll now install Grafana for visualizing the scraped data.

Steps for Installing Grafana:

  1. Update the package info:
sudo apt-get install -y apt-transport-https
sudo apt-get install -y software-properties-common wget
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
  1. Add stable repository of Grafana -

     echo "deb https://packages.grafana.com/enterprise/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
    
  1. Update repository and Install Grafana

sudo apt-get update
sudo apt-get install grafana-enterprise
  1. Start the Grafana server:
sudo systemctl daemon-reload
sudo systemctl start grafana-server
sudo systemctl status grafana-server
  1. Configure Grafana to run at boot time

sudo systemctl enable grafana-server.service
  1. Access the Grafana Dashboard using http://localhost:3000/login

  2. The default login Username: Password for accessing the Grafana dashboard is admin: admin

  3. Change the default admin password after the login

4. Setting up the Grafana Dashboard

Once Grafana is installed, we need to set up the Grafana Dashboard by connecting it to the Prometheus data source.

4.1 Create a Prometheus Data Source in Grafana

  1. Open the Grafana dashboard and go to Settings (Gear Icon)Data Sources.

  2. Click on Add Data Source.

  3. Select Prometheus as the data source.

  4. Enter the hostname or IP address of the Prometheus server.

  5. Save and test the data source.

4.2 Import Grafana Dashboard from Grafana Labs

  1. Search for pre-existing open-source dashboards on Grafana Labs (e.g., "Linux Memory").

  2. Import a dashboard using its Dashboard ID.

By following these steps, you can successfully set up Prometheus, Node Exporter, and Grafana for monitoring and visualizing metrics from various resources.