Monitoring and Logging Your DevOps Pipeline with Prometheus and Grafana

Learn how to set up monitoring and logging for your DevOps pipeline using Prometheus and Grafana. Track application health, identify issues early, and maintain reliable performance.

Monitoring

Welcome back to the final post of our DevOps Pipeline series! If you’ve been following along, you’ve set up Source Code Management (SCM), implemented Continuous Integration (CI), automated Automated Testing, and deployed your applications through Continuous Delivery and Deployment (CD/CD) with Docker and Kubernetes. If you missed any of those steps, make sure to catch up on the previous posts to build a complete DevOps pipeline.

In this last part of the series, we’ll cover monitoring and logging—the critical practice of tracking your application’s performance, health, and behavior once it’s deployed. We’ll be using Prometheus for metrics collection and Grafana for visualizing and analyzing those metrics, ensuring you can quickly detect and resolve issues.

Here’s what you’ll learn:

  • How to set up Prometheus for metrics collection.
  • How to visualize and analyze your data with Grafana.
  • How to monitor your Kubernetes cluster and applications.
  • Best practices for monitoring and logging in a DevOps environment.

Ready to keep your applications stable and ensure they run smoothly? Let’s dive into Prometheus and Grafana!

In any modern DevOps workflow, monitoring and logging are fundamental. With automated deployment and rapid code changes, being proactive about tracking your infrastructure and application behavior is key to maintaining a healthy, scalable pipeline.

Key Benefits of Monitoring and Logging:

  • Visibility into Application Performance: Gain insights into latency, resource usage, errors, and traffic patterns to optimize your application.
  • Quick Detection of Anomalies and Issues: Quickly find and resolve issues with metrics that highlight abnormal behaviors before they impact users.
  • Improved Collaboration and Decision Making: Logs and metrics help development and operations teams work together more effectively by providing a common ground for performance and health data.
  • Auditing and Security: Monitoring access logs and error patterns can help you detect and mitigate security issues.

Pro Tip: A good monitoring and logging setup should cover infrastructure metrics (e.g., CPU, memory, disk usage), application metrics (e.g., response times, error rates), and business metrics (e.g., user sign-ups, purchase rates).

2.1 What Is Prometheus?

Prometheus is a powerful, open-source monitoring and alerting toolkit. Designed for multi-dimensional data collection and time-series-based storage, Prometheus is built to scrape metrics from your applications, Kubernetes clusters, servers, and other components in your environment.

Key Prometheus Features:

  • Multi-dimensional time series data storage with labels and metadata.
  • PromQL query language for querying data and building visualizations.
  • Alertmanager integration for triggering alerts based on predefined rules.
  • Extensibility with exporters to gather metrics from various applications, servers, and cloud services.

Use Case: Prometheus is ideal for monitoring microservices and containerized environments like Kubernetes, providing real-time metrics and rich data visualization.


2.2 Installing Prometheus on Different Platforms

Prometheus is highly versatile and can be installed on multiple platforms.

For Linux Systems:

1. Download the Prometheus Package:

wget https://github.com/prometheus/prometheus/releases/download/v2.30.0/prometheus-2.30.0.linux-amd64.tar.gz

2. Extract and Move to Directory:

tar -xvzf prometheus-2.30.0.linux-amd64.tar.gz
cd prometheus-2.30.0.linux-amd64

3. Run Prometheus:

./prometheus --config.file=prometheus.yml

Prometheus is now running on port 9090. Access the web UI by visiting http://localhost:9090.

For Kubernetes Cluster (Prometheus Operator):

1. Install the Prometheus Operator: Use Helm for installation:

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install prometheus prometheus-community/kube-prometheus-stack

2. Access the Prometheus UI:

kubectl port-forward svc/prometheus-kube-prometheus-prometheus 9090:9090

Visit http://localhost:9090 to access the UI.

Pro Tip: Using Helm for Kubernetes installations is recommended, as it simplifies the process of managing Kubernetes resources.


2.3 Configuring Prometheus for Metrics Collection

Prometheus uses a configuration file, typically named prometheus.yml, to define which metrics it will collect and how frequently.

1. Set the Scrape Interval and Targets:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'node-metrics'
    static_configs:
    - targets: ['localhost:9100']
  • scrape_interval: Sets how frequently to scrape metrics (15 seconds in this example).
  • job_name: Identifies the scraping job.
  • targets: Specifies the list of endpoints to scrape metrics from.

2.4 Scraping Application and System Metrics

Using Node Exporter for System Metrics

1. Download and Run Node Exporter:

wget https://github.com/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.linux-amd64.tar.gz
tar -xvzf node_exporter-1.2.2.linux-amd64.tar.gz
./node_exporter
  • Node Exporter runs on port 9100 and exposes system metrics like CPU usage, memory consumption, disk I/O, and network statistics.

2. Verify Node Exporter is Running: Access http://localhost:9100/metrics.

Scraping Application Metrics

  • Integrate Prometheus client libraries in your application code to expose custom metrics.
  • Libraries are available for various languages: Go, Python, Java, JavaScript, Ruby.
    Example (Node.js/Express):
const express = require('express');
const app = express();
const promClient = require('prom-client');

const counter = new promClient.Counter({
    name: 'my_app_requests_total',
    help: 'Total number of requests',
});

app.get('/', (req, res) => {
    counter.inc(); // Increment the counter on each request
    res.send('Hello, Prometheus!');
});

app.get('/metrics', (req, res) => {
    res.set('Content-Type', promClient.register.contentType);
    res.end(promClient.register.metrics());
});

app.listen(3000, () => console.log('App listening on port 3000'));
  • Expose metrics at /metrics.
  • Update Prometheus targets to scrape metrics from localhost:3000/metrics.

