MySQL on Kubernetes
MySQL on Kubernetes
Running MySQL on Kubernetes brings cloud-native benefits (elasticity, standardization) but requires careful handling of stateful workloads.
Operator Pattern
Use an Operator rather than raw StatefulSets. Operators encode operational knowledge — handling failover, backups, and scaling automatically. Percona Operator for MySQL is the most production-ready open-source option, supporting both Group Replication and classic primary/replica topologies.
StatefulSet Essentials
- Init containers set
server-idfrom Pod ordinal (100 + ordinal) - Two Services:
mysql-write(targets pod-0) andmysql-read(round-robins all pods) - PodAntiAffinity ensures replicas land on different nodes/AZs
- PodDisruptionBudget ensures at least 2/3 nodes available during maintenance
Storage
Use reclaimPolicy: Retain — the default Delete will destroy EBS volumes on PVC deletion. Use Local PV for maximum I/O or EBS gp3/io2 on AWS with WaitForFirstConsumer binding to ensure same-AZ placement.
Backups
CronJob running percona/percona-xtrabackup nightly, syncing to S3. Combine with binlog shipping for point-in-time recovery.
Observability
Deploy prom/mysqld-exporter as a sidecar or separate Deployment. Key alerts: connection pool utilization >80%, replica lag >30s, slow query rate spike.