Answers: <p>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).</p>
  <p>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.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-05-14T15:53:18.813Z
Answers: 2022-05-14T15:53:18.813Z
Answers: hazmei
Body: <p>I am using <em>K8S</em> with <em>Helm 3</em>.</p>
  <p>I am also using <em>MYSQL 5.7</em> database.</p>
  <p>How can I create <em>MYSQL</em> 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?</p>
  <p>I am using <code>PersistentVolume</code> and <code>PersistentVolumeClaim</code>.</p>
  <p>Here are the <em>YAML</em> files:</p>
  <p><strong>Mysql pod:</strong></p>
  <pre><code>apiVersion: v1
  kind: Pod
  metadata:
    name: myproject-db
    namespace: {{ .Release.Namespace }}
    labels:
      name: myproject-db
      app: myproject-db
  spec:
    hostname: myproject-db
    subdomain: {{ include &quot;k8s.db.subdomain&quot; . }}
    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
  </code></pre>
  <p><strong>Persistent Volume:</strong></p>
  <pre><code>apiVersion: v1
  kind: PersistentVolume
  metadata:
    name: mysql-pv
    labels:
      type: local
      name: mysql-pv
  spec:
    capacity:
      storage: 5Gi
    accessModes:
      - ReadWriteOnce
    hostPath:
      path: &quot;/data/mysql&quot;
  </code></pre>
  <p><strong>Persistent Volume Claim:</strong></p>
  <pre><code>apiVersion: v1
  kind: PersistentVolumeClaim
  metadata:
    name: mysql-pvc
  spec:
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 5Gi
    volumeMode: Filesystem
    volumeName: mysql-pv
  </code></pre>
  <p>Also, created a storage class. It is not used, but here it is:</p>
  <p><strong>Storage Class:</strong></p>
  <pre><code>apiVersion: storage.k8s.io/v1
  kind: StorageClass
  metadata:
    name: mysql-storage
  provisioner: docker.io/hostpath
  volumeBindingMode: WaitForFirstConsumer
  </code></pre>
  <p>after running:</p>
  <p>Helm install myproject myproject/</p>
  <p>The database is created, and can be used.</p>
  <p>If I add records and stop and remove the database pod, I would like that the records will be kepts - no loss of db.</p>
  <p>Instead, I see that the db data is lost when the MYSQL pod is restarted.</p>
  <p>What can I do in order to use <code>helm install ...</code> or <code>helm upgrade</code> (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?</p>
  <p>Thanks.</p>
  
Tags: <kubernetes><helm><mysql><persistent-volumes>
Title: K8S + HELM. Create a persistence volume for mysql database
UserDisplayName: Eitan
ThreadText: # K8S + HELM. Create a persistence volume for mysql database
  
  (Asked by: username_0 on 2022-04-18)
  
  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.
  
  **Tags:** kubernetes, helm, mysql, persistent-volumes
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-05-14. Score: 1 | Accepted answer
  
  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.
  
  
  ---
  
  