Day 74 - Connecting EC2 with Grafana ๐ŸŽ‰

Nilkanth MistryNilkanth Mistry
4 min read

Introduction ๐Ÿ“Š

Monitoring the performance of your server infrastructure is crucial for ensuring optimal operation and identifying potential issues. In this tutorial, we'll explore how to set up Grafana and Prometheus to monitor CPU usage on Ubuntu instances. Grafana provides a powerful visualization interface, while Prometheus collects and stores metrics.

Prerequisites ๐Ÿ“‹

Before we begin, make sure you have:

  • Two Ubuntu instances running on your preferred cloud provider (e.g., AWS, Azure).

  • SSH access to both instances.

  • Basic knowledge of Ubuntu commands and Prometheus/Grafana.

Step-by-Step Guide ๐Ÿ› ๏ธ

Step 1: Launch Ubuntu Instances on AWS โ˜๏ธ

  1. Login to AWS Management Console:

    • Navigate to the AWS Management Console. ๐ŸŒ

    • Select the region where you want to launch your instances. ๐ŸŒ

  2. Launch First Ubuntu EC2 Instance:

    • Go to the EC2 Dashboard. ๐Ÿ“Š

    • Click on Launch Instance. ๐Ÿš€

    • Choose an Ubuntu AMI (e.g., Ubuntu Server 20.04 LTS). ๐Ÿง

    • Select the Instance Type (e.g., t2.micro for free tier). ๐Ÿ’ก

    • Configure the instance details and add storage as needed. ๐Ÿ“ฆ

    • Add a tag for easier identification (e.g., Name: Grafana-Server). ๐Ÿท๏ธ

    • Configure the security group to allow HTTP (port 80) and Grafana (port 3000). ๐Ÿ”

    • Review and Launch the instance. ๐ŸŽ‰

  3. Launch Second Ubuntu EC2 Instance:

    • Repeat the steps above, but add a tag like Name: Prometheus-Server and ensure the security group allows Prometheus (port 9090). ๐Ÿ”

Step 2: Install Grafana on the First Ubuntu Instance ๐Ÿง

  1. Connect to your Grafana Instance:

    • Use SSH to connect to your Grafana instance:

        ssh -i your-key.pem ubuntu@your-grafana-instance-public-ip
      
  2. Install Grafana:

    • Update the system and install required packages:

        sudo apt-get update
        sudo apt-get install -y software-properties-common
      

    • Add the Grafana APT repository and its GPG key:

        sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
        wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
      

    • Install Grafana:

        sudo apt-get update
        sudo apt-get install -y grafana
        sudo systemctl enable grafana-server
        sudo systemctl start grafana-server
      

    • Access Grafana by navigating to http://<your-grafana-instance-ip>:3000 in your browser, using the default login credentials (admin/admin). ๐ŸŒ๐Ÿ”‘

Step 3: Install Prometheus on the Second Ubuntu Instance ๐Ÿง

  1. Connect to your Prometheus Instance:

    • Use SSH to connect to your Prometheus instance:

        ssh -i your-key.pem ubuntu@your-prometheus-instance-public-ip
      
  2. Install Prometheus:

    • Update the system and install Prometheus:

        sudo apt-get update
        sudo apt-get install -y prometheus
        sudo systemctl enable prometheus
        sudo systemctl start prometheus
      

Step 4: Configure Prometheus as a Data Source in Grafana ๐Ÿ“ˆ

  1. Log in to Grafana:

    • Open your Grafana instance in a web browser. ๐ŸŒ
  2. Add Prometheus Data Source:

    • Navigate to Settings > Data Sources. โš™๏ธ

    • Click on Add your first data source. โž•

    • Choose Prometheus and enter the IP address of the second Ubuntu instance (e.g., http://<your-prometheus-instance-ip>:9090). ๐ŸŒ

    • Click Save & Test to verify the connection. โœ…

Step 5: Create CPU Usage Dashboard in Grafana ๐Ÿ“Š

  1. Create a New Dashboard:

    • In Grafana, go to the + menu on the left sidebar and choose Dashboard. โž•
  2. Add a New Panel:

    • Click on Add new panel. โž•

    • Select the Prometheus data source. ๐Ÿ”

  3. Enter Query for CPU Usage:

    • Use the following query to monitor user CPU usage:

        rate(node_cpu_seconds_total{mode="user"}[1m])
      
  4. Configure Visualization Settings:

    • Customize the visualization settings as needed. ๐ŸŽจ

    • Click Apply to save the panel. ๐Ÿ’พ

Step 6: Explore and Analyze Metrics ๐Ÿ”

  1. Explore the Grafana Dashboard:

    • Navigate through the Grafana dashboard to visualize and analyze CPU usage metrics from both Ubuntu instances. ๐Ÿ“ˆ

    • Customize panels and queries based on your monitoring requirements. ๐Ÿ› 

Conclusion ๐ŸŽ‰

Setting up Grafana and Prometheus on Ubuntu allows you to efficiently monitor CPU usage and other system metrics. This tutorial provides a basic configuration, and you can expand on it to include additional metrics and visualizations according to your specific needs.

Thank you for reading! ๐Ÿ™


By following these steps, you'll be able to monitor your Ubuntu instances using Grafana and Prometheus, ensuring optimal performance and quick issue detection. Don't forget to share your progress and tag us on LinkedIn! ๐Ÿ“ข

Happy Learning! ๐Ÿ˜Š

0
Subscribe to my newsletter

Read articles from Nilkanth Mistry directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nilkanth Mistry
Nilkanth Mistry

Embark on a 90-day DevOps journey with me as we tackle challenges, unravel complexities, and conquer the world of seamless software delivery. Join my Hashnode blog series where we'll explore hands-on DevOps scenarios, troubleshooting real-world issues, and mastering the art of efficient deployment. Let's embrace the challenges and elevate our DevOps expertise together! #DevOpsChallenges #HandsOnLearning #ContinuousImprovement