How to Run MySQL, PostgreSQL & MongoDB on Kubernetes: A Step‑by‑Step Guide
Learn how to deploy stateful databases such as MySQL, PostgreSQL, and MongoDB on Kubernetes by mastering StatefulSets, PersistentVolumes, PVCs, StorageClasses, and related best practices for storage, scaling, backup, monitoring, and disaster recovery.
Key Concepts for Running Databases on Kubernetes
Databases are stateful workloads that require persistent storage, backup, high availability, and stable network identities. Kubernetes, originally designed for stateless apps, can run databases safely by using StatefulSets, PersistentVolumes (PV), PersistentVolumeClaims (PVC), and StorageClasses.
StatefulSets
StatefulSet is a Kubernetes resource for managing stateful applications such as databases. It ensures each pod has stable network identity and persistent storage.
Persistent storage: Uses PVs so each pod retains data after restarts.
Stable network identifiers: Each pod receives a unique, consistent name (e.g., mypod-0).
Tutorial: Create a Database on Kubernetes
Step 1: Create a StorageClass (if needed)
Define a StorageClass to provision volumes dynamically. Example for AWS EKS:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: standard
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp3
reclaimPolicy: RetainApply with kubectl apply -f storage-class.yaml.
Step 2: Create a PersistentVolume (PV)
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: standard
hostPath:
path: /mnt/dataStep 3: Create a PersistentVolumeClaim (PVC)
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: standardStep 4: Deploy MySQL StatefulSet
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: "mysql"
replicas: 3
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
ports:
- containerPort: 3306
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: "your_password"
volumeMounts:
- name: mysql-storage
mountPath: /var/lib/mysql
volumeClaimTemplates:
- metadata:
name: mysql-storage
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 10Gi
storageClassName: standardStep 5: Create a Headless Service for MySQL
# Headless service
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
app: mysql
spec:
ports:
- name: mysql
port: 3306
selector:
app: mysqlStep 6: Ship MySQL Logs to Monitoring Tools
Configure MySQL to forward slow‑query, error, and general logs to systems such as Datadog, Grafana, Prometheus, or the ELK stack for performance analysis.
Step 7: Perform Regular Backups and Restores
Use tools like Velero to back up PVs and restore them, ensuring data durability. Velero provides cluster backup, disaster recovery, and scheduled backup capabilities.
Step 8: Configure Database Alerts
Set up alerting with Datadog, Nagios, Prometheus, or Grafana and integrate with Slack or PagerDuty to receive notifications on performance issues or failures.
Conclusion
Running databases on Kubernetes introduces challenges around state management, persistent storage, and network stability, but by leveraging PersistentVolumes, StorageClasses, StatefulSets, and PVCs, operators can ensure data integrity and high availability.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
