Setting Up Grafana with Prometheus

Setting Up Grafana with Prometheus

3/17/2022, 12:00:00 AM
kubernetes
prometheus
grafana

Grafana is an interactive web application used to monitor our systems.

Grafana is open-source analytics and monitoring software. It helps to create, explore and share the data using the dashboards.  It helps us to query, visualize and understand the data.

In this hands-on lab, we will set up Grafana and add Prometheus as a data source to it. You can refer to the previous hands-on lab Introduction to Prometheus to set up Prometheus on a virtual machine.

About Lab Setup

Our lab has been set up with necessary tools like base OS (Ubuntu), developer tools like Git, Vim, wget, and others. 

You can bring this setup by clicking the “Lab Setup” button on the right side of the screen.

Note that there are three app-specific URLs exposed specifically for the hands-on lab purpose.

Prerequisite

Before installing Grafana we will install Prometheus and Node Exporter on top of our basic lab setup.

1. Install Prometheus

curl -L https://github.com/prometheus/prometheus/releases/download/v2.33.1/prometheus-2.33.1.linux-amd64.tar.gz --output prometheus-2.33.1.linux-amd64.tar.gz && tar -xf prometheus-2.33.1.linux-amd64.tar.gz

Then start the Prometheus server as a background process.

nohup ./prometheus-2.33.1.linux-amd64/prometheus --config.file=prometheus.yml &

Now, if you click on the prometheus-web under Lab URLs, you will see the Prometheus dashboard.

2. Install Node Exporter

curl -L https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz -o node_exporter-1.3.1.linux-amd64.tar.gz  && tar -xf node_exporter-1.3.1.linux-amd64.tar.gz

Then start the Node Exporter program in the background by running the following command:

nohup ./node_exporter-1.3.1.linux-amd64/node_exporter &

Now, if you click on node-exporter under Lab URLs, you will get Node Exporter with a link to Metrics. Click on Metrics to see its content.

Installing Grafana

Now, our lab setup is ready for installing Grafana since we already have installed the Prometheus server on it.

We are using the open-source version of Grafana.

Run the following command to install Grafana:

sudo apt-get install -y apt-transport-https &&
sudo apt-get install -y software-properties-common && 
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -

Now we will be adding the repository for stable releases:

echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list

Install the Grafana Package.

sudo apt-get update &&
sudo apt-get install grafana

By default, the Grafana server runs at a port 3000. However, we will first change the default port as there are some backend processes running on our Lab at the same port.

The command shown below will change http_port to 30000 which is currently set to 3000 in the configuration file of grafana /usr/share/grafana/conf/defaults.ini.

sed -i 's/3000/30000/g' /usr/share/grafana/conf/defaults.ini

Then, enable the automatic start of Grafana by systemd:

sudo systemctl daemon-reload &&
sudo systemctl start grafana-server &&
sudo systemctl enable grafana-server.service

Now, check Grafana server status by executing the command sudo systemctl status grafana-server.

Grafana is running now. Open the web by clicking the ‘grafana-web’ URL on the right side of the page under the Lab URLs section. Login with the default user admin with password as admin.

After this Grafana will prompt you to enter a new password. You can enter the password of your choice.

Add a Prometheus data source in Grafana

Before creating our first dashboard in Grafana we have to add data sources. In this section, we will learn how to add Prometheus as a Data source to Grafana.

Go through following steps to add Prometheus as a Data Source.

  • Move the cursor to the cog icon on the left-most side menu.
  • Click on “Data Sources” in the sidebar.
  • Choose “Add data source”.
  • Select “Prometheus” as the data source.
  • Add the Prometheus url i.e. http://localhost:9090 in the url under the HTTP section.
Note: We have already installed Prometheus in the above steps. To know more, check our hands-on lab 'Intro to Prometheus lab'.
  • Click “Save & Test” to test the connection and to save the new data source.

The message "Data source is working" will appear in the dashboard after testing the connection. This means we have successfully added Prometheus as a data source.

Importing Node Exporter Quickstart Dashboard

In this section, we will import a dashboard named Node Exporter Quickstart that includes metrics for CPU, Memory, Disk I/O and Network received and Network Transmitted.

1. Click on add icon in the sidebar and select the import option.

2. Add the ID 13978 which will be used to import this dashboard, and then click on Load 

button. 

3. Under the "Importing dashboard from Grafana.com" section, Click on the Import button.

4. Now you should be able to see an awesome Grafana dashboard displaying the system metrics.

Conclusion

In this hands-on lab,  we learned how to install Grafana and add Prometheus as a data source to it.

What's Next

In the next hands-on lab, we will deep dive into how to write queries in Prometheus and create our own dashboards.

How likely are you going to recommend this lab to your friends or colleagues?

Unlikely
Likely

About the Author

Ayush Bhat

Ayush Bhat

Intern at CloudYuga

Ayush Bhat is passionate about cloud-native technologies. He is Certified Kubernetes Administrator, Linux Foundation Certified System Administrator, Ex294 Red Hat Certified and AWS Community Builder. He is currently doing internship at CloudYuga