Oct 15, 2024

Monitoring MongoDB using 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 MongoDB Prometheus Exporter specifically collects metrics from MongoDB.

In this article, you will deploy MongoDB alongside the MongoDB 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 MongoDB Resources

  3. Install MongoDB Exporter

  4. Verify Metrics

  5. Deploy Grafana Dashboard

  6. Access Grafana

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 MongoDB resources, exporter configuration, and dashboard:

cd mongodb-exporter-sample

Deploy MongoDB Resources

Deploy the MongoDB resources (Secret, Service, and StatefulSet) using the following commands:

kubectl create namespace mongodb

kubectl apply -f mongodb

This command applies all the Kubernetes manifests in the mongodb/ directory, setting up your MongoDB instance in the mongodb namespace.

Install MongoDB Exporter

Add the Prometheus Community Helm repository and update it:



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

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

mongodb:
  uri: "mongodb://admin:password123@mongodb.grade-submission.svc.cluster.local:27017"

serviceMonitor:
  enabled: true
  interval: 30s
  scrapeTimeout: 10s
  additionalLabels:
    release: prometheus

image:
  tag: "0.41.0"

extraArgs

Explanation of key fields:

  1. mongodb.uri:

Specifies the MongoDB connection string, which connects to our MongoDB service using Basic Auth:

  • admin: username

  • password123: password

  • mongodb: name of the MongoDB Kubernetes service

  • grade-submission: namespace where the MongoDB service is deployed

  • svc.cluster.local: the default domain for Kubernetes services

  • 27017: default MongoDB port (replace if using a non-standard port)


  1. serviceMonitor:

The serviceMonitor configuration is used by the Prometheus to automatically discover and scrape metrics from the MongoDB exporter. Here's a breakdown of its key fields:

  • enabled: true

    • Activates the creation of a ServiceMonitor resource.

    • This allows Prometheus to automatically discover and scrape the MongoDB exporter.

  • interval: 30s

    • Sets how often Prometheus should scrape metrics from the MongoDB exporter.

    • Adjust this based on your monitoring needs and resource constraints.

  • scrapeTimeout: 10s

    • Defines the maximum time Prometheus should wait for a response from the exporter.

    • Ensure this is less than the interval to prevent overlapping scrapes.

  • release: prometheus

    • A label that matches the release name of your Prometheus installation

    • Ensures that the correct Prometheus instance picks up this ServiceMonitor.


  1. extraArgs:

The extraArgs field enables specific collectors and sets the log level. A collector in the context of Prometheus exporters is a component that gathers specific metrics from the target system.

  • -collector.diagnosticdata: Gathers diagnostic data about MongoDB.

  • -collector.replicasetstatus: Collects metrics on replica set health.

  • -collector.dbstats: Provides database-wide statistics.

  • -collector.topmetrics: Reports on resource-intensive operations.

  • -collector.indexstats: Gathers index usage and performance data.

  • -collector.collstats: Collects statistics about database collections.

  • -collector.dbstatsfreestorage: Reports on free storage in the database.

  • -collector.currentopmetrics: Captures data on currently running operations.

  • -collector.fcv: Reports the MongoDB feature compatibility version.

  • -log.level=debug: Sets detailed logging for troubleshooting.


Verify Metrics

Port-forward the MongoDB Exporter service and check the metrics:



After verifying, stop the port-forward:

Deploy Grafana Dashboard

Apply the Grafana dashboard ConfigMap:

kubectl apply -f grafana -n monitoring

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

Access Grafana

Port-forward the Grafana service:

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

The MongoDB 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 "MongoDB Dashboard" or similar

This dashboard will provide detailed insights into your MongoDB performance and health, including metrics on connections, operations, database sizes, 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:

1. Delete the MongoDB Exporter:

helm uninstall mongodb-exporter -n mongodb

2. If you deployed MongoDB as part of this guide:

3. Remove the Grafana dashboard:

4. If you no longer need the Prometheus Community Helm repository:

helm repo remove prometheus-community

Remember to adjust the namespace flags if you used different namespaces in your setup.

Let’s keep in touch

Subscribe to the mailing list and receive the latest updates

Let’s keep in touch

Subscribe to the mailing list and receive the latest updates

Let’s keep in touch

Subscribe to the mailing list and receive the latest updates

Let’s keep in touch

Subscribe to the mailing list and receive the latest updates