In this tutorial, we'll walk through how to set up MySQL on a Kubernetes (K8s) cluster, along with integrating Prometheus and Grafana for monitoring. We'll use the Bitnami Helm charts for MySQL, Prometheus, and Grafana, and show you how to create a custom Grafana dashboard to monitor MySQL.
1. Create a Namespace for Your Lab
First, create a Kubernetes namespace where you’ll deploy your MySQL and monitoring components.
kubectl create namespace my-lab
To verify the creation of the namespace, run:
kubectl get namespaces
This should list my-lab as one of the namespaces in your cluster.
2. Install MySQL Using Helm
We'll use the Bitnami Helm chart to install MySQL in the my-lab namespace. If you haven't added the Bitnami chart repository yet, do so with the following command:
helm repo add bitnami https://charts.bitnami.com/bitnami
Now, install MySQL with this command:
helm upgrade --install mysql bitnami/mysql --namespace my-lab
To check the status and ensure MySQL is running, use:
kubectl get pods --namespace my-lab
Access the MySQL Pod
To connect to MySQL, first, you’ll need to retrieve the MySQL password from the Kubernetes secrets. Use this command to get the secret:
kubectl get secret -n my-lab
Then decode the mysql-root-password and mysql-password as follows:
kubectl get secret/mysql -oyaml -n my-lab
You can decode the password using base64:
echo <encoded-password> | base64 -d </encoded-password>
Now, access the MySQL pod using kubectl exec:
kubectl exec -it mysql-0 --namespace my-lab -- /bin/bash
Once inside the pod, connect to MySQL using the root password:
mysql -u root -p
Enter the decoded root password when prompted.
Create a Database and Some Tables
Once inside MySQL, create a database and some tables to monitor:
create database my_db_1; use my_db_1; create table my_table_1 (a varchar(100)); create table my_table_2 (a varchar(100)); create table my_table_3 (a varchar(100)); insert into my_table_1 values('One'); insert into my_table_1 values('One-One'); select * from my_table_1;
3. Install Prometheus and Grafana
Next, we will install Prometheus and Grafana to monitor MySQL.
Install Prometheus and Grafana
First, add the Prometheus community Helm charts:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts helm repo update
Create a grafana namespace:
kubectl create ns grafana
Now, install the Prometheus-Grafana stack using Helm:
helm upgrade --install grafana prometheus-community/kube-prometheus-stack --namespace grafana
You can verify the installation by checking the pods:
kubectl get pods -n grafana
Expose MySQL with LoadBalancer
To access MySQL from outside the Kubernetes cluster, we need to expose the MySQL service using a LoadBalancer. Edit the MySQL service:
kubectl edit svc/mysql -n my-lab
Change the service type from ClusterIP to LoadBalancer:
spec: type: LoadBalancer
Verify the external IP has been assigned:
kubectl get svc -n my-lab
If you're using Minikube, you may need to run minikube tunnel to expose the service externally:
minikube tunnel
Now, access MySQL externally using the IP assigned:
kubectl create namespace my-lab
4. Install Prometheus MySQL Exporter
We need to install the MySQL Exporter to collect MySQL metrics and expose them for Prometheus to scrape.
Create Exporter Configuration
Create a file mysql-exporter-values.yaml with your MySQL connection details:
kubectl get namespaces
Now, install the MySQL exporter using Helm:
helm repo add bitnami https://charts.bitnami.com/bitnami
Port-Forward Prometheus MySQL Exporter
To forward the MySQL exporter metrics to your local machine for testing, use:
helm upgrade --install mysql bitnami/mysql --namespace my-lab
Verify the metrics are being exposed by visiting http://localhost:9104/metrics.
5. Connect Grafana to Prometheus
Now that Prometheus and the MySQL exporter are set up, let’s connect Grafana to Prometheus.
Port-Forward Grafana
To access the Grafana dashboard, run the following port-forward command:
kubectl get pods --namespace my-lab
This will make Grafana accessible at http://localhost:3000. The default login credentials are:
Username: admin
Password: prom-operator
Once logged in, add Prometheus as a data source in Grafana:
Go to Configuration > Data Sources.
Add Prometheus as a data source with the URL http://prometheus-operated:9090.
6. Import Grafana Dashboards
To monitor MySQL, we can import pre-configured dashboards from Grafana's dashboard repository.
Import Dashboards
Go to Dashboards > Import and enter the following dashboard IDs:
Dashboard ID 14057 (MySQL Overview)
Dashboard ID 7362 (MySQL Performance)
These dashboards will automatically load and show relevant MySQL metrics, such as connections, queries, and resource utilization.
7. Conclusion
You've successfully set up MySQL in Kubernetes and integrated Prometheus and Grafana for monitoring. You can now monitor your MySQL instance in real-time using Grafana dashboards.
Key Takeaways:
- MySQL is running on Kubernetes using the Bitnami Helm chart.
- Prometheus is used to scrape metrics from MySQL using the MySQL exporter.
- Grafana is configured to visualize the data collected by Prometheus.
- You can access MySQL externally using a LoadBalancer service.
With this setup, you can easily scale and monitor your database infrastructure on Kubernetes.
The above is the detailed content of Setting Up MySQL on Kubernetes with Prometheus & Grafana Monitoring. For more information, please follow other related articles on the PHP Chinese website!

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.