---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql-statefulset
  namespace: mysql
  labels:
    app: mysql
spec:
  serviceName: mysql-service
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:5.7
          ports:
            - containerPort: 3306
          volumeMounts:
            - name: mysql-data
              mountPath: "/var/lib/mysql"
              subPath: "mysql"
            - name: mysql-initdb
              mountPath: /docker-entrypoint-initdb.d
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-secrets
                  key: ROOT_PASSWORD
            - name: MYSQL_USER
              valueFrom:
                secretKeyRef:
                  name: mysql-secrets
                  key: MYSQL_USER
            - name: MYSQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-secrets
                  key: MYSQL_PASSWORD
          resources:
            requests:
              cpu: 100m
              memory: 500Mi
            limits:
              memory: 1Gi
      volumes:
        - name: mysql-data
          persistentVolumeClaim:
            claimName: mysql-data-disk
        - name: mysql-initdb
          configMap:
            name: mysql-initdb-config
