How to Set Up Prometheus and Grafana for Monitoring on Ubuntu 22.04 LTS

In today’s fast-paced DevOps landscape, ensuring that your applications are running smoothly and efficiently is essential. Monitoring systems provide visibility into the health and performance of your applications, allowing you to proactively address issues before they impact users. Prometheus and Grafana are two powerful tools that, when combined, offer comprehensive monitoring and visualization capabilities. Prometheus excels at collecting and storing metrics, while Grafana allows you to create interactive and insightful dashboards. This tutorial will guide you through setting up Prometheus and Grafana on Ubuntu 22.04 LTS, enabling you to monitor your applications effectively.


Prerequisites

Before starting, ensure that you have:

  • Administrative access to the Ubuntu server.
  • Basic knowledge of Linux commands and file system structure.
  • Pre-installed tools, such as sudo and apt, as well as optional tools like Docker or Git for extended use cases.

Technical Implementation

Step 1: Install Prometheus

Prometheus is a powerful open-source monitoring and alerting toolkit. To install Prometheus, start by updating your package list and installing the necessary packages:

# Update the package list and install Prometheus
sudo apt update && sudo apt install prometheus -y

Once installed, start the Prometheus service:

# Start the Prometheus service
sudo systemctl start prometheus

# Enable Prometheus to start at boot
sudo systemctl enable prometheus

Step 2: Configure Prometheus

Configure Prometheus to scrape metrics from your applications by editing its main configuration file:

# Open the Prometheus configuration file for editing
sudo nano /etc/prometheus/prometheus.yml

Add the following content:

global:
  scrape_interval: 10s

scrape_configs:
  - job_name: 'node'
    scrape_interval: 10s
    static_configs:
      - targets: ['localhost:9100']

Explanation:

  • scrape_interval: The frequency at which Prometheus collects data from the target.
  • targets: Specifies the endpoint from which Prometheus will scrape metrics. In this example, it scrapes from localhost:9100.

Save and exit the editor (CTRL+X, then Y, and Enter).

Restart Prometheus to apply the configuration:

sudo systemctl restart prometheus

Step 3: Install Grafana

Grafana is an open-source platform for visualizing metrics. To install Grafana, run the following commands:

# Update the package list and install Grafana
sudo apt update && sudo apt install grafana -y

Start the Grafana service:

# Start the Grafana service
sudo systemctl start grafana-server

# Enable Grafana to start at boot
sudo systemctl enable grafana-server

Step 4: Configure Grafana

Edit the Grafana configuration file to specify custom settings:

# Open the Grafana configuration file for editing
sudo nano /etc/grafana/grafana.ini

Add the following lines:

[server]
http_port = 3000

Explanation:

  • http_port: Specifies the port on which Grafana will listen for incoming requests. By default, Grafana uses port 3000.

Save and exit the file, then restart Grafana:

sudo systemctl restart grafana-server

Step 5: Create Users and Dashboards

To enhance security and collaboration, create a new user in Grafana:

# Add a new superuser
sudo grafana-cli admin reset-admin-password <new_password>

After setting up user access, log in to Grafana at http://localhost:3000 with the username admin and your new password.

Create a new dashboard:

  1. Navigate to “Create” > “Dashboard”.
  2. Add a new panel and select Prometheus as the data source.
  3. Enter your query and customize the panel for visualization.

Step 6: Integrate Prometheus with Grafana

To integrate Prometheus with Grafana:

  1. Go to “Configuration” > “Data Sources” in Grafana.
  2. Click “Add data source” and select Prometheus.
  3. Enter http://localhost:9090 as the URL and click “Save & Test”.

Once configured, create dashboards using Prometheus queries (PromQL) to visualize metrics.


Best Practices

To maintain a reliable and secure monitoring setup:

  • Secure Communication: Use SSL/TLS certificates to enable HTTPS between Prometheus and Grafana.
  • Regular Updates: Update Prometheus and Grafana to the latest versions to patch security vulnerabilities and access new features.
  • Monitor Critical Metrics: Track CPU usage, memory consumption, and disk I/O to identify potential performance bottlenecks.
  • Alerting: Configure alert rules in Prometheus to notify your team when metrics reach critical thresholds.

Troubleshooting

Common Issues

Problem: Prometheus not scraping metrics.
Solution: Verify that the scrape_interval is correctly configured and that the target endpoint is reachable.

Problem: Grafana not displaying metrics.
Solution: Ensure that the Prometheus data source is correctly configured in Grafana and that it can connect to http://localhost:9090.

Useful Commands

  • Check Prometheus logs: sudo journalctl -u prometheus
  • Check Grafana logs: sudo journalctl -u grafana-server
  • Validate Prometheus configuration: promtool check config /etc/prometheus/prometheus.yml

For further support, consult the official Prometheus documentation and Grafana documentation.


Conclusion

In this tutorial, we’ve walked through setting up Prometheus and Grafana on Ubuntu 22.04 LTS to provide real-time monitoring and visualization for your applications. By following these steps, you can:

  • Collect detailed metrics from your applications.
  • Visualize those metrics in customizable dashboards.
  • Proactively identify and resolve potential issues.

Next Steps:

  • Integrate your monitoring setup with a CI/CD pipeline for automated deployments.
  • Scale your solution to monitor multiple applications or distributed environments.
  • Experiment with advanced features such as alerting, templating, and custom plugins.

With Prometheus and Grafana in place, you’ll be equipped to maintain high-performing, reliable applications in your DevOps workflows.