DEV Community

Cover image for Install and Setup Grafana & Prometheus on Ubuntu 20.04 | 22.04/EC2
Shubham Patil
Shubham Patil

Posted on

Install and Setup Grafana & Prometheus on Ubuntu 20.04 | 22.04/EC2

What is Prometheus ?
Prometheus is a open source Linux Server Monitoring tool mainly used for metrics monitoring, event monitoring, alert management, etc.
Prometheus has changed the way of monitoring systems and that is why it has become the Top-Level project of Cloud Native Computing Foundation (CNCF).
Prometheus uses a powerful query language i.e. “PromQL”.
In Prometheus tabs are on and handles hundreds of services and microservices.
Prometheus use multiple modes used for graphing and dashboarding support.
Prometheus Components

  1. Prometheus Server Prometheus server is a first component of Prometheus architecture. Prometheus server is a core of Prometheus architecture which is divided into several parts like Storage, PromQL, HTTP server, etc. In Prometheus server data is scraped from the target nodes and then stored int the database. 1.a. Storage Storage in Prometheus server has a local on disk storge. Prometheus has many interfaces that allow integrating with remote storage systems. 1.b. PromQL

Prometheus uses its own query language i.e. PromQL which is very powerful querying language.
PromQL allows the user to select and aggregate the data.

  1. Service Discovery Next and very important component of Prometheus Server is the Service Discovery. With the help of Service discovery the services are identified which are need to scraped. To Pull metrics, identification of services and finding the targets are compulsory needed. Through Service discovery we monitor the entities and can also locate its targets.
  2. Scrape Target Once the services are identified and the targets are ready then we can pull metrics from it and can scrape the target. We can export the data of end point using node exporters. Once the metrics or other data is pulled, Prometheus stores it in a local storage.
  3. Alert Manager Alert Manager handles the alerts which may occurs during the session. Alert manager handles all the alerts which are sent by Prometheus server. Alert manager is one of the very useful component of Prometheus tool. If in case any big error or any issue occurs, alert manager manage those alerts and contact with human via E-mail, Text Messages, On-call, or any other chat application service.
  4. User Interface User interface is also a important component as it builds a bridge between the user and the system. In Prometheus, user interface are note that much user friendly and can be used till graph queries. For good exclusive dashboards Prometheus works together with Grafana (visualization tool). Using Grafana over Prometheus to visualize properly we can use custom dashboards. Grafana dashboards displays via pie charts, line charts, tables, good data graphs of CPU usage, RAM utilization, network load, etc with indicators. Grafana supports and run with Prometheus by querying language i.e. PromQL. To fetch data from Prometheus and to display the results on Grafana dashboards PromQL is used. What is Grafana ? Grafana is a free and open source visualization tool mostly used with Prometheus to which monitor metrics. Grafana provides various dashboards, charts, graphs, alerts for the particular data source. Grafana allows us to query, visualize, explore metrics and set alerts for the data source which can be a system, server, nodes, cluster, etc. We can also create our own dynamic dashboard for visualization and monitoring. We can save the dashboard and can even share with our team members which is one of the main advantage of Grafana. What is Node Exporter ? Node exporter is one of the Prometheus exporters which is used to expose servers or system OS metrics. With the help of Node exporter we can expose various resources of the system like RAM, CPU utilization, Memory Utilization, disk space. Node exporter runs as a system service which gathers the metrics of your system and that gathered metrics is displayed with the help of Grafana visualization tool. Prerequisites Ubuntu with 22.04 Version Root user account with sudo privilege. Prometheus system user and group. Sufficient storage on your system and good internet connectivity. Ports Required- 9090 (Prometheus), 3000 (Grafana), 9100 (Node Exporter) We will update the system repository index by using the following command.

Step #1:Creating Prometheus System Users and Directory
Create a system user for Prometheus

sudo useradd --no-create-home --shell /bin/false prometheus
Enter fullscreen mode Exit fullscreen mode

Output

Create the directories in which we will be storing our configuration files and libraries:

sudo mkdir /etc/prometheus
sudo mkdir /var/lib/prometheus
Enter fullscreen mode Exit fullscreen mode

Output

Set the ownership of the /var/lib/prometheus directory

sudo chown prometheus:prometheus /var/lib/prometheus
Enter fullscreen mode Exit fullscreen mode

Output

Step #2:Download Prometheus Binary File
Now we will download the latest version of Prometheus. We can copy the download link as per our Operating System from Prometheus download page

Using below command we can download Prometheus, here we are downloading Prometheus 2.46 version, you use above link to download specific version.

You need to inside /tmp :

cd /tmp/
Enter fullscreen mode Exit fullscreen mode

Download the Prometheus setup using wget

wget https://github.com/prometheus/prometheus/releases/download/v2.46.0/prometheus-2.46.0.linux-amd64.tar.gz
Enter fullscreen mode Exit fullscreen mode

Output

Extract the files using tar

tar -xvf prometheus-2.46.0.linux-amd64.tar.gz
Enter fullscreen mode Exit fullscreen mode

Move the configuration file and set the owner to the prometheus user

cd prometheus-2.46.0.linux-amd64

sudo mv console* /etc/prometheus

sudo mv prometheus.yml /etc/prometheus

sudo chown -R prometheus:prometheus /etc/prometheus
Enter fullscreen mode Exit fullscreen mode

