1 year ago

#297975

test-img

Axel

Kubernetes PersistentVolume data is lost after physical machine restart

I recently started using PersistentVolumes and PersistenVolumeClaims for persistent storage in my container.

My end goal is to make sure that a file in a specific folder (let's call it data) on the container survives any restart. The implementation is:

  1. Creation of the PersistentVolume
  2. Creation of the PersistentVolumeClaim that automatically binds to the PV thanks to a Storage Class
  3. Creation of a simple deployment that pulls a single image from a remote repository

The deployment starts correctly. I am able to connect to the only container and create a file in data

If i delete and recreate the deployment, i can see that it pulls the image, recreates the pod and the container, and by reconnecting to the container i see the file previously created in the folder data. This is expected behaviour.

If i restart my PC, I can see the deployment automatically starting the pods, and both the PV and PVC are still there and working.

However, if I access the data folder, i can't see the previously created file. So, my questions are:

1. Are PersistentVolumes meant to persist even after a hard machine restart?

2. If so, how can I achieve this?

I understand that I am missing a lot of theory on how Kubernetes works, but the official documentation doesn't cover this, and I couldn't find any useful information online. If you need any YAML file of the entities just tell me. Thanks.

EDIT:

Yaml config of both the PV and PVC:

apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: vl0001
    spec:
      storageClassName: my-sc
      capacity:
        storage: 2Gi
      accessModes:
        - ReadWriteOnce
      local:
        path: /mnt
      nodeAffinity:
        required:
          nodeSelectorTerms:
          - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
              - node0001

PVC:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc0001
spec:
  storageClassName: my-sc
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

kubernetes

persistent-volumes

persistent-storage

0 Answers

Your Answer

Accepted video resources