---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  namespace: mysql
  labels:
    app: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        imagePullPolicy: IfNotPresent
        env:
        - name: TZ
          value: "Europe/Madrid"
        - name: MYSQL_ROOT_PASSWORD_FILE
          value: /tmp/mysql-root-password-file/password
        ports:
        - containerPort: 3306
          name: mysql-tcp
          protocol: TCP
        livenessProbe:
          exec:
            command:
            - bash
            - "-c"
            - |
              /usr/bin/mysqladmin -u root -p"$(cat /tmp/mysql-root-password-file/password)" ping
          initialDelaySeconds: 10
          failureThreshold: 5
          successThreshold: 1
          periodSeconds: 10
        readinessProbe:
          exec:
            command:
            - bash
            - "-c"
            - |
              /usr/bin/mysqladmin -u root -p"$(cat /tmp/mysql-root-password-file/password)" ping
          initialDelaySeconds: 10
          failureThreshold: 5
          successThreshold: 1
          periodSeconds: 10
        startupProbe:
          exec:
            command:
            - bash
            - "-c"
            - |
              /usr/bin/mysqladmin -u root -p"$(cat /tmp/mysql-root-password-file/password)" ping
          failureThreshold: 10
          successThreshold: 1
          initialDelaySeconds: 10
          periodSeconds: 10
        securityContext:
          privileged: true
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: mysql-data-volume
        - mountPath: /tmp/mysql-root-password-file
          name: mysql-root-password-file
        - mountPath: /usr/bin/add-mysql-user
          name: add-mysql-user-configmap
          subPath: add-mysql-user
      nodeSelector:
        mysql: server
      volumes:
      - name: mysql-data-volume
        hostPath:
          path: "/srv/mysql"
      - name: mysql-root-password-file
        secret:
          secretName: mysql-password
      - name: add-mysql-user-configmap
        configMap:
          name: add-mysql-user-configmap
          defaultMode: 0755
