DEV Community

Cover image for Monitoring Your Server with Prometheus, Node Exporter, and Grafana Using Docker
Jobin Biju
Jobin Biju

Posted on

Monitoring Your Server with Prometheus, Node Exporter, and Grafana Using Docker

In this tutorial, you’ll learn how to set up a comprehensive monitoring stack for your server using Prometheus, Node Exporter, and Grafana. We’ll use Docker to simplify the deployment and Nginx as a reverse proxy. Let’s dive in!

Prerequisites

  • An Ubuntu server (we're using a Lightsail instance).
  • Docker installed.
  • Nginx installed and configured as a reverse proxy for your application.

1: Setting Up Docker Compose

First, create a directory for your monitoring setup:



mkdir -p ~/monitoring
cd ~/monitoring


Enter fullscreen mode Exit fullscreen mode

Create a network for Prometheus and Grafana to communicate:



docker network create monitoring


Enter fullscreen mode Exit fullscreen mode

Create a Docker Compose file (docker-compose.yml) with the following content:



version: '3'

services:
  node-exporter:
    image: prom/node-exporter
    container_name: node-exporter
    ports:
      - 9100:9100
    restart: unless-stopped
    networks:
      - monitoring

  prometheus:
    image: prom/prometheus
    container_name: prometheus
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    ports:
      - 9090:9090
    restart: unless-stopped
    networks:
      - monitoring

  grafana:
    image: grafana/grafana
    container_name: grafana
    ports:
      - 3000:3000
    environment:
      - GF_SERVER_ROOT_URL=%(protocol)s://%(domain)s:%(http_port)s/grafana/
      - GF_SERVER_SERVE_FROM_SUB_PATH=false
    restart: unless-stopped
    networks:
      - monitoring

networks:
  monitoring:
    driver: bridge


Enter fullscreen mode Exit fullscreen mode

Next, create the Prometheus configuration file (prometheus.yml):



global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'node-exporter'
    static_configs:
      - targets: ['node-exporter:9100']


Enter fullscreen mode Exit fullscreen mode

2: Launch the Monitoring Stack

Run the Docker Compose stack:



docker-compose up -d


Enter fullscreen mode Exit fullscreen mode

3: Configure Nginx as a Reverse Proxy

Open your Nginx configuration file:



sudo nano /etc/nginx/sites-available/default #use your domain instead of default if required.


Enter fullscreen mode Exit fullscreen mode

Add the following server blocks for Prometheus and Grafana:



server {
    listen 80;

    server_name your-domain.com; # Replace with your domain

    location / {
        proxy_pass http://localhost:8080; # Your application
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /grafana/ { #subpath to use grafana
        proxy_pass http://localhost:3000/; 
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}


Enter fullscreen mode Exit fullscreen mode

Save and close the file, then test the Nginx configuration:



sudo nginx -t


Enter fullscreen mode Exit fullscreen mode

Reload Nginx to apply the changes:



sudo systemctl reload nginx

Enter fullscreen mode Exit fullscreen mode




4: Access Grafana

  1. Open your browser and go to http://your-domain.com/grafana/.
  2. Log in with the default credentials (user: admin, password: admin).
  3. Make sure to change the password when prompted.

Add Prometheus as a Data Source

  1. Go to Connections > Data Sources.
  2. Click on "Add data source" and select Prometheus.
  3. Set the URL to http://prometheus:9090 and click "Save & Test".

Create Dashboards

You can now import pre-built dashboards or create your own. A good starting point is the Node Exporter Full dashboard.

Node Exporter Dashboard

Conclusion

Congratulations! You have successfully set up a powerful monitoring stack with Prometheus, Node Exporter, and Grafana using Docker, integrated with Nginx as a reverse proxy. Now you can monitor your server's performance and health in real-time. Happy monitoring!

Top comments (0)