# Deployement resource configuration of Mysql
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  replicas: 1 # 1 replica for mysql
  revisionHistoryLimit: 5 # Keep last 5 copies of this deployment for rollback
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - image: mysql:8 # mysql version 8
          name: mysql
          resources:
            limits:
              memory: "1Gi"
              cpu: "1" # This container use 1 CPU
            requests:
              memory: "500Mi" # Assign 500Mi memory to Mysql container 
              cpu: "500m"
          env:  # Environment variable pull value from secrets and config map
            - name: MYSQL_DATABASE
              valueFrom:
                configMapKeyRef: # Pull from ConfigMap
                  name: mysql-config
                  key: MYSQL_DATABASE
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef: # Pull from Secrets
                  name: mysql-secret
                  key: MYSQL_ROOT_PASSWORD
            - name: MYSQL_USER
              valueFrom:
                secretKeyRef: # Pull from Secrets
                  name: mysql-secret
                  key: MYSQL_USER
            - name: MYSQL_PASSWORD
              valueFrom:
                secretKeyRef: # Pull from Secrets
                  name: mysql-secret
                  key: MYSQL_PASSWORD
          ports:
            - containerPort: 3306
              name: mysql
          volumeMounts:  # Volume mount from PVC to be mounted on a perticular directory of a container
            - name: mysql-persistent-storage # Name of mount point
              mountPath: /var/lib/mysql # Path in container where data will be mount from PVC
            - name: initdb
              mountPath: /docker-entrypoint-initdb.d
      volumes: 
        - name: mysql-persistent-storage
          persistentVolumeClaim:
            claimName: mysql-pv-claim # Which PVC to be used 
        - name: initdb
          configMap:  # Populate the volume with config map data
            name: mysql-config
