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

5k views Asked by At

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.

2

There are 2 answers

1
Matt Hamann On

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.

3
lavanya On

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