Monitoring your MongoDB Service on Kubernetes

Monitoring your MongoDB Service on Kubernetes

5 Min Read

As more and more organizations shift to containerized applications, monitoring your MongoDB service on Kubernetes has become increasingly important. In this blog post, we will explore the best practices for monitoring MongoDB on Kubernetes and provide detailed code examples to help you get started. Let’s dive in!

Section 1: Setting up Prometheus and Grafana

Prometheus is an open-source monitoring system and time series database. It integrates well with Kubernetes and is a popular choice for monitoring containerized applications. Grafana, on the other hand, is an open-source visualization and analytics software that allows you to create beautiful dashboards from your monitoring data. To monitor your MongoDB service, you will first need to set up Prometheus and Grafana in your Kubernetes cluster.

You can use the following YAML manifest to deploy Prometheus and Grafana using the Prometheus Community Helm charts. Save this file as ‘prometheus-grafana.yaml’ and apply it using ‘kubectl apply -f prometheus-grafana.yaml’:


apiVersion: helm.cattle.io/v1
kind: HelmChart
metadata:
  name: prometheus
  namespace: monitoring
spec:
  chart: prometheus-community/kube-prometheus-stack
  targetNamespace: monitoring
  valuesContent: |-
    grafana:
      enabled: true
    prometheus:
      enabled: true
    alertmanager:
      enabled: false
    kubeStateMetrics:
      enabled: false
    nodeExporter:
      enabled: false

Section 2: Configuring Prometheus to Monitor MongoDB

Once you have Prometheus and Grafana up and running, you’ll need to configure Prometheus to scrape metrics from your MongoDB service. To do this, you’ll need to create a custom configuration file for Prometheus and update the deployment to use it. This configuration file tells Prometheus where to find your MongoDB service and which metrics to collect.

Create a file called ‘prometheus-config.yaml’ with the following content. Make sure to replace ‘{MONGODB_NAMESPACE}’ and ‘{MONGODB_SERVICE_NAME}’ with the appropriate values for your MongoDB service:


global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: 'mongodb'
    static_configs:
      - targets: ['{MONGODB_SERVICE_NAME}.{MONGODB_NAMESPACE}:27017']
    metrics_path: /metrics
    basic_auth:
      username: 'metrics'
      password: 'your_password_here'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: '{MONGODB_SERVICE_NAME}.{MONGODB_NAMESPACE}:9216'

Next, create a Kubernetes ConfigMap to store the Prometheus configuration file and update the Prometheus deployment to use it:



apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config
namespace: monitoring
data:
prometheus.yml: |
(Insert the contents of 'prometheus-config.yaml' here)

apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus-deployment
namespace: monitoring
spec:
template:
spec:
containers:
- name: prometheus
image: prom/prometheus:latest
volumeMounts:
- name: prometheus-config
mountPath: /etc/prometheus/prometheus.yml
subPath: prometheus.yml
volumes:
- name: prometheus-config
configMap:
name: prometheus-config

Section 3: MongoDB Exporter for Prometheus

To monitor MongoDB, you’ll need to use the MongoDB Exporter from Percona, which exposes MongoDB metrics in a format that Prometheus can scrape. Deploy the MongoDB Exporter as a sidecar container alongside your MongoDB service. First, create a Kubernetes secret to store the MongoDB Exporter credentials:


apiVersion: v1
kind: Secret
metadata:
  name: mongodb-exporter-credentials
  namespace: {MONGODB_NAMESPACE}
stringData:
  MONGODB_URI: "mongodb://metrics:your_password_here@{MONGODB_SERVICE_NAME}.{MONGODB_NAMESPACE}:27017"

Next, update your MongoDB deployment or StatefulSet to include the MongoDB Exporter container. Add the following container definition to the ‘spec.template.spec.containers’ section:


- name: mongodb-exporter
  image: percona/mongodb_exporter:latest
  env:
    - name: MONGODB_URI
      valueFrom:
        secretKeyRef:
          name: mongodb-exporter-credentials
          key: MONGODB_URI
  ports:
    - name: exporter
      containerPort: 9216

Section 4: Visualizing MongoDB Metrics in Grafana

With Prometheus now collecting metrics from your MongoDB service, it’s time to visualize the data using Grafana. You can use the official Percona MongoDB dashboard as a starting point, and customize it according to your needs. To import the dashboard, follow these steps:

1. Access the Grafana UI by port-forwarding the Grafana service: ‘kubectl port-forward svc/grafana -n monitoring 3000:80’.

2. Open your browser and navigate to ‘http://localhost:3000’.

3. Log in using the default credentials (admin/admin), and change the password when prompted.

4. Click on the ‘+’ icon on the left side menu, and then click ‘Import’.

5. Enter the dashboard ID ‘2583’ and click ‘Load’.

6. Select ‘Prometheus’ as the data source and click ‘Import’.

Now you have a comprehensive dashboard for monitoring your MongoDB service on Kubernetes. Feel free to customize the dashboard to suit your specific requirements.

Share this Article
Leave a comment