Home > Software engineering >  Kubernetes - Replicated stateful mysql database
Kubernetes - Replicated stateful mysql database

Time:09-26

Helloo

This is the scenario:

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  #x-app  #
spec:
  ports:
  - name: mysql
    port: 3306
  clusterIP: None
  selector:
    app: mysql  #x-app


           

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

Does anyone have any idea? Thank youu!!

CodePudding user response:

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

CodePudding user response:

Default kubernetes pod create, not sync data between scaled pods. You can use kubernetes init container or common storage or any script to do so

  • Related