apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  serviceName: mysql
  selector:
    matchLabels:
      app: mysql
  updateStrategy:
    type: RollingUpdate
  replicas: 2
  template:
    metadata:
      labels:
        app: mysql
    spec:
      initContainers:
      - name: init-mysql
        image: k8spracticalguide/mysql:5.7.22
        command: ["bash", "/mnt/scripts/setup.sh"]
        volumeMounts:
        - name: confd
          mountPath: /mnt/conf.d
        - 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:
        - name: data
          mountPath: /var/lib/mysql
        - name: initdb
          mountPath: /docker-entrypoint-initdb.d
        - name: confd
          mountPath: /etc/mysql/conf.d
        - name: backup
          mountPath: /mnt/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
            - >-
              mysql -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
