PV file not saved on host

10/12/2019

hi all quick question on host paths for persistent volumes

I created a PV and PVC here

apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

and I ran a sample pod

apiVersion: v1
kind: Pod
metadata:
  name: task-pv-pod
spec:
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: task-pv-claim
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: task-pv-storage

i exec the pod and created a file

root@task-pv-pod:/# cd /usr/share/nginx/html
root@task-pv-pod:/usr/share/nginx/html# ls
tst.txt

However, when I go back to my host and try to ls the file , its not appearing. Any idea why? My PV and PVC are correct as I can see that it has been bounded.

ubuntu@ip-172-31-24-21:/home$ cd /mnt/data
ubuntu@ip-172-31-24-21:/mnt/data$ ls -lrt
total 0
-- adr
kubernetes

2 Answers

10/14/2019

First of all, you don't need to create a PV if you are creating a PVC. PVCs create PV, if you have the right storageClass.

Second, hostPath is one delicate PV in Kubernetes world. That's the only PV that doen't need to be created to be mounted in a Pod. So you could have not created neither PV nor PVC and a hostPath volume would work just fine.

To make a test, delete your PV and PVC, and create your Pod like this:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-volume
  labels:
    app: nginx
spec:
  containers:
  - image: nginx
    name: nginx
    securityContext:
      privileged: true
    ports:
    - containerPort: 80
      name: nginx-http
    volumeMounts:
    - name: nginx
      mountPath: /root/nginx-volume   # path in the pod
  volumes:
  - name: nginx
    hostPath:
      path: /var/test             # path in the host machine

I know this is a confusing concept, but that's how it is.

-- suren
Source: StackOverflow

10/14/2019

A persistent volume (PV) is a kubernetes resource which has its own lifecycle independent of the pod pv documentation. Using a PVC to consume from a PV makes it visible in some other tool. For example azure files, ELB, a server with NFS, etc. My point here is that there is no reason why the PV should exist in the node.

If you want your persistence to be saved in the node use the hostPath option for PVs. Check this link. Though this is not a good production practice.

-- Rodrigo Loza
Source: StackOverflow