Introduction
Monitoring server performance is essential to ensure system reliability, identify bottlenecks, and maintain high availability. Prometheus and Grafana are powerful open-source tools that work seamlessly together to collect, visualize, and analyze server metrics in real-time. In this guide, you’ll learn how to set up Prometheus for data collection and Grafana for creating insightful dashboards to monitor your server’s performance.
Prerequisites
Before getting started, ensure you have:
- An Ubuntu 22.04 VPS
- Root or sudo privileges
- Basic knowledge of Linux commands
Step 1: Update Your System
Ensure your server is up-to-date to avoid compatibility issues:
sudo apt update && sudo apt upgrade -y
Step 2: Install Prometheus
Prometheus is a time-series database designed for monitoring and alerting. Follow these steps to install it:
1. Create a Prometheus User:
sudo useradd –no-create-home –shell /bin/false prometheus
2. Create Directories for Prometheus Data:
sudo mkdir /etc/prometheus
sudo mkdir /var/lib/prometheus
3. Download Prometheus:
cd /tmp
wget https://github.com/prometheus/prometheus/releases/latest/download/prometheus-2.43.0.linux-amd64.tar.gz
4. Extract the Archive:
tar -xvf prometheus-2.43.0.linux-amd64.tar.gz
5. Move Binaries:
sudo mv prometheus-2.43.0.linux-amd64/prometheus /usr/local/bin/
sudo mv prometheus-2.43.0.linux-amd64/promtool /usr/local/bin/
6. Move Configuration Files:
sudo mv prometheus-2.43.0.linux-amd64/consoles /etc/prometheus
sudo mv prometheus-2.43.0.linux-amd64/console_libraries /etc/prometheus
sudo mv prometheus-2.43.0.linux-amd64/prometheus.yml /etc/prometheus
Step 3: Configure Prometheus
Edit the Prometheus configuration file:
sudo nano /etc/prometheus/prometheus.yml
Example configuration:
global:
scrape_interval: 15sscrape_configs:
– job_name: ‘prometheus’
static_configs:
– targets: [‘localhost:9090’]
Step 4: Set Up Prometheus as a Service
Create a systemd service file:
sudo nano /etc/systemd/system/prometheus.service
Add the following:
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target[Service]
User=prometheus
ExecStart=/usr/local/bin/prometheus \
–config.file=/etc/prometheus/prometheus.yml \
–storage.tsdb.path=/var/lib/prometheus/[Install]
WantedBy=multi-user.target
Reload systemd, start, and enable Prometheus:
sudo systemctl daemon-reload
sudo systemctl start prometheus
sudo systemctl enable prometheus
Verify Prometheus:
Check if Prometheus is running:
sudo systemctl status prometheus
Access Prometheus at:
http://your_server_ip:9090
Step 5: Install Grafana
Grafana is used to visualize the data collected by Prometheus.
1. Add Grafana Repository:
sudo apt install -y software-properties-common
sudo add-apt-repository “deb https://packages.grafana.com/oss/deb stable main”
2. Install Grafana:
sudo apt update && sudo apt install grafana -y
3. Start and Enable Grafana:
sudo systemctl start grafana-server
sudo systemctl enable grafana-server
Access Grafana:
Visit Grafana at:
http://your_server_ip:3000
Default login credentials:
- Username: admin
- Password: admin (change on first login)
Step 6: Configure Prometheus as a Data Source in Grafana
-
- Log into Grafana.
- Go to Settings → Data Sources.
- Click Add data source and select Prometheus.
- Set the URL to:
http://localhost:9090
- Click Save & Test.
Step 7: Create Dashboards in Grafana
To create a dashboard:
-
- Click the + icon → Dashboard.
- Click Add new panel.
- In the query field, enter:
node_cpu_seconds_total
- Adjust the visualization settings as needed.
- Click Apply to save the panel.
Step 8: Monitor Key Metrics
Common Prometheus metrics you can track:
- CPU Usage:
rate(node_cpu_seconds_total[1m])
- Memory Usage:
node_memory_MemAvailable_bytes
- Disk Usage:
node_filesystem_avail_bytes
- Network Traffic:
rate(node_network_receive_bytes_total[5m])
Step 9: Enable Alerts in Grafana
To set up alerts:
-
- Go to your dashboard.
- Click the panel title → Edit.
- Go to the Alert tab and click Create Alert.
- Set conditions like:
WHEN avg() OF query(A, 5m, now) IS ABOVE 80
- Save the alert configuration.
Step 10: Securing Prometheus and Grafana
-
- Enable authentication for Prometheus by configuring
prometheus.yml
. - Use HTTPS for Grafana by editing
/etc/grafana/grafana.ini
and configuring SSL. - Restrict access to monitoring tools using a firewall:
- Enable authentication for Prometheus by configuring
sudo ufw allow 9090/tcp
sudo ufw allow 3000/tcp
Conclusion
Congratulations! You’ve successfully set up Prometheus and Grafana to monitor your server’s performance. This powerful combination provides real-time insights into system metrics, helping you detect and respond to issues promptly. Regular monitoring ensures optimal performance, improves resource management, and enhances the security of your infrastructure.