apiVersion: v1
kind: ServiceAccount
metadata:
  name: $NAME
  labels:
    db: mysql
    instance: $NAME
---
apiVersion: v1
kind: Service
metadata:
  name: $NAME
  labels:
    db: mysql
    instance: $NAME
spec:
  type: ClusterIP
  ports:
    - name: server
      port: 3306
      protocol: TCP
      targetPort: 3306
  selector:
    db: mysql
    instance: $NAME
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-$NAME
  labels:
    db: mysql
    instance: $NAME
  namespace:
spec:
  selector:
    matchLabels:
      db: mysql
      instance: $NAME
  replicas: 1
  template:
    metadata:
      labels:
        db: mysql
        instance: $NAME
    spec:
      serviceAccountName: $NAME
      volumes:
      - name: certs
        secret:
          secretName: mysql-$NAME-config
      - name: data
        persistentVolumeClaim:
          claimName: mysql-$NAME-pvc
      containers:
        - name: mariadb
          image: mariadb:10.7.4
          ports:
            - containerPort: 3306
              name: server
          volumeMounts:
          - name: data
            mountPath: /var/lib/mariadb
          - name: certs
            mountPath: /etc/mysql/conf.d/
          env:
          - name: MYSQL_ROOT_PASSWORD
            valueFrom:
              secretKeyRef:
                name: mysql-$NAME-config
                key: MYSQL_ROOT_PASSWORD                
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-$NAME-pvc
  labels:
    db: mysql
    instance: $NAME
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
  storageClassName: standard
