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
Create a network for Prometheus and Grafana to communicate:
docker network create monitoring
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
Next, create the Prometheus configuration file (prometheus.yml):
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'node-exporter'
static_configs:
- targets: ['node-exporter:9100']
2: Launch the Monitoring Stack
Run the Docker Compose stack:
docker-compose up -d
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.
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;
}
}
Save and close the file, then test the Nginx configuration:
sudo nginx -t
Reload Nginx to apply the changes:
sudo systemctl reload nginx
4: Access Grafana
- Open your browser and go to http://your-domain.com/grafana/.
- Log in with the default credentials (user: admin, password: admin).
- Make sure to change the password when prompted.
Add Prometheus as a Data Source
- Go to Connections > Data Sources.
- Click on "Add data source" and select Prometheus.
- 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.
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)