Oct 18, 2024

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

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

  3. Install MySQL 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 your repository containing the necessary MySQL resources, exporter configuration, and dashboard:

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

Deploy MySQL Resources

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

kubectl create namespace database-monitoring

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

kubectl apply -f mysql

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

Install MySQL Exporter

Add the Prometheus Community Helm repository and update it:



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

helm install mysql-exporter prometheus-community/prometheus-mysql-exporter \
  --namespace database-monitoring \
  --values

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

mysql:
  host: "mysql.database-monitoring.svc.cluster.local"
  port: 3306
  user: "mysqluser"
  pass: "userpassword"
  existingSecret: "mysql-secret"
  existingSecretKey: "MYSQL_PASSWORD"

serviceMonitor:
  enabled: true

Explanation of key fields:

  • mysql: Specifies the MySQL connection details.

    • host: The MySQL service name.

    • port: The MySQL port.

    • user and pass: MySQL credentials for the exporter.

    • existingSecret and existingSecretKey: Specifies the Kubernetes secret containing the MySQL password.

  • serviceMonitor: Configuration for Prometheus to automatically discover and scrape metrics from the MySQL exporter.

    • enabled: true: Activates the creation of a ServiceMonitor resource.

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

  • resources: Sets resource limits and requests for the exporter.

Verify Metrics

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

kubectl port-forward svc/mysql-exporter-prometheus-mysql-exporter 9104:9104 -n database-monitoring & 

curl

After verifying, stop the port-forward:

kill

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:

kubectl port-forward svc/prometheus-grafana 3000:80 -n monitoring

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

The MySQL 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 "MySQL Exporter Quickstart"

This dashboard will provide detailed insights into your MySQL performance and health, including metrics on connections, query execution, InnoDB performance, 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 MySQL Exporter:

helm uninstall mysql-exporter -n database-monitoring

If you deployed MySQL as part of this guide:

kubectl delete -f mysql/ -n database-monitoring

Remove the Grafana dashboard:

kubectl delete -f grafana -n monitoring

If you no longer need the Prometheus Community Helm repository:

helm repo remove prometheus-community

Finally, delete the namespace:

kubectl delete namespace database-monitoring

Kubernetes Training

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

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