apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mariadb
  namespace: mysql
spec:
  selector:
    matchLabels:
      app: mariadb
  serviceName: "mariadb"
  replicas: 3
  podManagementPolicy: Parallel
  template:
    metadata:
      labels:
        app: mariadb
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9104"
    spec:
      terminationGracePeriodSeconds: 30
      initContainers:
      - name: init-config
        command: ['/bin/bash', '/etc/mysql/conf.d-configmap/init.sh']
        env:
        - name: K8S_NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: DATADIR
          value: /data/db
        - name: AUTO_RECOVERY_MODE
          value: confirm-force-bootstrap
        - name: WSREP_CLUSTER_ADDRESS
          value: "gcomm://mariadb-0.mariadb,mariadb-1.mariadb,mariadb-2.mariadb"
        workingDir: /etc/mysql/conf.d-configmap
        volumeMounts:
        - name: mysql
          mountPath: /data
        - name: conf-readonly
          mountPath: /etc/mysql/conf.d-configmap
        - name: conf
          mountPath: /etc/mysql/conf.d
        - name: initdb
          mountPath: /docker-entrypoint-initdb.d
        image: mariadb:10.2.36-bionic@sha256:b7be3ade3d5441c79b5c8a9cf2c2269f14bf420876a06def7d50e1763f042238
      containers:
      - name: mariadb
        image: mariadb:10.2.36-bionic@sha256:b7be3ade3d5441c79b5c8a9cf2c2269f14bf420876a06def7d50e1763f042238
        ports:
        - containerPort: 3306
          name: mysql
        - containerPort: 4444
          name: sst
        - containerPort: 4567
          name: replication
        - containerPort: 4567
          protocol: UDP
          name: replicationudp
        - containerPort: 4568
          name: ist
        env:
        - name: MYSQL_ROOT_HOST
          value: "localhost"
        - name: MYSQL_ALLOW_EMPTY_PASSWORD
          value: "yes"
        - name: MYSQL_INITDB_SKIP_TZINFO
          value: "yes"
        readinessProbe:
          exec:
            command:
            - /bin/sh
            - -ec
            - test ON = "$(mysql -e "SHOW STATUS LIKE 'wsrep_ready';" -N -B | sed 's/wsrep_ready\t//')"
          initialDelaySeconds: 30
        livenessProbe:
          exec:
            command:
            - mysql
            - -e
            - "SHOW DATABASES;"
          # might need to be tweaked for large initial state transfers
          initialDelaySeconds: 60
        volumeMounts:
        - name: mysql
          mountPath: /data
        - name: conf
          mountPath: /etc/mysql/conf.d
        - name: initdb
          mountPath: /docker-entrypoint-initdb.d
      - name: metrics
        image: prom/mysqld-exporter:v0.12.1@sha256:9fe9938c4ac9216cc24005144338f14fac4f604f139b481cc541bead008db3c1
        env:
        - name: DATA_SOURCE_NAME
          value: root@(localhost:3306)/
        ports:
        - containerPort: 9104
      volumes:
      - name: conf
        emptyDir: {}
      - name: conf-readonly
        configMap:
          name: conf-d
      - name: initdb
        emptyDir: {}
  volumeClaimTemplates:
  - metadata:
      name: mysql
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi
