-1

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?

alex
  • 343
  • 2
  • 9
  • 1
    Please only ask **programming** related questions on SO! – Shadow Sep 25 '21 at 16:35
  • @alex have you checked that mysql is a kind of software that can run in multiple instances? if so, have you checked how it is run in a distributed mode? – Jonas Sep 25 '21 at 16:48
  • Not sure if you have already referred to this page https://kubernetes.io/docs/tasks/run-application/run-replicated-stateful-application/ You need to write down an init container to do the replication task for you, even better use a Kubernetes operator https://github.com/mysql/mysql-operator – hellodk Sep 25 '21 at 17:29
  • @hellodk Do you believe that if i write down an init container, it will be fine? – alex Sep 25 '21 at 19:11
  • @Jonas thank you for your answer!! i have checked that mysql can run in multiple instances but i can't understand how this could solve my problem.. – alex Sep 25 '21 at 19:15

2 Answers2

1

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

pkaramol
  • 16,451
  • 43
  • 149
  • 324
  • Thank you for your answer!! Do you believe that i can't avoid operator? i tried to use an operator but i couldn't connecti mysql with keyrock! – alex Sep 25 '21 at 19:12
  • I think operators are meant to solve those kind of issues; otherwise you have to address replication yourself and that would be a lot of work. This is what operators are for, to handle application (domain) - specific logic – pkaramol Sep 25 '21 at 20:22
  • Thank you again.. Could you answer me to another question? if i will use operators, should i use and statefulsets too? Also i have to create an InnoDB Cluster too? Maybe only the Operator? – alex Sep 25 '21 at 21:25
  • I have not used the specific operator but most likely yes, it will create a StatefulSet (or whatever is required under the hood) for you. Same for the InnoDB cluster – pkaramol Sep 25 '21 at 21:35
  • And will i have to delete the code i wrote above or i will keep it? I have to read more about Operators, because i haven't understand very well how it works.. – alex Sep 25 '21 at 21:47
  • 1
    @alex operators are the correct way to go, if possible please use them. You can refer to this MySQL Operator - https://github.com/cybozu-go/moco – hellodk Sep 27 '21 at 10:02
  • Thank you.. i will use operators.. And if i don't want to sxale up my database, should i use statefulsets or just Deployment?? – alex Sep 29 '21 at 20:58
0

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

Thulasya
  • 11
  • 3
  • How does this solve the problem to run two instances of MySQL on Kubernetes? - it will just be two different databases with inconsistent data. – Jonas Sep 25 '21 at 18:37