How to scrape data from an EC2 instance and expose it to Prometheus using the Node Exporter?

How to scrape data from an EC2 instance and expose it to Prometheus using the Node Exporter?

To scrape data from an EC2 instance and expose it to Prometheus using the Node Exporter, you need to follow these general steps:

  1. Set up an EC2 instance: Launch an EC2 instance on AWS with the appropriate operating system (e.g., Ubuntu, CentOS). Ensure that the necessary security groups and firewall rules are in place to allow communication between the EC2 instance and Prometheus server.

  2. Install Node Exporter: SSH into the EC2 instance and download the Node Exporter binary suitable for your operating system. Extract the archive and run the Node Exporter as a service. For example, on Ubuntu, you can use the following commands:

     shellCopy code$ wget https://github.com/prometheus/node_exporter/releases/download/vX.X.X/node_exporter-X.X.X.linux-amd64.tar.gz
     $ tar xvfz node_exporter-X.X.X.linux-amd64.tar.gz
     $ cd node_exporter-X.X.X.linux-amd64
     $ ./node_exporter
    

    Replace X.X.X with the desired version number.

  3. Verify Node Exporter: Access the Node Exporter's metrics endpoint to confirm it's running correctly. Open a web browser and enter http://<EC2_instance_IP>:9100/metrics. You should see a page with various metrics in plain text format.

  4. Configure Prometheus: Edit the Prometheus configuration file (prometheus.yml) to include the EC2 instance as a target for scraping. Add a job section with appropriate labels, specifying the EC2 instance's IP address or hostname and the port (default is 9100). For example:

     yamlCopy codescrape_configs:
       - job_name: 'node'
         static_configs:
           - targets: ['<EC2_instance_IP>:9100']
    
  5. Restart Prometheus: If Prometheus is already running, restart it to apply the updated configuration.

  6. Verify scraping: Access the Prometheus expression browser by opening http://<Prometheus_server_IP>:9090/graph in a web browser. Execute a query like node_cpu or node_memory_MemTotal to verify that Prometheus is scraping metrics from the EC2 instance successfully.

By following these steps, you should be able to scrape data from the EC2 instance using the Node Exporter and monitor it in Prometheus. You can then create custom dashboards and alerts based on the scraped metrics in Prometheus.