Setting up Prometheus and Grafana

What is Prometheus?

Prometheus is an open-source monitoring and alerting toolkit originally developed at SoundCloud. It is designed for reliability and scalability, ideal for collecting time-series metrics from cloud-native environments like Kubernetes or AWS. Prometheus stores data in a custom time-series database and uses a flexible query language called PromQL.

Prometheus works by scraping metrics from targets (like EC2, Node Exporter, or containers) at specified intervals. These metrics are then stored locally and can be queried or used to trigger alerts.

Prometheus supports service discovery, meaning it can automatically detect and begin monitoring new services based on changes in your infrastructure.

What is Grafana?

Grafana is an open-source analytics and visualization platform that integrates seamlessly with Prometheus. It helps users create interactive and customizable dashboards to visualize metrics data. Grafana supports alerts, templating, role-based access, and can be used to monitor everything from infrastructure and logs to user applications.

Grafana doesn’t collect data itself — it reads it from sources like Prometheus, InfluxDB, MySQL, Loki, and more.

How Prometheus and Grafana Work Together

  • Prometheus collects and stores metrics from services and infrastructure.

  • Grafana reads these metrics via Prometheus’s HTTP API and visualizes them.

  • This combination provides real-time monitoring, alerting, and visual dashboards for DevOps and SRE teams.

    Install Prometheus and Grafana on AWS EC2

  • You can set up Prometheus and Grafana on your EC2 instance in just a few steps.

    Install Prometheus

    # Amazon Linux 2
    sudo yum update -y
    
    # Ubuntu
    sudo apt-get update
  • 2. Create a Prometheus user and folders
    sudo useradd --no-create-home --shell /bin/false prometheus
    
    sudo mkdir /etc/prometheus /var/lib/prometheus
  • 3. Download and install Prometheus
    wget https://github.com/prometheus/prometheus/releases/download/v2.34.0/prometheus-2.34.0.linux-amd64.tar.gz
    tar -xvzf prometheus-2.34.0.linux-amd64.tar.gz
    cd prometheus-2.34.0.linux-amd64
    
    sudo cp prometheus promtool /usr/local/bin/
    sudo cp -r consoles console_libraries /etc/prometheus
    sudo cp prometheus.yml /etc/prometheus
    sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus

    4. Create systemd service file

    sudo vim /etc/systemd/system/prometheus.service
  • Paste the following:
    [Unit]
    Description=Prometheus
    After=network.target
    
    [Service]
    User=prometheus
    ExecStart=/usr/local/bin/prometheus \
    --config.file=/etc/prometheus/prometheus.yml \
    --storage.tsdb.path=/var/lib/prometheus/
    Restart=always
    
    [Install]
    WantedBy=multi-user.target

    5. Start Prometheus

    sudo systemctl daemon-reexec
    
    sudo systemctl enable prometheus
    
    sudo systemctl start prometheus
  • Prometheus will run on port 9090
    Check by visiting:
    http://<your-ec2-public-ip>:9090
  • Install Grafana
    ubuntu
    sudo apt-get install -y software-properties-common
    sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
    sudo apt-get update
    sudo apt-get install grafana

    Amazon Linux 2

    sudo yum install -y https://dl.grafana.com/oss/release/grafana-8.3.0-1.x86_64.rpm

    Start Grafana

    sudo systemctl enable grafana-server
    
    sudo systemctl start grafana-server

    Grafana will run on port 3000
    Access it via:

    http://<your-ec2-public-ip>:3000
    Login to Grafana
    • Username: admin

    • Password: admin (you will be prompted to change it)

    Add Prometheus as a Data Source in Grafana
    1. Click the gear icon in the left menu → Data Sources.

    2. Select Prometheus.

    3. Set the URL to:
      http://<your-ec2-public-ip>:9090

    4. Click Save & Test to verify the connection.
      Using Node Exporter for System Metrics

      To monitor EC2-level system metrics like CPU, disk, and memory:

      1. Download and install Node Exporter:
        
        wget https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-amd64.tar.gz
        tar -xvzf node_exporter-1.5.0.linux-amd64.tar.gz
        cd node_exporter-1.5.0.linux-amd64
        ./node_exporter
        
        

      2. Update  prometheus.yml

      scrape_configs:
      
      - job_name: 'node'
      
      static_configs:
      
      - targets: ['localhost:9100']

    3. Restart Prometheus to load changes:

                 sudo systemctl restart prometheus

Conclusion

Prometheus and Grafana together form a powerful, open-source monitoring and visualization stack. Setting them up on AWS EC2 gives you full control of your metrics and dashboards. Whether you’re monitoring applications, infrastructure, or containers, this setup gives you the tools to ensure performance and reliability.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *