Deploy MySQL on Kubernetes: Step‑by‑Step Guide with Helm, PVs, and Monitoring
This tutorial explains how to deploy a primary‑replica MySQL cluster on Kubernetes using Helm, configuring persistent volumes, setting up Prometheus monitoring, and provides commands for installation, verification, and clean removal, all with detailed code snippets.
Overview
MySQL is a relational database management system originally developed by MySQL AB and now owned by Oracle. It is one of the most popular RDBMS for web applications. Deploying MySQL on Kubernetes offers resource isolation, dynamic scaling, environment consistency, and easier operations.
Resource isolation
Dynamic scaling
Environment consistency
Operational convenience
Official documentation: https://docs.oracle.com/en-us/iaas/mysql-database/doc/getting-started.html
Deploying a Primary‑Replica MySQL Cluster
1) Add Helm repository
helm repo add bitnami https://charts.bitnami.com/bitnami
helm pull bitnami/mysql
tar -xf mysql-9.3.3.tgz2) Modify configuration
Edit mysql/values.yaml to set image registry, repository, tag, architecture, persistence, storageClass, nodePort, and other parameters.
...
image:
registry: myharbor.com
repository: bigdata/mysql
tag: 8.0.30-debian-11-r15
architecture: replication
primary:
persistence:
enabled: true
size: 10Gi
storageClass: "mysql-local-storage"
local:
- name: mysql-0
host: "local-168-182-110"
path: "/opt/bigdata/servers/mysql/data/data1"
service:
type: NodePort
nodePorts:
mysql: "30306"
secondary:
replicaCount: 2
persistence:
enabled: true
size: 10Gi
storageClass: "mysql-local-storage"
local:
- name: mysql-1
host: "local-168-182-111"
path: "/opt/bigdata/servers/mysql/data/data1"
- name: mysql-2
host: "local-168-182-112"
path: "/opt/bigdata/servers/mysql/data/data1"
service:
type: NodePort
nodePorts:
mysql: "30307"
metrics:
enabled: true
image:
registry: myharbor.com
repository: bigdata/mysqld-exporter
tag: 0.14.0-debian-11-r333) Add PersistentVolume definitions
{{- range .Values.primary.persistence.local }}
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: {{ .name }}
labels:
name: {{ .name }}
spec:
storageClassName: {{ $.Values.primary.persistence.storageClass }}
capacity:
storage: {{ $.Values.primary.persistence.size }}
accessModes:
- ReadWriteOnce
local:
path: {{ .path }}
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- {{ .host }}
---
{{- end }}
{{- range .Values.secondary.persistence.local }}
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: {{ .name }}
labels:
name: {{ .name }}
spec:
storageClassName: {{ $.Values.secondary.persistence.storageClass }}
capacity:
storage: {{ $.Values.secondary.persistence.size }}
accessModes:
- ReadWriteOnce
local:
path: {{ .path }}
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- {{ .host }}
---
{{- end }}4) Create StorageClass
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: {{ .Values.primary.persistence.storageClass }}
provisioner: kubernetes.io/no-provisioner5) Install the chart
# Create persistent directories
mkdir -p /opt/bigdata/servers/mysql/data/data1
# Pull and push MySQL image to private registry
docker pull docker.io/bitnami/mysql:8.0.30-debian-11-r15
docker tag docker.io/bitnami/mysql:8.0.30-debian-11-r15 myharbor.com/bigdata/mysql:8.0.30-debian-11-r15
docker push myharbor.com/bigdata/mysql:8.0.30-debian-11-r15
# Pull and push mysqld‑exporter image
docker pull docker.io/bitnami/mysqld-exporter:0.14.0-debian-11-r33
docker tag docker.io/bitnami/mysqld-exporter:0.14.0-debian-11-r33 myharbor.com/bigdata/mysqld-exporter:0.14.0-debian-11-r33
docker push myharbor.com/bigdata/mysqld-exporter:0.14.0-debian-11-r33
# Install Helm chart
helm install mysql ./mysql -n mysql --create-namespaceCheck deployment status with kubectl get pods -w -n mysql and retrieve the root password:
MYSQL_ROOT_PASSWORD=$(kubectl get secret -n mysql mysql -o jsonpath="{.data.mysql-root-password}" | base64 -d) NAME: mysql
LAST DEPLOYED: Mon Sep 19 23:57:18 2022
NAMESPACE: mysql
STATUS: deployed
REVISION: 1
CHART: mysql 9.3.3
APP VERSION: 8.0.306) Test and verify
Primary service (read/write) is reachable at mysql-primary.mysql.svc.cluster.local:3306; secondary service (read‑only) at mysql-secondary.mysql.svc.cluster.local:3306. Use a client pod to run MySQL commands against each endpoint.
7) Prometheus monitoring
Deploy the mysqld‑exporter sidecar, forward port 9104, and query metrics (e.g.,
kubectl port-forward -n mysql svc/mysql-metrics 9104:9104 &then curl http://127.0.0.1:9104/metrics). Grafana can visualize the metrics; retrieve the admin password with:
kubectl get secret -n grafana grafana -o jsonpath="{.data.admin-password}" | base64 --decode8) Uninstall
helm uninstall mysql -n mysql
kubectl delete pod -n mysql $(kubectl get pod -n mysql | awk 'NR>1{print $1}') --force
kubectl patch ns mysql -p '{"metadata":{"finalizers":null}}'
kubectl delete ns mysql --forceConclusion
The guide implements a primary‑replica MySQL cluster on Kubernetes but does not cover high‑availability solutions; those are left for future exploration.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
