Problem with connection to apache after scaling up, my stateful database on Kubernetes

Viewed 124

Hiii!!!

i have deployed to Kubernetes keyrock, apache and mysql..

After i used the hpa and my stateful database scaled up, i can't login to my simple site.. This is my sql code:

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 #50
              cpu: 50m
            limits:
                memory: 500Mi #220?
                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 # MARK P
                  key: password
            - name: MYSQL_ROOT_HOST
              valueFrom:
                secretKeyRef:
                  name: mysql-secret # MARK P
                  key: host                  
  volumeClaimTemplates:
    - metadata:
        name: mysql-storage
      spec:
        accessModes:
            - ReadWriteOnce
        storageClassName: standard #?manual
        resources:
            requests:
                storage: 5Gi

And it's headless service:

# Headless service for stable DNS entries of StatefulSet members.
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


       

Anyone can help me?

I'm using gke.. Keyrock and apache are deployments and mysql is statefulset..

Thank you!!

1 Answers

You can't just scale up a Standalone database. Using HPA for stateless application works but not for stateful applications like a database.

Increasing replica of your StaetefulSet will just create another pod with a new MySQL instance. This new replica isn't aware of the data in your old replica. Basically, you now have completely two different databases. That's why you can't login after scaling up. When your request get routed to the new replica, this instance does not have the user info that you created in the old replica.

In this case, you should deploy your database in clustered mode. Then, you can take advantage of horizontal scaling.

I recommend to use a database operator like mysql/mysql-operator, presslabs/mysql-operator, or KubeDB to manage your database in Kubernetes. Out of these operators, KubeDB has autoscaling feature. I am not sure that other operators provide this feature.

Disclosure: I am one of the developer of KubeDB operator.

Related