Skip to content

Prometheus & Grafana Monitoring Setup Guide. With Ubuntu VPS and Local Mac Air.

Notifications You must be signed in to change notification settings

WaryaWayne/Grafana-Prometheus-Node_Exporter

Repository files navigation

Prometheus & Grafana Monitoring Setup Guide

A comprehensive guide for setting up Prometheus and Node Exporter on a VPS, then connecting to Grafana locally for system monitoring and visualization.


Table of Contents


VPS Setup (Ubuntu)

Initial System Preparation

Update Your System

Keep your system packages up to date for security and compatibility.

sudo apt update && sudo apt upgrade -y

Create Prometheus User

Create a dedicated system user for Prometheus with no login capabilities for security purposes.

sudo useradd --no-create-home --shell /bin/false prometheus

Create Required Directories

Set up the configuration and data storage directories for Prometheus.

sudo mkdir /etc/prometheus /var/lib/prometheus

Set Directory Permissions

Grant the Prometheus user ownership of its directories.

sudo chown prometheus:prometheus /var/lib/prometheus /etc/prometheus

Prometheus Installation

Download Prometheus

Navigate to the temporary directory and download the latest Prometheus release. Check prometheus.io for the most recent version.

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

Extract the Archive

Unpack the downloaded tarball.

tar xvfz prometheus-2.53.0.linux-amd64.tar.gz

Navigate to the Extracted Directory

Move into the newly extracted Prometheus directory.

cd prometheus-2.53.0.linux-amd64

Copy Executables to System Path

Install Prometheus binaries to the system-wide binary directory.

sudo cp prometheus /usr/local/bin/
sudo cp promtool /usr/local/bin/

Set Executable Permissions

Ensure the Prometheus user owns the binaries.

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

Copy Configuration Files

Set up the default configuration and console files.

sudo cp -r consoles /etc/prometheus
sudo cp -r console_libraries /etc/prometheus
sudo cp prometheus.yml /etc/prometheus/

Configure Prometheus

Edit the configuration file to customize your monitoring setup. The default configuration is a good starting point.

sudo nvim /etc/prometheus/prometheus.yml

Example basic configuration:

global:
  scrape_interval: 15s

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

Set Configuration Permissions

Grant the Prometheus user ownership of the configuration file.

sudo chown prometheus:prometheus /etc/prometheus/prometheus.yml

Create Systemd Service

Set up Prometheus as a systemd service for automatic startup and easy management.

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

Service file contents:

