Monitoring is crucial for maintaining the health and performance of any infrastructure. Prometheus, coupled with Node Exporter, provides an effective solution for collecting and visualizing metrics from your servers. In this guide, we’ll walk through setting up Node Exporter on an AWS EC2 instance and configuring Prometheus to scrape metrics from it.

Introduction to Node Exporter and Prometheus

Prometheus is an open-source systems monitoring and alerting toolkit designed to collect metrics from configured targets at specified intervals, evaluate rule expressions, and generate alerts if needed. It is widely used for monitoring cloud-native applications and infrastructure.

Node Exporter is a Prometheus exporter for hardware and OS metrics exposed by *nix kernels. It allows Prometheus to collect metrics such as CPU usage, memory utilization, disk I/O, and network statistics from Linux-based systems. By setting up Node Exporter on an EC2 instance, you can monitor your instance’s performance in real time.

Fetching and Unpacking the Node Exporter Release

  1. Connect to Your EC2 Instance
    First, connect to your EC2 instance via SSH. Replace <instance-ip> with the public IP of your instance:

    ssh -i /path/to/your-key.pem ec2-user@<instance-ip>
  1. Download the Latest Node Exporter Release
    Navigate to the Node Exporter GitHub releases page and copy the download link for the latest version. Use wget to download it to your EC2 instance:

    wget https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-amd64.tar.gz
  1. Unpack the Downloaded Tarball
    Extract the tarball to a suitable location:

    tar xvfz node_exporter-1.5.0.linux-amd64.tar.gz

cd node_exporter-1.5.0.linux-amd64/

  1. Move the Node Exporter Binary
    Move the node_exporter binary to /usr/local/bin:

    sudo mv node_exporter /usr/local/bin/

Creating a Systemd Unit File for Node Exporter

To manage Node Exporter as a service, we’ll create a systemd unit file:

  1. Create the Unit File
    Create a new node_exporter.service file:

    sudo nano /etc/systemd/system/node_exporter.service
  1. Add the Following Configuration

    [Unit]

Description=Node Exporter

[Service]

User=node_exporter

ExecStart=/usr/local/bin/node_exporter

Restart=always

[Install]

WantedBy=multi-user.target

  1. Create a Node Exporter User
    Create a system user for running the Node Exporter service:

    sudo useradd –no-create-home –shell /bin/false node_exporter

Enabling and Starting the Node Exporter Service

  1. Reload the Systemd Daemon
    After creating the unit file, reload the systemd daemon to recognize the new service:

    sudo systemctl daemon-reload
  1. Enable and Start the Service
    Enable Node Exporter to start on boot and start the service immediately:

    sudo systemctl enable node_exporter

sudo systemctl start node_exporter

  1. Verify the Service Status
    Ensure that Node Exporter is running:

    sudo systemctl status node_exporter

Verifying Node Exporter Metrics

To verify that Node Exporter is working correctly:

  1. Check the Metrics
    Open a browser and navigate to http://<instance-ip>:9100/metrics. You should see a long list of metrics being exposed by Node Exporter.
  2. Test with Curl
    You can also use curl to fetch metrics:

    curl http://localhost:9100/metrics

 

Configuring Prometheus to Scrape Node Exporter Metrics

Next, you’ll need to configure Prometheus to scrape metrics from the Node Exporter:

  1. Edit the Prometheus Configuration File
    Assuming Prometheus is already installed and running, edit its configuration file (prometheus.yml):

    – job_name: ‘node_exporter’

  static_configs:

    – targets: [‘<instance-ip>:9100’]

  1. Restart Prometheus
    After saving the changes, restart Prometheus to apply the new configuration:

    sudo systemctl restart prometheus

Verifying Target Status in Prometheus

  1. Access Prometheus UI
    Navigate to your Prometheus server’s UI at http://<prometheus-server-ip>:9090/.
  2. Check Target Status
    Under the “Status” -> “Targets” section, verify that the Node Exporter target is listed and in the “UP” state.

Conclusion

Following this guide, you have successfully set up Node Exporter on an EC2 instance and configured Prometheus to monitor its metrics. This setup provides a robust foundation for monitoring your infrastructure and ensuring optimal performance.

References

Set up and configure Prometheus metrics collection on Amazon EC2 instances

Automating Amazon EC2 Instances Monitoring with Prometheus EC2 Service Discovery and AWS Distro for OpenTelemetry