mysqlkubernetesreplicationhpastatefulset

Kubernetes - How to use statefulsets for my Stateful database?


I have a cluster on gke with apache, mysql and keyrock and i would like to scale it up with horizontal pod autoscaler.

For mysql i am using statefulset and the code is here:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  serviceName: mysql
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:5.7.21
          imagePullPolicy: Always
          resources:
            requests:
              memory: 50Mi 
              cpu: 50m
            limits:
                memory: 500Mi 
                cpu: 400m #65  
          ports:
          - containerPort: 3306
            name: mysql
          volumeMounts:
            - name: mysql-storage
              mountPath: /var/lib/mysql
              subPath: mysql
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-secret 
                  key: password
            - name: MYSQL_ROOT_HOST
              valueFrom:
                secretKeyRef:
                  name: mysql-secret 
                  key: host                  
  volumeClaimTemplates:
    - metadata:
        name: mysql-storage
      spec:
        accessModes:
            - ReadWriteOnce
        storageClassName: standard 
        resources:
            requests:
                storage: 5Gi

and mysql-service code:

apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql  
spec:
  ports:
  - name: mysql
    port: 3306
  clusterIP: None
  selector:
    app: mysql  


           

Problem: When hpa scales up mysql database, the second replica is empty and i don't know how to synchronize the 2 replicas!

Any ideas?


Solution

  • By default a StatefulSet will not handle the issue of replication.

    You will have to resort to MySQL kubernetes operator that handles application specific logic such as the one you are seeking for.

    https://github.com/mysql/mysql-operator