[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

Start and Enable Prometheus

Reload systemd, start the service, and enable it to run on boot.

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

Verify Prometheus is Running

Check the service status to ensure everything started correctly.

sudo systemctl status prometheus

If you see errors, check the logs with:

sudo journalctl -u prometheus -f

Access Prometheus Web UI

Prometheus should now be accessible via your web browser at:

http://YOUR_VPS_IP:9090

Node Exporter Installation

Node Exporter collects hardware and OS metrics from your VPS, making them available to Prometheus.

Create Node Exporter User

Create a dedicated system user for Node Exporter.

sudo useradd --no-create-home --shell /bin/false node_exporter

Download Node Exporter

Navigate to the temp directory and download the latest release. Check the Node Exporter releases for the newest version.

cd /tmp
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz

Extract the Archive

Unpack the downloaded tarball.

tar xvfz node_exporter-1.8.2.linux-amd64.tar.gz

Navigate to the Extracted Directory

Move into the Node Exporter directory.

cd node_exporter-1.8.2.linux-amd64

Copy Executable to System Path

Install the Node Exporter binary.

sudo cp node_exporter /usr/local/bin/

Set Executable Permissions

Grant ownership to the node_exporter user.

sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter

Create Systemd Service

Set up Node Exporter as a systemd service.

sudo nvim /etc/systemd/system/node_exporter.service

Service file contents:

[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target

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

[Install]
WantedBy=multi-user.target

Start and Enable Node Exporter

Reload systemd, start the service, and enable it for automatic startup.

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

Verify Node Exporter is Running

Check the service status.

sudo systemctl status node_exporter

Node Exporter should now be running on port 9100.


Connecting Prometheus to Node Exporter

Update Prometheus Configuration

Add Node Exporter as a scrape target so Prometheus can collect system metrics.

sudo nvim /etc/prometheus/prometheus.yml

Add this job to your scrape_configs section:

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

  - job_name: "node"
    static_configs:
      - targets: ["localhost:9100"]
        labels:
          app: "node_exporter"

Restart Prometheus

Apply the configuration changes.

sudo systemctl restart prometheus

Verify the Connection

Visit the Prometheus web UI at http://YOUR_VPS_IP:9090, navigate to Status > Targets, and confirm that both the prometheus and node jobs show as "UP".


Local Setup (macOS)

Grafana Installation

Install Grafana via Homebrew

Use Homebrew to install Grafana on your local machine.

brew install grafana

Create Grafana Alias

The standard brew services start grafana command may not properly find the config file. This alias ensures Grafana starts with the correct paths.

alias grafanaGo="grafana server --config /opt/homebrew/etc/grafana/grafana.ini --homepath /opt/homebrew/opt/grafana/share/grafana web"

Add Alias to Shell Configuration

Make the alias permanent by adding it to your shell configuration file.

echo 'alias grafanaGo="grafana server --config /opt/homebrew/etc/grafana/grafana.ini --homepath /opt/homebrew/opt/grafana/share/grafana web"' >> ~/.zshrc

Reload Shell Configuration

Apply the changes to your current session.

source ~/.zshrc

Start Grafana Server

Launch Grafana using the alias.

grafanaGo

Grafana will start on port 3000 by default. Access it at:

http://localhost:3000

Default credentials:

  • Username: admin
  • Password: admin

You'll be prompted to change the password on first login.

Custom credentials:

  • Set the username and password flags in grafana.ini file

SSH Tunneling

Create SSH Tunnel to VPS

Proxy the Prometheus port from your VPS to your local machine using an SSH tunnel. This allows Grafana to access Prometheus securely without exposing it to the internet.

Replace lightsail2to6x with your VPS hostname or IP address (you can set up an SSH alias in ~/.ssh/config).

ssh -L 9090:localhost:9090 lightsail2to6x

This keeps the tunnel open. Run this in a separate terminal window or use -f to background it.

Alternative with key:

ssh -L 9090:localhost:9090 -i ~/.ssh/your_key user@YOUR_VPS_IP

Grafana Configuration

Access Grafana

Open your browser and navigate to:

http://localhost:3000

Log in with the default credentials (admin/admin) or the password you set.

Add Prometheus Data Source

  1. Navigate to Connections > Data sources in the left sidebar
  2. Click Add data source
  3. Select Prometheus
  4. In the configuration page:
    • Name: Prometheus (or any name you prefer)
    • URL: http://localhost:9090 (the proxied VPS Prometheus)
  5. Scroll down and click Save & Test

You should see a green success message confirming the connection.

Import a Dashboard

Grafana has a rich library of community dashboards. We'll use a popular Node Exporter dashboard.

  1. Navigate to Dashboards in the left sidebar
  2. Click Import
  3. Enter dashboard ID: 1860
  4. Click Load
  5. Configure the import:
    • Name: Node Exporter Full (or customize)
    • Folder: Select or create a folder
    • Prometheus: Select your Prometheus data source
    • UID: Change if necessary (optional)
  6. Click Import

You should now see a comprehensive dashboard displaying all your VPS system metrics including CPU, memory, disk, network, and more.

Alternative Dashboard Import via URL

You can also visit:

http://localhost:3000/dashboard/import

Then follow the same steps above.


Troubleshooting

Prometheus Won't Start

Check the service status and logs:

sudo systemctl status prometheus
sudo journalctl -u prometheus -f

Common issues:

  • Configuration syntax errors in prometheus.yml
  • Port 9090 already in use
  • Permission issues with /var/lib/prometheus

Node Exporter Won't Start

Check the service status and logs:

sudo systemctl status node_exporter
sudo journalctl -u node_exporter -f

Common issues:

  • Port 9100 already in use
  • Binary permissions not set correctly

Grafana Can't Connect to Prometheus

  • Ensure the SSH tunnel is active
  • Verify Prometheus is running on the VPS: http://YOUR_VPS_IP:9090
  • Check that the data source URL is http://localhost:9090 (not the VPS IP)
  • Verify your firewall allows port 9090 on the VPS

Dashboard Shows No Data

  • Confirm Node Exporter is running: sudo systemctl status node_exporter
  • Check Prometheus targets are UP: http://localhost:9090/targets
  • Verify the time range in Grafana matches when data collection started
  • Ensure the correct Prometheus data source is selected in the dashboard

Useful Resources


Happy Monitoring! 📊

About

Prometheus & Grafana Monitoring Setup Guide. With Ubuntu VPS and Local Mac Air.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published