K8S PersistenVolume and PersistentVolumeClaim

2/21/2019

I am practicing k8s on storage topic. I don't understand why step2: PersistentVolume has different storage size when the tutorial configures PersistenVolumeClaim in step3

For example nfs-0001.yaml, nfs-0002.yaml. storages are 2Gi and 5Gi

apiVersion: v1
kind: PersistentVolumemetadata:
  name: nfs-0001
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    server: 172.17.0.7
    path: /exports/data-0001
apiVersion: v1
kind: PersistentVolume
metadata:  name: nfs-0002
spec:  
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    server: 172.17.0.7
    path: /exports/data-0002

Example in step3 : pvc-mysql.yaml, pvc-http.yaml

kind: PersistentVolumeClaim
apiVersion: v1metadata:
  name: claim-mysql
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: claim-http
spec:  
  accessModes:
    - ReadWriteOnce  
  resources:
    requests:      
      storage: 1Gi

And when I check the pv and pvc

master $ kubectl get pvc
NAME          STATUS    VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
claim-http    Bound     nfs-0001   2Gi        RWO,RWX                       17m
claim-mysql   Bound     nfs-0002   5Gi        RWO,RWX                       17m
master $ kubectl get pv
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM                 STORAGECLASS   REASON    AGE
nfs-0001   2Gi        RWO,RWX        Recycle          Bound     default/claim-http        19m
nfs-0002   5Gi        RWO,RWX        Recycle          Bound     default/claim-mysql        19m

Neither 1Gi and 3Gi shown up in the terminal.

Question:

  1. Where are the 1Gi and 3Gi?

  2. If they are not used. Is it safe to put arbitrary number to storage in PersistenVolumeClaim yaml?

-- Sarit
kubernetes
storage

1 Answer

2/21/2019

You need to understand the difference between PV and PVC's. PVC is a declaration of stroage which that at some point become available for application to use and that is not the actual size of volume allocated.

PV's are the actual volume allocated at the time on the disk and ready to use. In order to use these PVs user needs to create PersistentVolumeClaims which is nothing but a request for PVs. A claim must specify the access mode and storage capacity, once a claim is created PV is automatically bound to this claim.

In your case, you have PV size of 5 and 3 GB respectively and you have started two PVC's with 3 and 1 GB respectively with accessmode: ReadWriteOnce that means there can be only one PV is attached to the one PVC.

Now the capacity of the PV available is the larger than requested and hence it allocated the larger size PV to the PVC.

PVC.spec.capacity is user's request for storage, "I want 10 GiB volume". PV.spec.capacity is actual size of the PV. PVC can bind to a bigger PV when there is no smaller available PV, so the user can get actually more than he wants.

Similarly, dynamic provisioning works typically in bigger chunks. So if user asks for 0.5GiB in a PVC, he will get 1 GiB PV because that's the smallest one that AWS can provision.

There is nothing wrong about it. Also, you should not put any random number in PVC size, it should be well calculated according to your application need and scaling.

-- Prafull Ladha
Source: StackOverflow