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
Clone the Repository
Deploy MongoDB Resources
Install MongoDB Exporter
Verify Metrics
Deploy Grafana Dashboard
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:
Deploy MongoDB Resources
Deploy the MongoDB resources (Secret, Service, and StatefulSet) using the following commands:
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
:
Explanation of key fields:
mongodb.uri
:
Specifies the MongoDB connection string, which connects to our MongoDB service using Basic Auth:
admin
: usernamepassword123
: passwordmongodb
: name of the MongoDB Kubernetes servicegrade-submission
: namespace where the MongoDB service is deployedsvc.cluster.local
: the default domain for Kubernetes services27017
: default MongoDB port (replace if using a non-standard port)
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.
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:
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:
Log into Grafana
Click on the "Dashboards" icon in the left sidebar
Select "Browse"
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:
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:
Remember to adjust the namespace flags if you used different namespaces in your setup.