Deploy a Highly Available MySQL Cluster on Kubernetes – Step‑by‑Step Guide
This guide explains MySQL’s fundamentals, describes a master‑slave high‑availability architecture, and provides a complete step‑by‑step tutorial for deploying the cluster on Kubernetes using ConfigMaps, Services, a StatefulSet, dynamic PVCs, and verification commands.
MySQL Overview
MySQL is a popular open‑source relational database management system, widely used in web applications for its speed, low cost, and dual licensing (community and commercial).
High‑Availability Architecture
The guide adopts a master‑slave replication with read‑write separation: a single master handles writes, multiple slaves serve reads. If the master fails, traffic can be switched to a slave. Replication is asynchronous by default and can be configured per database or table.
Kubernetes Deployment Steps
1. Create a ConfigMap containing MySQL configuration files for master and slave.
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql
labels:
app: mysql
data:
master.cnf: |
[mysqld]
log-bin
log_bin_trust_function_creators=1
lower_case_table_names=1
slave.cnf: |
[mysqld]
super-read-only
log_bin_trust_function_creators=1Apply with kubectl apply -f mysql-configmap.yaml -n kube-public.
2. Create Services for the master (mysql) and read‑only endpoint (mysql‑read).
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
app: mysql
spec:
ports:
- name: mysql
port: 3306
clusterIP: None
selector:
app: mysql
---
apiVersion: v1
kind: Service
metadata:
name: mysql-read
labels:
app: mysql
spec:
ports:
- name: mysql
port: 3306
selector:
app: mysqlApply with kubectl apply -f mysql-service.yaml -n kube-public.
3. Create a StatefulSet that runs three MySQL pods, mounts the ConfigMap, and provisions a dynamic PersistentVolumeClaim using an NFS storage class.
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
serviceName: mysql
replicas: 3
volumeClaimTemplates:
- metadata:
name: data
annotations:
volume.beta.kubernetes.io/storage-class: "nfs"
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 10Gi
template:
metadata:
labels:
app: mysql
spec:
initContainers:
- name: init-mysql
image: mysql:5.7
command:
- bash
- "-c"
- |
set -ex
[[ `hostname` =~ -([0-9]+)$ ]] || exit 1
ordinal=${BASH_REMATCH[1]}
echo [mysqld] > /mnt/conf.d/server-id.cnf
echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
if [[ $ordinal -eq 0 ]]; then
cp /mnt/config-map/master.cnf /mnt/conf.d/
else
cp /mnt/config-map/slave.cnf /mnt/conf.d/
fi
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ALLOW_EMPTY_PASSWORD
value: "1"
ports:
- name: mysql
containerPort: 3306
volumeMounts:
- name: data
mountPath: /var/lib/mysql
subPath: mysql
- name: conf
mountPath: /etc/mysql/conf.d
resources:
requests:
cpu: 500m
memory: 1Gi
livenessProbe:
exec:
command: ["mysqladmin", "ping"]
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
readinessProbe:
exec:
command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
initialDelaySeconds: 5
periodSeconds: 2
timeoutSeconds: 1
volumes:
- name: conf
emptyDir: {}
- name: config-map
configMap:
name: mysqlDeploy with kubectl apply -f mysql-statefulset.yaml -n kube-public and monitor pods using kubectl get pods -l app=mysql -w --namespace=kube-public.
Verification
Run a temporary MySQL client container to connect to the master and create a test database:
kubectl run mysql-client --image=mysql:5.7 -it --rm --restart=Never -- mysql -h mysql-0.mysql.kube-public
CREATE DATABASE demo;
CREATE TABLE demo.messages (message VARCHAR(250));
INSERT INTO demo.messages VALUES ('hello');Similarly, connect to the read‑only service mysql-read to verify replication.
Dynamic PVC creation requires an NFS StorageClass:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs
provisioner: example.com/nfsApply with kubectl apply -f class.yaml.
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.
Full-Stack DevOps & Kubernetes
Focused on sharing DevOps, Kubernetes, Linux, Docker, Istio, microservices, Spring Cloud, Python, Go, databases, Nginx, Tomcat, cloud computing, and related technologies.
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.
