Wednesday, July 17, 2024
HomeHow toHow to Install Grafana and Prometheus on Ubuntu 24.04

How to Install Grafana and Prometheus on Ubuntu 24.04

Grafana is an open-source and multi-platform data visualization platform developed by Grafana Labs. Grafana provides an interactive data visualization web application that includes charts, graphs, and alerts. With Grafana, you can query, visualize, set up alerts, and explore metrics, logs, and traces of TSDB. It is a powerful tool that turns time-series database (TSDB) data into an insightful graph and visualization.

In Grafana, you can add your time-series database data via the ‘Data Source’. Grafana supports multiple data sources such as Prometheus, InfluxDB, PostgreSQL, Loki, Jaeger, Graphite, Google Cloud Monitoring, AWS CloudWatch, Azure Monitor, and many more.

In this tutorial, you’ll learn how to install Grafana, Prometheus, and node_exporter on Ubuntu 24.04 servers. You’ll also install Nginx as a reverse proxy for Grafana, integrate node_exporter and Prometheus, and then add Prometheus as a data source to the Grafana dashboard.

Prerequisites

Before you begin, make sure you have:

  • Two or three Ubuntu 24.04 servers.
  • A non-root user with administrator privileges.

Installing Grafana on Ubuntu

Grafana is a web application for data visualization. To install Grafana, you need to add the Grafana repository. In this example, you’ll also install Nginx as a reverse proxy for Grafana.

Installing dependencies and adding a repository

In this section, you’ll install dependencies including Nginx, then add the Grafana repository.

First, run the command below to install dependencies for Grafana. Type Y to confirm the installation.

sudo apt install gnupg2 apt-transport-https software-properties-common wget nginx

Now add Grafana GPG key and repository with the following command.

wget -q -O - https://packages.grafana.com/gpg.key > grafana.key cat grafana.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/grafana.gpg > /dev/null
echo ‘deb [signed-by=/etc/apt/trusted.gpg.d/grafana.gpg] https://packages.grafana.com/oss/deb stable main’ | sudo tee /etc/apt/sources.list.d/grafana.list

Then, update and refresh your package index with the command below:

sudo apt update

Installing and configuring Grafana

After adding the Grafana repository, you’ll install the Grafana package with the APT package manager. And then configure Grafana to run at localhost with the domain such as hwdomain.io.

To install Grafana, run the following apt command. Type Y to proceed with the installation.

sudo apt install grafana

After the installation is finished, reload the systemd manager with the command below:

sudo systemctl daemon-reload

Now you can start and enable the grafana-server with the following systemctl command. Then, verify to ensure the service is running.

sudo systemctl enable --now grafana-server
sudo systemctl status grafana-server

In the following output, you can see the grafana-server is running and enabled.

With the grafana-server running, you’ll configure it to run at localhost.

Open the Grafana configuration /etc/grafana/grafana.ini with nano editor.

sudo nano /etc/grafana/grafana.ini

Change the default configuration with the following. Make sure to change the domain option with your local domain name for Grafana. For this example, you’ll run Grafana within the domain hwdomain.io.

[server]

# The IP address to bind to, empty will bind to all interfaces
http_addr = localhost

# The http port  to use
http_port = 3000

# The public facing domain name used to access grafana from a browser
domain = hwdomain.io

When finished, save the file and exit the editor.

Now run the command below to restart grafana-server and apply your changes. With this, Grafana should be running on localhost with the default HTTP port 3000.

sudo systemctl restart grafana-server

Setting up Nginx as a reverse proxy

In this section, you’ll create a new Nginx server block as a reverse proxy for the grafana-server that is running on localhost with port 3000.

Create a new Nginx server block configuration /etc/nginx/sites-available/grafana.conf with nano editor.

sudo nano /etc/nginx/sites-available/grafana.conf

Add the following configuration to set up Nginx as a reverse proxy for Grafana. Make sure to change the server_name option with your Grafana domain name.

# this is required to proxy Grafana Live WebSocket connections.
map $http_upgrade $connection_upgrade {
  default upgrade;
  '' close;
}

server {
    listen      80;
    server_name hwdomain.io;

  root /usr/share/nginx/html;
  index index.html index.htm;


  access_log /var/log/nginx/grafana-access.log;
  error_log /var/log/nginx/grafana-error.log;

  location / {
    proxy_set_header Host $http_host;
    proxy_pass http://localhost:3000/;
  }

  # Proxy Grafana Live WebSocket connections.
  location /api/live {
    rewrite  ^/(.*)  /$1 break;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_set_header Host $http_host;
    proxy_pass http://localhost:3000/;
  }
}

