Persistent volume and Persistent volume claim

I have created the following PV, PVc and pod .
For the Persistent volume i have chosen the /opt directory of the host machine as the volume.
After creating the pod i logged in inside the pod and created a file text.txt under /etc/lala directory ,and came out of the pod.

Question) Will this file, text.txt, be now available in the /opt directory of the host machine ?

Following is the Persistent Volume yaml file.

apiVersion: v1
kind: PersistentVolume
metadata:
name: mypv
spec:
accessModes:
- ReadWriteMany
capacity:
storage: 1G
storageClassName: normal
hostPath:
path: /opt

Following is the Persistent Volume claim yaml file

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mypvc
spec:
accessModes:
- ReadWriteMany
storageClassName: normal
resources:
requests:
storage: 1G

Following is the Pod yaml file .

apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:

  • image: nginx
    name: nginx
    ports:
    • containerPort: 80
      resources: {}
      volumeMounts:
      • name: myvol
        mountPath: /etc/lala
        dnsPolicy: ClusterFirst
        restartPolicy: Always
        volumes:
    • name: myvol
      persistentVolumeClaim:
      claimName: mypvc
      status: {}

In order to understand and debug your code, could you please format it a bit better. Use the </> key to generate a code block. This will

Help preserve:
  - proper indentation
  - dashes won't turn into ° or ▪️
  - Quotes won't turn into smart quotes (which don't work)

Thank you Sir for the reply.

I tried and every thing worked well.
only when i created the text.txt file inside the pod using the command " echo hello > text.txt " the file was created but after logging out of the pod when i checked the /opt directory of my host machine the file was not there.
Where can i find the same.

All this dots, circles and squares are dashes(-). This platform is automatically converting dashes to dots and circles.
You can also see the yaml file in the following git repo :
Deepakranjanmishra/DevOps_Class/tree/main/MY_KUBERNETES/PV-PVC

Following is the Pod yaml file .

apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:

  • image: nginx
    name: nginx
    ports:
    • containerPort: 80
      resources: {}
      volumeMounts:
      • name: myvol
        mountPath: /etc/lala
        dnsPolicy: ClusterFirst
        restartPolicy: Always
        volumes:
    • name: myvol
      persistentVolumeClaim:
      claimName: mypvc
      status: {}

Code is still garbled.

Sir i have replied back in the email. Please take a look at the email.

I haven’t received mail from you, sorry.

Here’s a tutorial how to enter code correctly in Discourse, the forum software we’re using. I know it’s a bother, but it’s a skill worth learning.

If you press the button indicated by Rob, you can paste your code correctly, like this

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: nginx
    name: nginx
    ports:
    - containerPort: 80
    resources: {}
    volumeMounts:
      - name: myvol
        mountPath: /etc/lala
  dnsPolicy: ClusterFirst
  restartPolicy: Always
  volumes:
    - name: myvol
      persistentVolumeClaim:
        claimName: mypvc
status: {}

I have tried your code from the git repo and it does what you expect.

controlplane ~ ➜  k get po
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          12s

controlplane ~ ➜  k exec -it nginx -- bash
root@nginx:/# cd /etc/lala
root@nginx:/etc/lala# echo "hi" > test.txt
root@nginx:/etc/lala# ls -l
total 4
-rw-r--r-- 1 root root 3 Jun 19 05:24 test.txt
root@nginx:/etc/lala# 
exit

controlplane ~ ➜  ls -l /opt
total 4
-rw-r--r--    1 root     root             3 Jun 19 05:24 test.txt

controlplane ~ ➜  cat /opt/test.txt 
hi

controlplane ~ ➜ 

If your cluster has more than one node, then whichever node the PV binds to is where the file will end up.

1 Like

Yes i will, Thank You.
Alistair Sir has provided the solution i was asking for.

Thank you very much Sir , this was what i was trying to know.
Sir , in the end you have said " If your cluster has more than one node, then whichever node the PV binds to is where the file will end up. "
How will we know that which node has the PV binded ?
Any other such information on PV and PVC if you can kindly share.

It will be created when the PVC is claimed by the pod.

controlplane ~ ➜  k get po -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP           NODE     NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          8s    10.244.2.2   node02   <none>           <none>

controlplane ~ ➜  k exec -it nginx -- bash
root@nginx:/# cd /etc/lala/
root@nginx:/etc/lala# touch test.txt
root@nginx:/etc/lala# ls -l
total 12
drwxr-xr-x 1 root root 4096 Apr 18 13:10 cni
drwx--x--x 4 root root 4096 Apr 18 13:10 containerd
-rw-r--r-- 1 root root    0 Jun 19 18:32 test.txt
root@nginx:/etc/lala# 
exit

controlplane ~ ✖ ssh node02
Last login: Wed Jun 19 18:31:54 2024 from 192.34.45.9

node02 ~ ➜  cd /opt

node02 /opt ✖ ls -l
total 12
drwxr-xr-x 1 root root 4096 Apr 18 13:10 cni
drwx--x--x 4 root root 4096 Apr 18 13:10 containerd
-rw-r--r-- 1 root root    0 Jun 19 18:32 test.txt

node02 /opt ➜  

As you can see here, there are already 2 directories on the node at /opt.
You should ideally create hostpath volumes for directories that won’t already exist on the host (like '/var/my-hostpath`), or the container can modify host files which is not necessarily what you want.

1 Like

Beautiful explanation Sir. Thanks a lot.
Deepak.