apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  serviceName: mysql
  updateStrategy:
    type: RollingUpdate
  replicas: 2
  template:
    metadata:
      labels:
        app: mysql
    spec:
      initContainers:
        - name: int-mysql
          image: k8spracticalguide/mysql:5.7.22
          command:
            - "bash"
            - "/mnt/scripts/setup.sh"
          volumeMounts:
            - mountPath: /mnt/conf.d
              name: confd
            - name: scripts
              mountPath: /mnt/scripts
            - name: initdb
              mountPath: /mnt/initdb
      containers:
        - name: mysql
          image: k8spracticalguide/mysql:5.7.22
          envFrom:
            - configMapRef:
                name: common-env
            - secretRef:
                name: common-env
          volumeMounts:
            - mountPath: /var/lib/mysql
              name: data
            - mountPath: /docker-entrypoint-initdb.d
              name: initdb
            - mountPath: /etc/mysql/conf.d
              name: confd
            - mountPath: /mnt/backup
              name: backup
          livenessProbe:
            exec:
              command:
                - /bin/bash
                - -ec
                - >-
                  mysqladmin -h localhost --user=root --password=${MYSQL_ROOT_PASSWORD} ping
            initialDelaySeconds: 5
            periodSeconds: 30
            timeoutSeconds: 5
          readinessProbe:
            exec:
              command:
                - /bin/bash
                - -ec
                - >-
                  mysqladmin -h localhost --user=root --password=${MYSQL_ROOT_PASSWORD} -e "SELECT 1"
            initialDelaySeconds: 5
            periodSeconds: 30
            timeoutSeconds: 5
      volumes:
        - name: initdb
          emptyDir: {}
        - name: confd
          emptyDir: {}
        - name: scripts
          configMap:
            name: mysql-scripts
        - name: backup
          persistentVolumeClaim:
            claimName: backup-mysql
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 1Gi