Grafana is an open-source platform for monitoring, visualization and analysis providing powerful and flexible visualizations with support for various data sources including Prometheus. This blog will guide you through installing Grafana, connecting it to Prometheus and creating your first dashboard to visualize your metrics.
Step-by-Step Guide on Installing Grafana
Step 1 :- Download and Install Grafana
Grafana is available for multiple operating systems. The installation process is straightforward and we will cover the steps for Linux, macOS and Windows.
Installing on Linux
Add the Grafana APT repository :-
sudo apt-get install -y software-properties-common sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
Add the Grafana GPG key :-
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
Install Grafana :-
sudo apt-get update sudo apt-get install grafana
Start and enable the Grafana service :-
sudo systemctl start grafana-server sudo systemctl enable grafana-server
Installing on macOS
Install Grafana using Homebrew :-
brew update brew install grafana
Start Grafana :-
brew services start grafana
Installing on Windows
Download the Grafana installer from the official Grafana website.
Run the installer and follow the on-screen instructions.
Start Grafana by running grafana-server from the command prompt.
Step 2 :- Access Grafana Web UI
After installation Grafana's web UI is accessible at localhost:3000. Open this URL in your web browser.
Step 3 :- Log In to Grafana
The default login credentials are :-
Username: admin
Password: admin
You will be prompted to change the default password upon first login.
How to Connect Grafana to Prometheus as a Data Source
Navigate to Configuration :-
Click on the gear icon in the left sidebar to open the Configuration menu.
Select Data Sources.
Add New Data Source :-
Click on the Add data source button.
Select Prometheus from the list of available data sources.
Configure the Data Source :-
In the URL field, enter the address of your Prometheus server (e.g. localhost:3000).
Set the Access mode to Server if Grafana and Prometheus are on the same server or Browser if accessing remotely.
Click Save & Test to verify the connection.
If the connection is successful you will see a green message indicating that the data source is working.
Overview of Grafana's Interface and Key Features
Grafana's interface is user-friendly and packed with features that make it easy to visualize and analyze data. Here are the key components :-
Grafana UI Components
Home Dashboard :-
The initial screen you see after logging in.
Provides quick access to creating dashboards, managing data sources and other configuration options.
Side Menu :-
- Located on the left side this menu provides access to Dashboards, Explore, Alerts and Configuration options.
Dashboards :-
The main area where you create and view visualizations.
Can be customized with different panels, rows and plugins.
Explore :-
- An interactive query console to perform ad-hoc queries and visualize data.
Alerts :-
- Configure alerts to notify you when certain conditions are met based on your data.
Key Features of Grafana
Customizable Dashboards :-
Create and organize dashboards with multiple panels.
Panels can be resized and arranged as needed.
Rich Visualization Options :-
Supports various types of visualizations, including graphs, heatmaps, tables and more.
Extensive customization options for each panel.
Templating and Variables :-
- Create dynamic dashboards with variables to change queries and visualizations on the fly.
Plugins :-
- Extend Grafana's functionality with a wide range of plugins for data sources, panels and apps.
Alerting :-
- Set up alerts to monitor critical metrics and receive notifications through email, Slack or other channels.
Creating Your First Dashboard and Panel
Let's create a simple dashboard to visualize metrics from Prometheus.
Step 1 :- Create a New Dashboard
Navigate to Dashboards :-
Click on the Dashboards icon in the side menu.
Select New Dashboard.
Add a New Panel :-
- Click on Add new panel.
Step 2 :- Configure the Panel
Select Data Source :-
- In the Query section select Prometheus as the data source.
Enter PromQL Query :-
In the query editor enter a PromQL query to retrieve data. For eg., to visualize CPU usage from Node Exporter use :-
node_cpu_seconds_total
Configure Visualization :-
Select the type of visualization you want (e.g., Graph, Gauge, Table).
Customize the visualization settings such as the title, legend and axes.
Step 3 :- Save the Dashboard
Save the Panel :-
- Click on Apply to save the panel configuration.
Save the Dashboard :-
Click on the Save dashboard icon at the top of the screen.
Enter a name for the dashboard and click Save.
Example :- Visualizing CPU Usage
Let's create a more specific example of visualizing CPU usage from Node Exporter.
Add a New Panel :-
- In the new dashboard click on Add new panel.
Configure the Panel :-
Select Prometheus as the data source.
Enter the following PromQL query to calculate the CPU usage rate :-
rate(node_cpu_seconds_total{mode!="idle"}[5m])
This query calculates the rate of CPU usage excluding the idle time over the last 5 minutes.
Customize the Visualization :-
Set the visualization type to Graph.
Configure the graph settings, such as the title (CPU Usage), legend and axes.
Apply and Save :-
Click on Apply to save the panel.
Save the dashboard with a meaningful name such as System Metrics.
Step 4 :- Adding More Panels
To make your dashboard more comprehensive you can add additional panels to visualize other metrics such as memory usage, disk I/O and network traffic.
Memory Usage
Add a New Panel.
Configure the Panel :-
Select Prometheus as the data source.
Enter the following PromQL query to visualize total memory usage :-
node_memory_MemTotal_bytes
Customize the visualization settings.
Apply and Save :-
Click on Apply to save the panel.
Save the dashboard.
Disk I/O
Add a New Panel.
Configure the Panel :-
Select Prometheus as the data source.
Enter the following PromQL query to visualize disk read bytes :-
rate(node_disk_read_bytes_total[5m])
Customize the visualization settings.
Apply and Save :-
Click on Apply to save the panel.
Save the dashboard.
Network Traffic
Add a New Panel.
Configure the Panel :-
Select Prometheus as the data source.
Enter the following PromQL query to visualize network receive bytes :-
rate(node_network_receive_bytes_total[5m])
Customize the visualization settings.
Apply and Save :-
Click on Apply to save the panel.
Save the dashboard.
Conclusion
Grafana is a powerful tool for visualizing and analyzing metrics from various data sources including Prometheus. In this blog we covered the installation and configuration of Grafana, connecting it to Prometheus, exploring Grafana's interface and key features and creating your first dashboard and panels.
By following this guide you now have the foundational knowledge to set up and use Grafana for monitoring and visualization. Stay tuned for more insights and practical tips to enhance your monitoring setup.