Kitloft

All tools / Kubernetes examples

MySQL on Kubernetes

MySQL as a StatefulSet with a persistent volume, root and app passwords in a Secret, and health checks.

Customize names, versions and passwords →

mysql.yaml
apiVersion: v1
kind: Secret
metadata:
  name: mysql
  namespace: default
type: Opaque
stringData:
  MYSQL_ROOT_PASSWORD: "change-me"
  MYSQL_DATABASE: "app"
  MYSQL_USER: "app"
  MYSQL_PASSWORD: "change-me"
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: default
spec:
  clusterIP: None
  selector:
    app: mysql
  ports:
    - name: mysql
      port: 3306
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
  namespace: default
spec:
  serviceName: mysql
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      securityContext:
        fsGroup: 999
      containers:
        - name: mysql
          image: mysql:8.4
          ports:
            - name: mysql
              containerPort: 3306
          envFrom:
            - secretRef:
                name: mysql
          readinessProbe:
            exec:
              command: ["mysqladmin", "ping", "-h", "127.0.0.1"]
            initialDelaySeconds: 10
            periodSeconds: 10
          livenessProbe:
            exec:
              command: ["mysqladmin", "ping", "-h", "127.0.0.1"]
            initialDelaySeconds: 60
            periodSeconds: 20
          resources:
            requests:
              cpu: 250m
              memory: 512Mi
            limits:
              memory: 2Gi
          volumeMounts:
            - name: data
              mountPath: /var/lib/mysql
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 10Gi

Passwords here are placeholders (change-me). The generator creates random ones for you.

How to use it

Commands
kubectl apply -f mysql.yaml
kubectl -n default get pods -w

Prefer Docker Compose? See the MySQL Docker Compose version.