Pro Tip: Start with basic metrics (e.g., request counts, response times) and gradually expand to more detailed metrics like database queries, cache hits, and user actions.

3.1 What Is Grafana?

Grafana is an open-source monitoring platform that enables you to visualize data from multiple sources like Prometheus, Elasticsearch, Graphite, and many more. With Grafana, you can build interactive dashboards, alerts, and graphs, allowing you to analyze and act upon metrics quickly.

Grafana’s Key Features:

  • Intuitive Dashboard Builder: Build custom dashboards with graphs, tables, and gauges.
  • Alerting System: Set up alerts with notifications based on custom thresholds.
  • Multi-Source Integration: Connect to multiple data sources for a comprehensive view.

3.2 Installing Grafana and Setting Up Your Environment

For Linux Systems:

1. Add Grafana Repository and Install:

sudo apt-get install -y software-properties-common
sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
sudo apt-get update
sudo apt-get install grafana

2. Start the Grafana Server:

sudo systemctl start grafana-server
sudo systemctl enable grafana-server

3. Access Grafana UI:

  • Visit http://localhost:3000.
  • Default login credentials are admin/admin.

For Kubernetes Cluster:

1. Deploy Grafana Using Helm:

helm repo add grafana https://grafana.github.io/helm-charts
helm install my-grafana grafana/grafana

2. Access Grafana:

kubectl port-forward svc/my-grafana 3000:80

3.3 Connecting Grafana to Prometheus

  1. Add a Data Source:
    • Navigate to Configuration > Data Sources > Add data source.
    • Select Prometheus and enter the Prometheus URL (e.g., http://localhost:9090).
  2. Test the Connection:
    • Click Save & Test to ensure Grafana successfully connects to Prometheus.

3.4 Building and Customizing Dashboards for Your Metrics

1. Create a New Dashboard:

  • Go to Create > Dashboard > Add new panel.

2. Visualize Data with PromQL:

  • Use Prometheus Query Language (PromQL) to fetch and visualize your data. Example Query: To view the CPU usage:
rate(node_cpu_seconds_total{mode!="idle"}[5m])
  • The rate function calculates the rate of change for your metric over time.

3. Customize the Visualization:

  • Select visualization types like line graphs, heatmaps, gauges, and more.
  • Add labels, colors, and legends to make your dashboards easily readable.

Pro Tip: Use Grafana Dashboard Templates available in the Grafana Labs Community to quickly build standard dashboards.

4.1 Scraping Kubernetes Cluster Metrics

Monitor your Kubernetes environment by scraping cluster metrics with Prometheus. Set up a scrape configuration for nodes, pods, and services to track real-time performance and usage.

Example Scrape Configuration for Kubernetes Nodes:

scrape_configs:
  - job_name: 'kubernetes-nodes'
    kubernetes_sd_configs:
    - role: node

4.2 Installing Kube-State-Metrics for Kubernetes Monitoring

Kube-State-Metrics provides detailed metrics on the state of Kubernetes objects, such as deployments, pods, and services.

1. Deploy Kube-State-Metrics:

kubectl apply -f https://github.com/kubernetes/kube-state-metrics/releases/latest/download/kube-state-metrics.yaml

2. Add a Scrape Job in Prometheus:

- job_name: 'kube-state-metrics'
  static_configs:
  - targets: ['kube-state-metrics.default:8080']

4.3 Setting Up Alerts and Notifications in Grafana

Create real-time alerts to ensure you’re notified about performance issues before they affect your applications:

  1. Set Alert Rules in Grafana:
    • Navigate to Alerts > Notification Channels.
    • Set up conditions to trigger alerts (e.g., CPU > 90%, Memory > 85%).
  2. Integrate Alert Notifications:
    • Connect Grafana with Slack, PagerDuty, Email, or Teams to receive real-time notifications.

4.4 Visualizing Kubernetes Application Health

Visualize your Kubernetes application’s health through metrics like:

  • Pod status: Ready vs. not ready.
  • Node resource usage: CPU and memory usage across nodes.
  • Deployment status: Number of active vs. desired replicas.

Pro Tip: Use Grafana’s Alerting System to automatically scale your Kubernetes cluster based on metrics (e.g., CPU usage > 80%).

5.1 Establishing Effective Metrics

  • Use RED metrics (Rate, Error, Duration) to monitor request rates, error counts, and request duration.
  • Combine these with USE metrics (Utilization, Saturation, Errors) for infrastructure health.

5.2 Managing Alerts to Minimize Noise

  • Set up tiered alerts to separate critical issues from minor ones.
  • Use deduplication to avoid alert fatigue by grouping related alerts.

5.3 Combining Logs with Metrics for Better Observability

  • Use a logging tool like Loki to gather logs, and combine these with Prometheus metrics in Grafana.
  • Correlate logs and metrics for faster troubleshooting and debugging.

5.4 Regularly Reviewing Dashboards for Improvements

  • Keep your dashboards up to date and review them regularly to ensure they reflect the latest system behavior.
  • Create different dashboards for different teams (DevOps, QA, Security) for targeted monitoring.

By setting up Prometheus and Grafana, you’ve empowered your DevOps pipeline with real-time monitoring and logging. You can now visualize your application’s health, detect issues early, and ensure that everything runs smoothly post-deployment. This marks the final stage of our DevOps Pipeline series, and you’ve built a fully automated, monitored, and robust pipeline!

Let’s Keep the Monitoring Conversation Going!

What tools and strategies do you use for monitoring and logging in your DevOps workflow? Share your experiences below! And if you found this series helpful, spread the word to your DevOps community! 🚀


Discover more from Abdelrahman Algazzar

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top