Installing Grafana and Prometheus

From Server rental store
Revision as of 20:01, 12 April 2026 by Admin (talk | contribs) (New server guide)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Installing Grafana and Prometheus

This guide details the installation and initial configuration of Prometheus and Grafana, a powerful open-source monitoring and alerting stack. Prometheus collects and stores metrics, while Grafana visualizes this data through interactive dashboards. This setup is crucial for understanding your server's performance and identifying potential issues before they impact users. For robust performance, consider dedicated servers from PowerVPS that offer full root access for complete control over your monitoring infrastructure.

Prerequisites

Before you begin, ensure you have the following:

  • A Linux server (this guide assumes a Debian/Ubuntu-based system).
  • Root or sudo privileges.
  • Basic understanding of Linux command line.
  • Internet connectivity to download packages.
  • A firewall configured to allow access to Prometheus (default port 9090) and Grafana (default port 3000).

Installing Prometheus

Prometheus is a time-series database and monitoring system.

Download Prometheus

First, navigate to the Prometheus releases page (https://prometheus.io/download/) to find the latest stable version. We'll download the pre-compiled binaries.

wget https://github.com/prometheus/prometheus/releases/download/v2.49.1/prometheus-2.49.1.linux-amd64.tar.gz
tar xzvf prometheus-2.49.1.linux-amd64.tar.gz
cd prometheus-2.49.1.linux-amd64

Move Binaries and Configuration Files

It's good practice to move the Prometheus binaries to a standard location and create dedicated directories for its data and configuration.

sudo mv prometheus promtool /usr/local/bin/
sudo mkdir /etc/prometheus
sudo mkdir /var/lib/prometheus
sudo mv prometheus.yml /etc/prometheus/prometheus.yml

Create Prometheus User

For security, run Prometheus under a dedicated non-root user.

sudo useradd --no-create-home --shell /bin/false prometheus
sudo chown -R prometheus:prometheus /etc/prometheus
sudo chown -R prometheus:prometheus /var/lib/prometheus

Configure Prometheus

Edit the Prometheus configuration file to define scrape targets. For now, we'll configure it to scrape itself.

sudo nano /etc/prometheus/prometheus.yml

Replace the contents with the following:

global:
  scrape_interval: 15s # How frequently to scrape targets by default.

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

Save and exit the file.

Create Systemd Service File

To manage Prometheus as a service, create a systemd unit file.

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

Add the following content:

[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

Save and exit. Now, enable and start the Prometheus service:

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

You should see `active (running)`. Access Prometheus at `http://your_server_ip:9090`.

Installing Grafana

Grafana is a popular open-source platform for monitoring and observability.

Download Grafana

Grafana provides official Debian/Ubuntu packages, which simplifies installation.

sudo apt update
sudo apt install -y apt-transport-https software-properties-common wget
wget -q -O - https://apt.grafana.com/gpg.key | sudo apt-key add -
echo "deb https://apt.grafana.com stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
sudo apt update
sudo apt install grafana

Start and Enable Grafana Service

Start the Grafana service and ensure it starts on boot.

sudo systemctl daemon-reload
sudo systemctl start grafana-server
sudo systemctl enable grafana-server
sudo systemctl status grafana-server

You should see `active (running)`. Access Grafana at `http://your_server_ip:3000`. The default login is `admin` with the password `admin`. You will be prompted to change this password on first login.

Connecting Prometheus to Grafana

Now, let's configure Grafana to use Prometheus as a data source.

Add Prometheus Data Source

1. Log in to your Grafana instance (`http://your_server_ip:3000`). 2. Navigate to the Configuration menu (gear icon on the left sidebar). 3. Click on "Data sources". 4. Click "Add data source". 5. Select "Prometheus". 6. In the "URL" field, enter `http://localhost:9090` (or `http://your_server_ip:9090` if Grafana is on a different server). 7. Leave "Access" as "Server (default)". 8. Click "Save & test". You should see a "Data source is working" message.

Import a Pre-built Dashboard

Grafana has a vast library of community-contributed dashboards. Importing one is a quick way to visualize your data.

1. Go to the "Dashboards" menu (four squares icon). 2. Click "Import". 3. In the "Import via grafana.com" field, enter a dashboard ID. A good starting point is the "Node Exporter Full" dashboard (ID: 1860). 4. Click "Load". 5. On the next screen, select your Prometheus data source from the dropdown. 6. Click "Import".

You should now see a dashboard displaying your server's system metrics!

Troubleshooting

  • **Prometheus not starting:** Check the systemd status with `sudo systemctl status prometheus`. Examine logs with `journalctl -u prometheus`. Common issues include incorrect configuration file paths or permissions.
  • **Grafana not starting:** Check `sudo systemctl status grafana-server` and `journalctl -u grafana-server`. Ensure the Grafana user has the necessary permissions.
  • **Data not appearing in Grafana:**
   *   Verify Prometheus is running and accessible from Grafana (`http://localhost:9090`).
   *   Check Prometheus's "Targets" page (`http://your_server_ip:9090/targets`) to ensure it's successfully scraping your targets.
   *   Ensure your firewall is not blocking communication between Grafana and Prometheus.
  • **Firewall issues:** Ensure ports 9090 (Prometheus) and 3000 (Grafana) are open.

Further Steps

  • **Install Node Exporter:** To get detailed system metrics (CPU, memory, disk, network), install the Node Exporter on your servers and add it as a target in Prometheus. See Installing Node Exporter.
  • **Configure Alerts:** Set up alerting rules in Prometheus and integrate Grafana with alert managers for proactive notifications.
  • **Explore other exporters:** Prometheus supports exporters for various services like databases, web servers, and applications.

This setup provides a solid foundation for monitoring your server infrastructure. For demanding workloads or large-scale deployments, consider the high-performance dedicated servers offered by PowerVPS.