Install Prometheus and Grafana on Ubuntu 20.04

Monitoring your infrastructure is essential for performance, reliability, and capacity planning.

In this guide, we’ll set up Prometheus (for metrics collection) and Grafana (for visualization) on Ubuntu 20.04, then connect Rocky Linux 8 and Ubuntu clients using Node Exporter.




๐Ÿงฉ What is Prometheus?

Prometheus is an open-source monitoring tool developed by SoundCloud, now part of the CNCF (Cloud Native Computing Foundation).
It collects metrics from systems and applications and stores them in a time-series database, allowing you to create alerts and analyze performance.


๐Ÿ“Š What is Grafana?

Grafana is an open-source data visualization platform.
It connects to Prometheus (and many other sources) to visualize data in interactive dashboards and graphs.


⚙️ Step 1: Update Ubuntu 20.04 Server

sudo apt update && sudo apt upgrade -y

๐Ÿง  Step 2: Install Prometheus on Ubuntu 20.04

1️⃣ Create Prometheus user and directories

sudo useradd --no-create-home --shell /bin/false prometheus
sudo mkdir /etc/prometheus /var/lib/prometheus

2️⃣ Download Prometheus

cd /tmp
wget https://github.com/prometheus/prometheus/releases/download/v2.53.0/prometheus-2.53.0.linux-amd64.tar.gz
tar xvf prometheus-2.53.0.linux-amd64.tar.gz
cd prometheus-2.53.0.linux-amd64

3️⃣ Move binaries and set permissions

sudo mv prometheus /usr/local/bin/
sudo mv promtool /usr/local/bin/
sudo mv consoles /etc/prometheus/
sudo mv console_libraries /etc/prometheus/
sudo mv prometheus.yml /etc/prometheus/
sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus
sudo chown prometheus:prometheus /usr/local/bin/prometheus /usr/local/bin/promtool

๐Ÿงพ Step 3: Create Prometheus Systemd Service

sudo tee /etc/systemd/system/prometheus.service > /dev/null <<EOF
[Unit]
Description=Prometheus Monitoring
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
EOF

Then enable and start Prometheus:

sudo systemctl daemon-reload
sudo systemctl enable prometheus
sudo systemctl start prometheus
sudo systemctl status prometheus

Access it in your browser:
๐Ÿ‘‰ http://:9090


๐Ÿ“ˆ Step 4: Install Grafana on Ubuntu 20.04

1️⃣ Add Grafana APT repository

sudo apt install -y apt-transport-https software-properties-common curl gpg
sudo mkdir -p /usr/share/keyrings/
curl -fsSL https://packages.grafana.com/gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/grafana.gpg
echo "deb [signed-by=/usr/share/keyrings/grafana.gpg] https://packages.grafana.com/oss/deb stable main" | \
sudo tee /etc/apt/sources.list.d/grafana.list

2️⃣ Install Grafana

sudo apt update
sudo apt install grafana -y

3️⃣ Enable and Start Grafana

sudo systemctl enable grafana-server
sudo systemctl start grafana-server

Open Grafana in a browser:
๐Ÿ‘‰ http://:3000
(Default credentials: admin / admin)


๐Ÿ”— Step 5: Connect Grafana to Prometheus

  1. Login to Grafana → http://<server-ip>:3000

  2. Go to Connections → Data Sources → Add Data Source

  3. Choose Prometheus

  4. In URL → http://localhost:9090

  5. Click Save & Test


๐Ÿ–ฅ️ Step 6: Install Node Exporter on Clients

➤ On Rocky Linux 8 & Ubuntu Clients

Run these commands on each client system (both Rocky 8 and Ubuntu):

# Download Node Exporter
cd /tmp
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
tar xvf node_exporter-1.8.2.linux-amd64.tar.gz
sudo mv node_exporter-1.8.2.linux-amd64/node_exporter /usr/local/bin/

# Create a user
sudo useradd --no-create-home --shell /bin/false node_exporter

# Create systemd service
sudo tee /etc/systemd/system/node_exporter.service > /dev/null <<EOF
[Unit]
Description=Prometheus Node Exporter
After=network.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable node_exporter
sudo systemctl start node_exporter

Default metrics endpoint →
๐Ÿ‘‰ http://:9100/metrics


๐Ÿ”ง Step 7: Add Clients to Prometheus Server

Edit Prometheus config on your main Ubuntu 20.04 server:

sudo vi /etc/prometheus/prometheus.yml

Add client targets at the bottom:

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]

  - job_name: "node_exporter"
    static_configs:
      - targets: ["192.168.1.10:9100", "192.168.1.11:9100", "192.168.1.12:9100"]

Replace the IPs with your Rocky 8 and Ubuntu client IPs.

Then restart Prometheus:

sudo systemctl restart prometheus

✅ Step 8: Verify Monitoring Setup

  • Prometheus Targets:
    ๐Ÿ‘‰ http://<server-ip>:9090/targets

  • Grafana Dashboards:
    ๐Ÿ‘‰ http://<server-ip>:3000

You should now see metrics from all your Ubuntu and Rocky Linux clients visualized beautifully in Grafana.


๐ŸŽฏ Summary

Component Description Default Port
Prometheus Metrics collection & storage 9090
Grafana Data visualization 3000
Node Exporter System metrics on clients 9100

๐Ÿง  Optional: Prebuilt Dashboards

In Grafana:

  • Go to Create → Import Dashboard

  • Use official Node Exporter dashboard ID: 1860


๐Ÿ Conclusion

You’ve successfully:

  • Installed Prometheus and Grafana on Ubuntu 20.04

  • Added Rocky 8 and Ubuntu client nodes

  • Configured Node Exporter for monitoring

  • Set up Grafana dashboards

Your monitoring stack is now production-ready! ๐ŸŽ‰


Would you like me to format this blog with Markdown headings + code syntax highlighting (ready for Medium or GitHub Pages publishing)?

No comments:

Post a Comment