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 โ๏ธ
Login to AWS Management Console:
Navigate to the AWS Management Console. ๐
Select the region where you want to launch your instances. ๐
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. ๐
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). ๐
- Repeat the steps above, but add a tag like
Step 2: Install Grafana on the First Ubuntu Instance ๐ง
Connect to your Grafana Instance:
Use SSH to connect to your Grafana instance:
ssh -i your-key.pem ubuntu@your-grafana-instance-public-ip
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 ๐ง
Connect to your Prometheus Instance:
Use SSH to connect to your Prometheus instance:
ssh -i your-key.pem ubuntu@your-prometheus-instance-public-ip
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 ๐
Log in to Grafana:
- Open your Grafana instance in a web browser. ๐
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 ๐
Create a New Dashboard:
- In Grafana, go to the
+
menu on the left sidebar and choose Dashboard. โ
- In Grafana, go to the
Add a New Panel:
Click on Add new panel. โ
Select the Prometheus data source. ๐
Enter Query for CPU Usage:
Use the following query to monitor user CPU usage:
rate(node_cpu_seconds_total{mode="user"}[1m])
Configure Visualization Settings:
Customize the visualization settings as needed. ๐จ
Click Apply to save the panel. ๐พ
Step 6: Explore and Analyze Metrics ๐
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! ๐