Devil_____03
Devil_____03

Reputation: 19

Does PersistentStorage in Kubernetes store the content to DISK or only on RAM

I am a beginner in Kubernetes and I am learning about Persistent Volume. Currently my knowledge is that the Persistent Volume will be preserved even after the pod is deleted, but only if we delete the Persistent Volume or stop that node(in my case I only ran a single node in kubernetes cluster on my local machine). I also noticed that the files I write to that volume from my pods were not written to my disk, for reference the below is my pv.yaml file,

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv-name
  namespace: my-namespace
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 25Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /home/me/dir

and this is my pvc.yaml file,

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc-name
  namespace: my-namespace
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests: 
      storage: 25Gi

kindly correct me if I have any errors in the yaml specification. Now I have some doubts in it,

  1. How can I write to my local machines disk from the pod, how can I create a volume that will write to my disk rather than only in nodes filesystem(since the cluster was running on local machine).
  2. Even if(assuming my current state) it is not stored on disk, but being written on the volume mounted path, will it be removed from the RAM, since it was written to Persistence Storage
  3. If it was removed from RAM after being written to Persistence Storage path, how can it be not available when checking in that local filesystem directory and how can it be not available if the node is stopped or the volume is deleted.

I tried changing the storageClassName to local-storage and created a storageClass.yaml file, which is

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer

but still I couldn't see any files being written to my local machines disk

(Edited)

I am using flyte's task decorator and PodTemplate to mount my volume like,

@task(
    cache=False,
    requests=Resources(mem="1Gi", storage="25Gi"),
    pod_template=PodTemplate(
        primary_container_name="my-task",
        pod_spec=V1PodSpec(
            containers=[
                V1Container(
                    name="my-task",
                    image="my-image",
                    volume_mounts=[V1VolumeMount(mount_path="/data", name="data", read_only=False)]
                )
            ],
            volumes=[
                V1Volume(
                    name="data",
                    persistent_volume_claim=V1PersistentVolumeClaimVolumeSource(
                        claim_name="my-pvc-name",
                        read_only=False
                    )
                )
            ],
        )
    ),
)

Upvotes: -1

Views: 287

Answers (2)

David Maze
David Maze

Reputation: 159242

Delete the PersistentVolume object entirely (and, unless you specifically know otherwise, the StorageClass too). The cluster will create the PersistentVolumeClaim and the underlying storage for you. You won't be able to directly access it from outside the cluster, but it will be backed by disk.

In the environments I work on, for example, the cluster will automatically provision an AWS EBS volume to hold the data, and that volume will follow the Pod around the cluster. If a Node happens to get destroyed, the data will still be in the external volume, and the cluster will remount it to a different Node when a new Pod is created and scheduled.


Since Kubernetes is a multi-node clustered environment, you should never expect files to be written to your local disk. Consider using plain Docker or even running the process directly on your host, if you specifically need local files on a single-system setup.

Correspondingly, you should almost never use a hostPath: volume, since that names a directory on whichever node the Pod happens to be running on. If the Pod is deleted and recreated on a different node (extremely routine) the data won't follow it, and if the original node is deleted (also fairly routine) the data will be destroyed as well.

Finally, in normal use you don't need to manually create PersistentVolume objects at all. The cluster should have a storage provisioner that sees the PersistentVolumeClaim and creates matching PersistenVolume with matching storage.

Upvotes: 1

David Espejo
David Espejo

Reputation: 11

How are you mounting the volume to the Pod?

It should be something like

apiVersion: v1
kind: Pod
metadata:
  name: pod-with-pv
  namespace: my-namespace
spec:
  containers:
    - name: app-container
      image: nginx:latest #change to your image
      volumeMounts:
        - name: data-volume
          mountPath: /data
  volumes:
    - name: data-volume
      persistentVolumeClaim:
        claimName: my-pvc-name

Also, are you using Flyte?

Upvotes: 1

Related Questions