Pull down to refresh

🕵️‍♂️A Step-by-Step Guide to Installing Prometheus and Grafana 🔍

date
Apr 18, 2024
slug
guide-to-installing-prometheus-and-grafana
author
status
Public
tags
Guide
Prometheus
Grafana
DevOps
summary
This article will guide you step by step on how to install prometheus and Grafana
type
Post
thumbnail
Prometheus-Grafana-banner.png
category
📢 DevOps Blogs
updatedAt
Apr 18, 2024 10:19 AM
Monitoring is vital for maintaining system health, diagnosing issues, and optimizing performance. Prometheus and Grafana represent a perfect duo for that purpose. Prometheus scrapes and stores metrics, while Grafana provides a user-friendly interface for visualizing this data and creating customized dashboards.

This article will guide you step by step on how to install prometheus and Grafana.

 

1- Installing Prometheus

Prometheus is a free, open-source software application used for monitoring and alerting, enabling users to collect and analyze metrics from various systems, services, and applications.

1.1 Download prometheus

Visit the Prometheus website and download the latest version suitable for your operating system. Prometheus is available for various platforms, including Linux, macOS, and Windows.
For Linux user, use curl and output the file downloaded to prometheus.tar.gz
curl -L -o /tmp/prometheus.tar.gz https://github.com/prometheus/prometheus/releases/download/v2.45.4/prometheus-2.45.4.linux-amd64.tar.gz

1.2 Extract the Archive:

Once the download is complete, extract the contents of the Prometheus archive and add prometehus & promtool binaries to your /usr/bin.
tar xvf /tmp/prometheus.tar.gz cd prometheus-2.45.4.linux-amd64 sudo cp prometheus promtool /usr/bin/

1.3 Configure Prometheus:

Start by creating a new user named prometheus to isolate the process.
sudo useradd --no-create-home --shell /bin/false prometheus
Next create a folder to store prometheus configuration files and data.
sudo mkdir /etc/prometheus sudo mkdir /var/lib/prometheus sudo chown prometheus:prometheus /etc/prometheus sudo chown prometheus:prometheus /var/lib/prometheus
Then copy the default prometheus configuration file
sudo cp -r prometheus.yml consoles/ console_libraries/ /etc/prometheus/
If needed, open this file in a text editor and customize it according to your monitoring requirements. Here, you can specify the targets to scrape, configure alerting rules, and define storage configurations.

1.4 Create prometheus service

To run Prometheus as a service on Linux, create a systemd file:
sudo vim /etc/systemd/system/prometheus.service
and past the content of the service (adapt the path to match your configuration)
[Unit] Description=Prometheus Documentation=https://prometheus.io/docs/introduction/overview/ Wants=network-online.target After=network-online.target [Service] Type=simple User=prometheus Group=prometheus ExecStart=/usr/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 \ --web.listen-address=0.0.0.0:9090 ExecReload=/bin/kill -HUP $MAINPID SyslogIdentifier=prometheus Restart=always [Install] WantedBy=multi-user.target
Now we can start our prometheus service:
sudo systemctl daemon-reload sudo systemctl start prometheus sudo systemctl enable prometheus

5 Access Prometheus Web UI:

Once Prometheus is running, you can access its web-based user interface by opening a web browser and navigating to http://localhost:9090.
notion image

2. Grafana

Grafana is an open-source analytics and visualization platform that allows users to create, explore, and share dashboards and data insights from various sources.

2.1 Installing Grafana

Download Grafana: Visit the Grafana website and download the latest version suitable for your operating system. Grafana supports various platforms, including Linux, macOS, and Windows.
Start by installing the prerequisite packages
sudo apt-get install -y apt-transport-https software-properties-common wget
Import GPG key
sudo mkdir -p /etc/apt/keyrings/ wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg > /dev/null
Add Grafana repository
echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
Install Grafana
sudo apt-get update sudo apt-get install grafana-server

2.2 Access Grafana Web UI:

Once Grafana is running, you can access its web-based user interface by opening a web browser and navigating to http://localhost:3000.
notion image
The default password for the admin user in Grafana is “admin”. You can change the password in the settings

2.3. Add prometheus as Data source

A Grafana data source is essentially a data source that Grafana connects to in order to retrieve data for visualization
To add the Prometheus data source, go to Home > Connections > Data Sources > Under Connections, click Add new connection.
notion image
Select Prometheus data source. Under the connection section , entre the url for your prometheus instance.
notion image
Save your changes.

3. Install node exporter

Node Exporter is a Prometheus exporter that collects system-level metrics from Linux and Unix systems. It provides detailed information about hardware, operating system, and other system-level metrics such as CPU usage, memory usage, disk usage, network statistics, and more. These metrics are then exposed to Prometheus for monitoring and alerting purposes.
To install Node Exporter on a Linux or Unix system, you can follow these general steps:

3.1 Download node exporter

You can download the latest version of Node Exporter from the Prometheus GitHub releases page:
curl -L -o /tmp/node_exporter.tar.gz https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz

3.2 Extract the Archive:

Once the download is complete, extract the contents of the node_exporter archive and add node_exporter binary to your /usr/bin.
tar xvf /tmp/node_exporter.tar.gz cd node_exporter-1.7.0.linux-amd64 sudo mv node_exporter /usr/bin

3.3 Create Node exporter service

To run Node exporter as a service on Linux, create a systemd file:
sudo vim /etc/systemd/system/node_exporter.service
and past the content of the service (adapt the path to match your configuration)
[Unit] Description=Node Exporter After=network.target [Service] ExecStart=/usr/bin/node_exporter [Install] WantedBy=multi-user.target
Now we can start our node_exporter service:
sudo systemctl daemon-reload sudo systemctl start node_exporter sudo systemctl enable node_exporter

3.4 Verify Installation

Once Node Exporter is running, you can verify that it’s collecting metrics by accessing its metrics endpoint in a web browser. By default, it listens on port 9100. So, you can visit http://your-server-ip:9100/metrics to see the exported metrics.

4. Configure Prometheus to scrape node exporter metrics

Finally, you need to configure Prometheus to scrape metrics from Node Exporter. This involves adding Node Exporter as a target in your Prometheus configuration file.
Open your prometheus configuration file:
sudo vim /etc/prometheus/prometheus.yml
Add the below scrape configuration:
scrape_configs: - job_name: 'node' static_configs: - targets: ['your-server-ip:9100']
Reload Prometheus configuration
sudo systemctl reload prometheus
Once you complete these steps, Prometheus should start scraping metrics from Node Exporter, and you can use Grafana to visualize and monitor these metrics through Prometheus.

5. Create node exporter dashboard

Grafana Community offers a vast choice of Grafana dashboard, for our case we will import this complete dashboard https://grafana.com/grafana/dashboards/1860-node-exporter-full/
Open Grafana in your web browser and go to the Dashboards section.
In the Dashboards section, click on the “Import” button.
notion image
paste the dashboard ID: 1860 , and click on the “Load” button.
notion image
You may need to configure some settings like the data source if it’s not already configured in your Grafana instance.
Finally, click on the “Import” button to import the dashboard into your Grafana instance.
notion image
 
Thank you for reading my blog …:)
© Copyrights: ProDevOpsGuy

Support 🫶

  • Help spread the word about ProDevOpsGuy by sharing it on social media and recommending it to your friends. 🗣️

Thank you for reading this long article.
Feedback on typos and content is always welcome.

#DevOps#Grafana#Guide#Prometheus