Dipesh Majumdar

Blog and Paintings

Volumes, Storageclasses, pvc

First of all, if you are creating a hostpath volume within a pod of type Dir and it looks like this...

  - name: test-volume
      path: /etc/some/path  #MIGHT NOT WORK
      type: Directory

it might not work because that path /etc/some/path might not be present in your node file system. 

  - name: test-volume
      path: /sys  #WORKED FOR ME
      type: Directory

so i created one like this /sys which might be present in the node file sytem and then only it started to work.  Whether this is something you should do is another debate. This blog is not responsible for anything that breaks in production systems. So adopt caution!!!

Now coming to the second point ->

After a lot of hard work I managed to write a volume from yaml file. I was a bit surprised to see that there is no way you create a volume imperatively - k create pv ...blah blah... so here is the yaml manifest of the pv

[dipesh.majumdar@demo ~]$ cat pv.yaml
apiVersion: v1
kind: PersistentVolume
  name: myvolume
 storageClassName: normal
  path: /etc/foo
  - ReadWriteOnce
  - ReadWriteMany
  storage: "10Gi"

But then i got intriged by the storageClassName, becausae i knew that I had my storageClass somewhere in the cluster but then i soon discovered - i do have and it is standard and it DOES NOT HAVE A NAMESPACE


STORAGECLASS IS NAMESPACE FREE - Imprint that in memory my dear readers!!!

anyway we will visit that later....

so i create my pv.yaml

[dipesh.majumdar@demo ~]$ k create -f pv.yaml
persistentvolume/myvolume created

[dipesh.majumdar@demo ~]$ k get pv
myvolume   10Gi       RWO,RWX        Retain           Available           normal                  1m

Looks good so far...

I dig deeper... and I find that this storageclassName is not the storageClass i was talking about in the beginning but it specifies a class which can be used for association with pvc for example... 

it is like a label

[dipesh.majumdar@demo ~]$ k get sc normal
Error from server (NotFound): "normal" not found
[dipesh.majumdar@demo ~]$ k get sc
NAME                 PROVISIONER            AGE
standard (default)   3d

Only PVs of the requested class, ones with the same storageClassName as the PVC, can be bound to the PVC.

[dipesh.majumdar@demo ~]$ cat pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
 name: mypvc
 storageClassName: normal
  - ReadWriteOnce
    storage: 4Gi


[dipesh.majumdar@demo ~]$ k get pvc
mypvc   Bound    myvolume   10Gi       RWO,RWX        normal         1m
[dipesh.majumdar@demo ~]$

ok so now its time to create a pod and associate the volume just created with it and make the volume belong to pvc and not configMap or secret which is also possible

$ k run busybox --restart=Never --image=busybox --dry-run -o yaml --  sleep 3600 >p.yaml

Edit the p.yaml with the yellow highlights ->

apiVersion: v1
kind: Pod
  creationTimestamp: null
    run: busybox
  name: busybox
   - name: myvolume #this volume can be named xyz and is not the pv we created earlier
       claimName: mypvc
  - args:
    - sleep
    - "3600"
    image: busybox
    name: busybox
     - mountPath: /etc/foo #this path need not match with pv hostpath we created earlier
       name: myvolume
#this volume can be named xyz and is not the pv we created earlier
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}


Makes a lot of sense now. 

Let me enter something into this directory now by getting inside the pod - >

[dipesh.majumdar@demo ~]$ k exec -it busybox -- sh
/ # pwd
/ # cd /etc/foo
/etc/foo # ls
/etc/foo # date > test.txt
/etc/foo # cat test.txt
Sun Apr  7 09:55:18 UTC 2019
/etc/foo # pwd


I will copy the /etc/foo/test.txt to my local

[dipesh.majumdar@demo ~]$ k cp mns/busybox:etc/foo/test.txt /home/dipesh.majumdar
[dipesh.majumdar@demo ~]$ ls test.txt
[dipesh.majumdar@demo ~]$ cat test.txt
Sun Apr  7 09:55:18 UTC 2019

Copying from local to pod

[dipesh.majumdar@demo ~]$ date >test2.txt
[dipesh.majumdar@demo ~]$ k cp test2.txt mns/busybox:etc/foo/
[dipesh.majumdar@demo ~]$ k exec -it busybox -- ls -ltra /etc/foo/
total 12
drwxr-xr-x    1 root     root          4096 Apr  7 09:53 ..
-rw-r--r--    1 root     root            29 Apr  7 09:55 test.txt
-rw-rw-r--    1 1000     1001            29 Apr  7 10:05 test2.txt
drwxr-xr-x    2 root     root            80 Apr  7 10:05 .
[dipesh.majumdar@demo ~]$


fire you memory - 

spec of pv has 4 items - storageClassName, accessModes,, hostPath.path

spec of pvc has 3 items -  storageClassName, accessModes,

notes - 

when the pv is just created status of pv is available. when it is attached to pvc it becomes bound. Deleting pvc makes status of pv as released

[dipesh.majumdar@demo ~]$ k delete pvc mypvc
persistentvolumeclaim "mypvc" deleted
[dipesh.majumdar@demo ~]$ k get pv
myvolume   10Gi       RWO,RWX        Retain           Released   mns/mypvc   normal                  1h
[dipesh.majumdar@demo ~]$

the volume name inside pod is not the persistent volume name but more pod specific.... very important point

persistentVolumeReclaimPolicy: Retain #means pv will be retained even after pvc attached to it is deleted until pv is manually deleted

                                                                         Delete #deleted automatically freeing up storage

                                                                         Recycle #data in the volume will be deleted before it is made available to other pvcs

Go Back