Monitoring Redis with Prometheus Exporter and Grafana

rslim087a

Rayan Slim

Posted on October 18, 2024

Monitoring Redis with Prometheus Exporter and Grafana

A Prometheus exporter is a tool that collects metrics from a specific system and exposes them in a format that Prometheus can scrape. The Redis Prometheus Exporter specifically collects metrics from Redis databases.

Redis Prometheus Exporter Metrics

In this article, you will deploy Redis alongside the Redis Prometheus Exporter to scrape metrics from your database. You'll then visualize these metrics using a Grafana dashboard.

Table of Contents

  1. Clone the Repository
  2. Deploy Redis Resources
  3. Install Redis Exporter
  4. Verify Metrics
  5. Deploy Grafana Dashboard
  6. Access Grafana
  7. Cleaning Up Resources

Prerequisites

This guide assumes you have already installed Helm and set up Prometheus in your Kubernetes cluster. If you haven't done this yet, please check out this article on setting up Prometheus with Kubernetes before continuing with this guide.

Clone the Repository

First, clone the repository containing the necessary Redis resources, exporter configuration, and dashboard:

git clone https://github.com/rslim087a/redis-prometheus-sample

cd redis-prometheus-sample
Enter fullscreen mode Exit fullscreen mode

Deploy Redis Resources

Create the namespace database-monitoring if it doesn't already exist:

kubectl create namespace database-monitoring
Enter fullscreen mode Exit fullscreen mode

Deploy the Redis resources (Secret, Service, and StatefulSet) using the following command:

kubectl apply -f redis/
Enter fullscreen mode Exit fullscreen mode

This command applies all the Kubernetes manifests in the redis/ directory, setting up your Redis instance.

Install Redis Exporter

Add the Prometheus Community Helm repository and update it:

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
Enter fullscreen mode Exit fullscreen mode

Now, install the Redis Exporter using the custom values file:

helm install redis-exporter prometheus-community/prometheus-redis-exporter \
  -f redis-exporter/redis-exporter-values.yaml \
  -n database-monitoring
Enter fullscreen mode Exit fullscreen mode

Let's examine the custom values in redis-exporter-values.yaml:

# Redis Exporter Helm Values

redisAddress: redis://redis:6379
auth:
  enabled: true
  secret:
    name: redis-secret
    key: REDIS_PASSWORD

serviceMonitor:
  enabled: true
  labels:
    release: prometheus

service:
  type: ClusterIP
  port: 9121

resources:
  limits:
    cpu: 100m
    memory: 128Mi
  requests:
    cpu: 100m
    memory: 128Mi
Enter fullscreen mode Exit fullscreen mode

Explanation of key fields:

  • redisAddress: Specifies the Redis connection details.
  • auth: Configures authentication for Redis.
    • enabled: true: Activates authentication.
    • secret: Specifies the Kubernetes secret containing the Redis password.
  • serviceMonitor: Configuration for Prometheus to automatically discover and scrape metrics from the Redis exporter.
    • enabled: true: Activates the creation of a ServiceMonitor resource.
    • labels: Ensures that the correct Prometheus instance picks up this ServiceMonitor.
  • service: Configures the Kubernetes service for the exporter.
  • resources: Sets resource limits and requests for the exporter.

Verify Metrics

Port-forward the Redis Exporter pod and check the metrics:

kubectl port-forward svc/redis-exporter-prometheus-redis-exporter 9121:9121 -n database-monitoring &

curl http://localhost:9121/metrics
Enter fullscreen mode Exit fullscreen mode

After verifying, stop the port-forward:

kill %1
Enter fullscreen mode Exit fullscreen mode

Deploy Grafana Dashboard

Apply the Grafana dashboard ConfigMap:

kubectl apply -f grafana -n monitoring
Enter fullscreen mode Exit fullscreen mode

This creates a ConfigMap with the grafana_dashboard: "1" label, which Grafana will automatically detect and import.

Access Grafana

Port-forward the Grafana service:

kubectl port-forward svc/prometheus-grafana 3000:80 -n monitoring &
Enter fullscreen mode Exit fullscreen mode

Access Grafana at http://localhost:3000 using the default credentials (usually admin/prom-operator).

The Redis dashboard should now be available in your Grafana instance. To find it, follow these steps:

  1. Log into Grafana
  2. Click on the "Dashboards" icon in the left sidebar
  3. Select "Browse"
  4. Look for a dashboard named "Redis Dashboard for Prometheus"

Redis Grafana Dashboard

This dashboard will provide detailed insights into your Redis performance and health, including metrics on connections, memory usage, operations per second, and more.

Cleaning Up Resources

If you've deployed this setup for testing or learning purposes, you may want to clean up the resources afterwards to avoid unnecessary costs or cluster clutter.

Here are the steps to remove the deployed resources:

Delete the Redis Exporter:

   helm uninstall redis-exporter -n database-monitoring
Enter fullscreen mode Exit fullscreen mode

If you deployed Redis as part of this guide:

   kubectl delete -f redis/ -n database-monitoring
Enter fullscreen mode Exit fullscreen mode

Remove the Grafana dashboard:

   kubectl delete -f grafana -n monitoring
Enter fullscreen mode Exit fullscreen mode

If you no longer need the Prometheus Community Helm repository:

   helm repo remove prometheus-community
Enter fullscreen mode Exit fullscreen mode

Finally, delete the namespace:

   kubectl delete namespace database-monitoring
Enter fullscreen mode Exit fullscreen mode

Kubernetes Training

If you found this guide helpful, check out our Kubernetes Training course

💖 💪 🙅 🚩
rslim087a
Rayan Slim

Posted on October 18, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related