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
Clone the Repository
Deploy MySQL Resources
Install MySQL Exporter
Verify Metrics
Deploy Grafana Dashboard
Access Grafana
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:
Deploy MySQL Resources
Create the namespace database-monitoring
if it doesn't already exist:
Deploy the MySQL resources (Secret, Service, and StatefulSet) using the following command:
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:
Let's examine the custom values in mysql-exporter-values.yaml
:
Explanation of key fields:
mysql: Specifies the MySQL connection details.
host
: The MySQL service name.port
: The MySQL port.user
andpass
: MySQL credentials for the exporter.existingSecret
andexistingSecretKey
: 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:
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 MySQL 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 "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:
If you deployed MySQL as part of this guide:
Remove the Grafana dashboard:
If you no longer need the Prometheus Community Helm repository:
Finally, delete the namespace:
Kubernetes Training
If you found this guide helpful, check out our Kubernetes Training course