How to create a directory inside a PVC and then push a file into it in kubernetes?

4/15/2021

I am working with IBM Kubernetes Service and need to create a directory in the PVC. There are 2 PVCs which are already created. Can I use the same to create a directory or shall I create a new one as those two are already bound?

Also, I tried creating new PV using a PV yaml file, but it is getting killed as soon as I try to create it. Then, I tried creating a PVC directly that refers to the storage class we have. But, the same is happening here as well. Can this be because of some access issue with NFS or what else might be the reason?

Once the directory is created, I also need to push files into it. Need some help with that as well.

-- Muskan Sharma
ibm-cloud
iks
kubernetes
kubernetes-pvc
persistent-volumes

2 Answers

4/19/2021

I have sample PV, PVC code

vi pv.yml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-xfusion
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 8Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/itadmin"
kubectl create -f pv.yml

sudo vi pvc.yml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-xfusion
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 4Gi


kubectl create -f pvc.yml


vi sudo pod.yml


apiVersion: v1
kind: Pod
metadata:
  name: pod-xfusion
spec:
  volumes:
    - name: storage-xfusion
      persistentVolumeClaim:
        claimName: pvc-xfusion
    - name: container-xfusion
      image: nginx:latest
      ports:
        - containerPort: 80
          
      volumeMounts:
        - mountPath: "/var/www/html"
          name: storage-xfusion


kubectl create -f pod.yml

check this might help which you are asking access modes

-- lavanya
Source: StackOverflow

4/16/2021

A PVC is a linkage between a volume (usually on a Block or NFS device) and your Kubernetes pod. Typically, when you bind a PVC to the pod, you mount it on a specific path within a container in the pod.

So, if you bound a PVC and mounted it to your container at /foo, then you should be able to write files directly into that folder. For example, if you were running a bash script, you could echo "this is some text" > /foo/test.txt and the text "this is some text" should end up in a file called test.txt within the /foo folder. Because that's pointing at your external storage volume via the PVC, the data would persist to that location.

Remember though, block devices are generally only allowed to mount to a single pod, so if you were running three instances of your deployment, then you'd be reading and writing from three separate storage locations.

One way to work around this limitation is to use NFS (File) storage, but this will likely result in poor performance if you are reading and writing heavily. If you find yourself doing this, then file storage is probably not what you want and you should explore databases or object stores instead.

-- Matt Hamann
Source: StackOverflow