Output

Move the binaries and set the owner

sudo mv prometheus /usr/local/bin/

sudo chown prometheus:prometheus /usr/local/bin/prometheus

Enter fullscreen mode Exit fullscreen mode

Output

Step #3:Prometheus configuration file
We have already copied /opt/prometheus-2.26.0.linux-amd64/prometheus.yml file /etc/prometheus directory, verify if it present and should look like below and modify it as per your requirement.


sudo nano /etc/prometheus/prometheus.yml
Enter fullscreen mode Exit fullscreen mode

Step #4:Creating Prometheus Systemd file
Create the service file


[Unit]

Description=Prometheus

Wants=network-online.target

After=network-online.target

[Service]

User=prometheus

Group=prometheus

Type=simple

ExecStart=/usr/local/bin/prometheus \

— config.file /etc/prometheus/prometheus.yml \

— storage.tsdb.path /var/lib/prometheus/ \

— web.console.templates=/etc/prometheus/consoles \

— web.console.libraries=/etc/prometheus/console_libraries

[Install]

WantedBy=multi-user.target

Enter fullscreen mode Exit fullscreen mode

Output

Reload systemd

sudo systemctl daemon-reload
Enter fullscreen mode Exit fullscreen mode

Start, Enable, Status Prometheus service

sudo systemctl start prometheus

sudo systemctl enable prometheus

sudo systemctl status prometheus
Enter fullscreen mode Exit fullscreen mode

Launch the Prometheus server by executing the following command:


./prometheus –config.file=prometheus.yml (or) ./prometheus
Enter fullscreen mode Exit fullscreen mode

Verify Prometheus Installation:

Open your web browser and access http://server-ip-address:9090. If Prometheus is running correctly, you will see the Prometheus web interface.

Image description

Image description

Image description

Image description

Steps to Install and set up the Grafana on Ubuntu 20.04 and 22.04/EC2

Grafana
What is Grafana?
Grafana is an open-source platform for data visualization, monitoring, and analysis. It allows you to create and display interactive, customizable dashboards with real-time monitoring and analytics capabilities. Grafana supports a wide range of data sources, including time-series databases and relational databases.
With Grafana, you can easily create visualizations and alerts based on your data, as well as collaborate with other team members through sharing and annotations. It also provides advanced features like plugins, annotations, and panels, which allow you to extend the platform’s capabilities and create custom visualizations and integrations.
Grafana is widely used in industries such as IT, finance, healthcare, and more, for monitoring and analyzing various metrics, logs, and sensor data. It is known for its user-friendly interface, ease of use, and powerful features, making it a popular choice for data visualization and monitoring.
Installation of Grafana
Update your Ubuntu system using the following command:

sudo apt-get update
Enter fullscreen mode Exit fullscreen mode

Getting packages on Ubuntu distributions
Add the Grafana repository to your Ubuntu installation:

sudo apt-get install -y apt-transport-https
sudo apt-get install -y software-properties-common wget
sudo wget -q -O /usr/share/keyrings/grafana.key https://apt.grafana.com/gpg.key
echo "deb [signed-by=/usr/share/keyrings/grafana.key] https://apt.grafana.com stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
Enter fullscreen mode Exit fullscreen mode

Install Grafana as a service
Now that all the packages are available, it is time for you to install them.
Update your package list using the following command:

sudo apt-get update
Enter fullscreen mode Exit fullscreen mode

Install the Grafana package using the following command:

sudo apt-get install grafana
Enter fullscreen mode Exit fullscreen mode

Check Grafana version
You can use the Grafana version command to check the currently installed version.

grafana-server -v
Enter fullscreen mode Exit fullscreen mode

Start the Grafana service using the following command:

sudo systemctl start grafana-server
Enter fullscreen mode Exit fullscreen mode

Enable the Grafana service to start on boot:
To start it and make sure that the service is always available every time the machine is restarted, type the command:

sudo systemctl enable grafana-server
Enter fullscreen mode Exit fullscreen mode

Verify your Grafana installation
Check the status of the Grafana service to make sure it is running.
To verify it, run the following command:

sudo systemctl status grafana-server
Enter fullscreen mode Exit fullscreen mode

Grafana should run automatically, but if this is not the case, make sure to start it.

sudo service grafana-server start
Enter fullscreen mode Exit fullscreen mode

In the future to stop and restart the Grafana, the commands are:
Restart

sudo service grafana-server restart
Enter fullscreen mode Exit fullscreen mode

Stop

sudo service grafana-server stop
Enter fullscreen mode Exit fullscreen mode

Allow Grafana TCP port 3000 in the Firewall
Grafana's default HTTP port is 3000, you’ll need to allow access to this port on the firewall.
If your firewall is UFW type the following commands:

sudo ufw allow 3000/tcp
Enter fullscreen mode Exit fullscreen mode

Image description

That’s it! You have now installed Grafana on Ubuntu. You can now start using it to create dashboards and visualize your data.
Set up Grafana

  1. Access the Grafana web UI by visiting http://localhost:3000 in your web browser.

Image description

  1. Login to Grafana web UI Image description

Dashboard Grafana

Image description

Setup for Data Source

Image description

Image description

Image description

Image description

Image description

Image description

After Setup Final Dashboard Output:=

Image description

Top comments (0)