apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysqlc
  namespace: edoc2
  labels:
    app: mysqlc
    k8s-app: mysqlc
spec:
  selector:
    matchLabels:
      app: mysqlc
  replicas: 3
  revisionHistoryLimit: 5
  serviceName: mysqlc
  template:
    metadata:
      labels:
        app: mysqlc
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9104"
    spec:
      #hostname: mysql
      #terminationGracePeriodSeconds: 30
      tolerations:
      - key: node-role.kubernetes.io/master
        effect: NoSchedule
      initContainers:
      - name: init-mysql
        image: registry.edoc2.com:5000/edoc2v5/mysql:8.0.12
        command:
        - bash
        - "/mnt/conf/int-mysql.sh"
        #- "-c"
        #- |
        #  set -ex
        #  [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
        #  ordinal=${BASH_REMATCH[1]}
        #  echo [mysqld] > /etc/mysql/conf.d/server-id.cnf
        #  echo server-id=$((100 + $ordinal)) >> /etc/mysql/conf.d/server-id.cnf
        #  echo loose-group_replication_local_address=`hostname`:24901 >>/etc/mysql/conf.d/server-id.cnf
        #  echo report_host=`hostname` >>/etc/mysql/conf.d/server-id.cnf
        #  cp /mnt/conf/mysql-conf.cnf /etc/mysql/conf.d/
        #  mkdir -p /var/lib/mysql/{data,tmp,blog,rlog,ulog}
        #  chown -R mysql:mysql /var/lib/mysql
        volumeMounts:
          - name: config-volume
            mountPath: /mnt/conf/
            readOnly: true
          - name: conf
            mountPath: /etc/mysql/conf.d/
          - name: mysql-data
            mountPath: /var/lib/mysql/
      containers:
      - name: mysql-container
        #image: registry.edoc2.com:5000/edoc2v5/mysql:prod620-v8.0.17.18
        #image: mysql:8.0.12
        image: registry.edoc2.com:5000/edoc2v5/mysql:8.0.12
        resources:
          limits:
            cpu: "2"
          requests:
            memory: "2048Mi"
        volumeMounts:
          - name: conf
            mountPath: /etc/mysql/conf.d/
          - name: mysql-data
            mountPath: /var/lib/mysql/
          #- mountPath: /dbbackup
          #  name: mysql-dbbackup-volume
        ports:
          - containerPort: 3306
            name: client
          - containerPort: 24901
        env:
          - name: TZ
            value: "Asia/Shanghai"
          - name: MYSQL_ROOT_PASSWORD
            valueFrom:
              secretKeyRef:
                key: mysql-root-password
                name: mysql-secret
          - name: MYSQL_INITDB_SKIP_TZINFO
            value: "1"
        #- name: TASKS
        #  value: "10 0 * * *"
        #- name: DAYS
        #  value: "15"
        #- name: MODE
        #  value: "cluster"
      - name: mysql-exporter
        env:
          - name: DATA_SOURCE_NAME
            value: exporter:018P4NUDZNaqeStsSkmO0A@tcp(127.0.0.1:3306)/performance_schema
        image: registry.edoc2.com:5000/edoc2v5/mysqld-exporter:v0.11.0
        ports:
          - containerPort: 9104
            name: exporter
      volumes:
        - name: config-volume
          configMap:
            items:
            - key: mysql-conf.cnf
              path: mysql-conf.cnf
            - key: int-mysql.sh
              path: int-mysql.sh
            name: mysqlc
        - name: conf
          emptyDir: {}
      nodeSelector:
        "node.labels.nodelabels": Middleware
  volumeClaimTemplates:
  - metadata:
      name: mysql-data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "local-mysql-storage"
      resources:
        requests:
          storage: 50Gi 
      #selector:
      #   matchLabels:
      #   volume-type: mysql
  #- metadata:
  #    name: mysql-dbbackup-volume
  #  spec:
  #    accessModes: [ "ReadWriteOnce" ]
  #    storageClassName: "backup-mysql-storage"
  #    resources:
  #      requests:
  #        storage: 50Gi