Save the file and exit the editor.

Now run the following command to activate the grafana.conf server block and verify your Nginx syntax. If you’ve proper syntax, you’ll see an output test is Successful - syntax is OK.

sudo ln -s /etc/nginx/sites-available/grafana.conf /etc/nginx/sites-enabled/
sudo nginx -t

Next, run the systemctl command below to restart and verify the Nginx web server status.

sudo systemctl restart nginx
sudo systemctl status nginx

You’ll see the Nginx web server is running:

Next, visit your Grafana domain name such as http://hwdomain.io/. If your installation is successful, you will be prompted with the Grafana login page.

Log in with the default user admin and password admin.

Once logged in, enter new password for your Grafana installation and click Submit to confirm.

You’ll see the Grafana dashboard like the following:

Installing and configuring Prometheus

Prometheus is a monitoring and alerting platform. With the Grafana running, you’ll install Prometheus on the server 192.168.5.16.

Installing Prometheus

In this section, you’ll install Prometheus manually by downloading the binary of Prometheus to your system. Before that, you’ll also create a new system user and prometheus.

First, run the command below to add a new user and group prometheus.

sudo groupadd --system prometheus
sudo useradd -s /sbin/nologin --system -g prometheus prometheus

Create a new data directory /var/lib/prometheus and configuration directories /etc/prometheus.

sudo mkdir -p /var/lib/prometheus
for i in rules rules.d files_sd; do sudo mkdir -p /etc/prometheus/${i}; done

Download Prometheus latest version for Linux with the following command.

curl -s https://api.github.com/repos/prometheus/prometheus/releases/latest|grep browser_download_url|grep linux-amd64|cut -d ‘"’ -f 4|wget -qi -

Once downloaded, extract the Prometheus binary file with tar command and go into it.

tar xvf prometheus*.tar.gz
cd prometheus*/

Move binary file prometheus and promtool to the /usr/local/bin directory. And then move configuration directories and the file prometheus.yml to /etc/prometheus directory.

sudo mv prometheus promtool /usr/local/bin/
sudo mv consoles console_libraries prometheus.yml /etc/prometheus/

Lastly, run the command below to change the permission and ownership of the Prometheus configuration and data directory to the user prometheus.

for i in rules rules.d files_sd; do sudo chown -R prometheus:prometheus /etc/prometheus/${i}; done
for i in rules rules.d files_sd; do sudo chmod -R 775 /etc/prometheus/${i}; done
sudo chown -R prometheus:prometheus /var/lib/prometheus/

Configuring Prometheus

After you’ve installed Prometheus, you’ll secure Prometheus using the basic_auth or basic authentication.

Install the apache2-utils package with the following apt command.

sudo apt install apache2-utils -y

Now run the htpasswd command below to generate bcrypt password for user admin. Type your password and repeat, and then copy the generated password.

htpasswd -nB admin

Next, run nano command below to create a new file /etc/prometheus/web.yml.

sudo nano /etc/prometheus/web.yml

Add the following configuration and make sure to change the bcrypt password below.

# basic_auth
basic_auth_users:
  admin: $2y$05$s8U/BrE5JhSO31XKSbtj8u8cPECULs3emEhlDfCB2GW1UefQ9x00C

Save and exit the file when finished.

Now open the default Prometheus configuration /etc/prometheus/prometheus.yml with nano.

sudo nano /etc/prometheus/prometheus.yml

Change the default job_name for prometheus like the following and make sure to change the basic_auth user and password.

scrape_configs:
  # The job name is added as a label `job=` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    # add settings for certificate and authentication
    scheme: http
    basic_auth:
      username: "admin"
      password: "password"

    static_configs:
      # if using a valid certificate, set the same hostname in the certificate
      - targets: ["localhost:9090"]

Save and exit the file.

Lastly, run the command below to change the ownership of files prometheus.yml and web.yml to the user prometheus.

sudo chown prometheus: /etc/prometheus/{prometheus.yml,web.yml}

Running Prometheus as a systemd service

Create a new systemd service file /etc/systemd/system/prometheus.service with the following nano editor.

sudo nano /etc/systemd/system/prometheus.service

Add the following lines to the file. With this, you’ll run Prometheus in the background as a systemd service and secure web console through the web.yml file.

[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
ExecReload=/bin/kill -HUP $MAINPID
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 \
 --web.listen-address=0.0.0.0:9090 \
 --web.config.file=/etc/prometheus/web.yml \

SyslogIdentifier=prometheus
Restart=always

[Install]
WantedBy=multi-user.target

Save the file and exit the editor.

Now run the command below to reload the systemd manager and apply your changes.

sudo systemctl daemon-reload

Then, start and enable prometheus service using the systemctl command below, and make sure that the service is running.

sudo systemctl enable --now prometheus
sudo systemctl status prometheus

In the following output, you can see the prometheus service is running and enabled.

Installing node_exporter

Now that Grafana and Prometheus are running, you’ll download and install node_exporter. The node_exporter allows you to scrape data for basic system monitoring.

Download the node_exporter binary file with the curl command below.

curl -s https://api.github.com/repos/prometheus/node_exporter/releases/latest| grep browser_download_url|grep linux-amd64|cut -d ‘"’ -f 4|wget -qi -

Once downloaded, extract the node_exporter and go into it.

tar -xvf node_exporter*.tar.gz cd node_exporter*/

Now copy the node_exporter binary file to the /usr/local/bin directory.

sudo cp node_exporter /usr/local/bin

After that, run the following command to create a new systemd service file /etc/systemd/system/node_exporter.service for the node_exporter.

sudo tee /etc/systemd/system/node_exporter.service <[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=default.target
EOF

Reload the systemd manager with the following:

sudo systemctl daemon-reload

Now you can start and enable the node_exporter service with the command below. And then, check the node_exporter service status.

sudo systemctl enable --now node_exporter sudo systemctl status node_exporter

You’ll see the node_exporter service is running and will start collecting and populating data. The node_exporter service is running on the default port 9100.

Integrating node_exporter to Prometheus

In this section, you’ll integrate and add node_exporter to the Prometheus.

Open the default Prometheus configuration /etc/prometheus/prometheus.yml.

sudo nano /etc/prometheus/prometheus.yml

Add a new job_name with the name node_exporter and point the target to the target such as 192.168.5.16:9100.

  - job_name: "node_exporter"

    static_configs:
      - targets: ["192.168.5.100:9100"]

Save and exit the file when you’re done.

Now run the command below to restart the prometheus service and apply the changes.

sudo systemctl restart prometheus

Next, access the Prometheus server http://192.168.5.16:9090 through your web browser and you’ll be prompted for basic authentication.

Enter your username and password and click Sign In.

After logged in to the Prometheus dashboard, enter node_memory_Active_bytes in the query and click Execute. You’ll see the graph of current active memory like the following:

Lastly, click Status > Targets and make sure both prometheus and node_exporter targets is active and running like the following:

Integrating Prometheus with Grafana as a data source

Now that Prometheus is ready, you’ll add and integrate Promethues to the Grafana as a data source. Then, you’ll create a new dashboard system for monitoring.

Within the Grafana dashboard, click Connections > Data Sources > Add data source to add a new data source in Grafana.

Select Prometheus as the data source.

Enter the Prometheus URL like http://192.168.5.16:9090/ and enter the username and password for your Prometheus server.

Scroll to the bottom and click Save & test. If successful, you’ll see the message Successfully queried the Prometheus API.

After adding Prometheus as the data source, you’ll create a new Grafana dashboard for the system monitoring.

Click on the menu Dashboard > Create dashboard.

Select Import dashboard to import dashboard.

Visit https://grafana.com/grafana/dashboards/ and find your suitable dashboard. In this example, you’ll load the Grafana dashboard with id 15172.

Click Load to import the dashboard.

Now select Prometheus backend and click Import to confirm.

If everything goes well, your Grafana dashboard should be created. Below is the screenshot from the Grafana dashboard with the Prometheus and node_exporter monitoring:

Conclusion

Congratulations! You’ve completed the installation of Grafana, Prometheus, and node_exporter on Ubuntu 24.04 servers. You’ve Grafana running with Nginx as a reverse proxy, secured Prometheus with password authentication, integrated node_exporter with Prometheus, added Prometheus as a data source to the Grafana, and imported the Grafana dashboard.


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Most Popular