K8S + HELM. Create a persistence volume for mysql database



  • I am using K8S with Helm 3.

    I am also using MYSQL 5.7 database.

    How can I create MYSQL pod, that the database will persist (created on first time) after first time the pod is created, and even the pod is down and up again, the data won't be lost?

    I am using PersistentVolume and PersistentVolumeClaim.

    Here are the YAML files:

    Mysql pod:

    apiVersion: v1
    kind: Pod
    metadata:
      name: myproject-db
      namespace: {{ .Release.Namespace }}
      labels:
        name: myproject-db
        app: myproject-db
    spec:
      hostname: myproject-db
      subdomain: {{ include "k8s.db.subdomain" . }}
      containers:
        - name: myproject-db
          image: mysql:5.7
          imagePullPolicy: IfNotPresent
          env:
            MYSQL_DATABASE: test
            MYSQL_ROOT_PASSWORD: 12345
          ports:
            - name: mysql
              protocol: TCP
              containerPort: 3306
          resources: 
            requests: 
              cpu: 200m
              memory: 500Mi
            limits:
              cpu: 500m
              memory: 600Mi
          volumeMounts:
            - name: mysql-persistence-storage
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-persistence-storage
          persistentVolumeClaim:
            claimName: mysql-pvc
    

    Persistent Volume:

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: mysql-pv
      labels:
        type: local
        name: mysql-pv
    spec:
      capacity:
        storage: 5Gi
      accessModes:
        - ReadWriteOnce
      hostPath:
        path: "/data/mysql"
    

    Persistent Volume Claim:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: mysql-pvc
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 5Gi
      volumeMode: Filesystem
      volumeName: mysql-pv
    

    Also, created a storage class. It is not used, but here it is:

    Storage Class:

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: mysql-storage
    provisioner: docker.io/hostpath
    volumeBindingMode: WaitForFirstConsumer
    

    after running:

    Helm install myproject myproject/

    The database is created, and can be used.

    If I add records and stop and remove the database pod, I would like that the records will be kepts - no loss of db.

    Instead, I see that the db data is lost when the MYSQL pod is restarted.

    What can I do in order to use helm install ... or helm upgrade (it is important that the pod created by helm command) and only after first time it will create the database, and for the next times the database data won't be lost?

    Thanks.



  • It seems that you are using the host file system for the persistent volume. Assuming that you have multiple worker nodes and the pod restarts on a different node, the data is “lost” (only exists in the node that it ran on previously).

    Force the pod to run on 1 particular node and restart the pod. You’ll notice that the data is still there as it restarts on the same node as before.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2