apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: database
spec:
  serviceName: dbsvc
  selector:
    matchLabels:
      app.k8s.io/name: mysql
  template:
    metadata:
      labels:
        app.k8s.io/name: mysql
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: mysql
          image: mysql:latest
          volumeMounts:
            - name: dbvol
              mountPath: /var/lib/mysql
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-credentials
                  key: rootPassword
            - name: MYSQL_DATABASE
              valueFrom:
                secretKeyRef:
                  name: mysql-credentials
                  key: databaseName
            - name: MYSQL_USER
              valueFrom:
                secretKeyRef:
                  name: mysql-credentials
                  key: mysqlUsername
            - name: MYSQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-credentials
                  key: mysqlPassword
      volumes:
        - name: dbvol
          persistentVolumeClaim:
            claimName: dbvol