This is an old revision of the document!


In Kubernetes and in docker in general, there are several type of storage, but let's focus on the basics here:

  • Dynamic
  • Static

To better illustrate this, check the following pictures:

Now, let's discuss a little bit about the components here. We will start from left to right.

  • Storage

First you have the actual storage, that storage can be cloud (AWS, Google, IBM Cloud etc) or Local (iSCSI, NAS, SAN, bare metal, etc). Kubernetes has a lot of plugins to provide access to that storage. On the other side, that storage can be replicated, hashed, mapped, encrypted, in RAID and so on and so on. From Kubernetes point of view, it is seen simply as a storage thanks to the plugin

  • Plugin

Plugins provide the entry point for Kubernetes, as described above, there are different plugins, like EBS Plugin for Cloud Volume or local for local storage. You can check more about Kubernetes Plugins here.

  • Persistent Volume (PV)

Persistent Volume represent the volumes in Kubernetes term, in other words, the physical representation of the volume gets translated to persistent volume in kubernetes terms. To use that volume, of course we have to use the next component, which is:

  • Persistent Volume Claim (PVC)

Both, PV and PVC are first class objects in Kubernetes, which means, that just like we can GET and DESCRIBE Pods, we can do the same for PV/PVC. It is important to note, that once a PVC has connected to PV, other PVC cannot connect to the same PV.

  • Volume

Lastly, we have the Volumes, which are the PV in the POD. Once the Volume is linked with the PVC which is linked with the PV. That PV can be used only by the containers which are in that pod. Other pods or other containers outside of the pod, cannot use that storage.

So, in a nutshell:

  1. We define the storage
  2. We define the PV
  3. We define the PVC
  4. Lastly, we define the Volume in the pod configuration.

There is a BIG issue with that kinda provisioning, IT DOESN'T SCALE. because of that, we have two type of privisioning:

  • Static
  • Dynamic

So let's get going and see how it is done with each type:

Let's configure Static storage.

Firstly, as we said, we have to configure, the PV, let's take one example of a PV:

PV Specs

kind: PersistentVolume
apiVersion: v1                                 <- It is first Class object, just like Pod, Deployment and so on
metadata:
  name: ps-pv                                  <- The name is completely arbitrary, so choose whatever, but with reason :)
  labels:
    type: local                                <- Local Label
spec:
  storageClassName: ps-fast                    <- Indicate that it is in "ps-fast" storage Class
  capacity:
    storage: 50Gi                              <- We allocate 50 GBs
  persistentVolumeReclaimPolicy: Retain        
#                                   ^
#                                   |
#                   What to happen after you remove a PVC?
#Retain - Keep it in "protected" mode
#Delete(Default) - This will DELETE the PV after removing the PVC
#
  accessModes:
    - ReadWriteOnce           
#         ^
#         |
#There are 3 access modes: 
#ReadWriteOnce - The PV can be taken in RW once by one pod
#ReadWriteMany - Same as above just it can be taken lots of times.
#ReadOnlyMany - RO for a lot of PODs. Not all type support all 3 modes
#For example, block devices don't support ReadWriteMany, but File based volumes (NFS, Object Volumes) usually do. Check your plugin docks
  hostPath:
    path: "/home/ubuntu/volume1"

You can see the description for each important attritube above. So that covers the explanation :) So, how we create it then ? Well, like any other kubernetes' object, we APPLY it:

Create PV

ubuntu@k8s-master:~/volume1$ kubectl apply -f pov1.yml
persistentvolume/ps-pv created
ubuntu@k8s-master:~/volume1$ kubectl get pv
NAME    CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
ps-pv   50Gi       RWO            Retain           Available           ps-fast                 3s
ubuntu@k8s-master:~/volume1$ kubectl get pvc
No resources found in default namespace.
ubuntu@k8s-master:~/volume1$

To use it, we need a PVC, to claim it and pod so we have a container to present it to.

As we already said, PVC “claims” the PV. Without PVC, the PV is useless, without PVC the PV is useless. They need to exist together.

So let's check the specs for the PVC.

PVC specs

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: ps-pvc
spec:
  storageClassName: ps-fast
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 50Gi

I will not go through this description as the attributes repeat, but you get the picture. We create a claim, and we hope that some volume can fulfil it :) As again, the PVC is a first class object, we can simply “apply” it:

Create PVC

ubuntu@k8s-master:~/volume1$ kubectl apply -f pvc.yml
persistentvolumeclaim/ps-pvc created
ubuntu@k8s-master:~/volume1$ kubectl get pvc
NAME     STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
ps-pvc   Bound    ps-pv    50Gi       RWO            ps-fast        3s
ubuntu@k8s-master:~/volume1$ kubectl get pv
NAME    CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM            STORAGECLASS   REASON   AGE
ps-pv   50Gi       RWO            Retain           Bound    default/ps-pvc   ps-fast                 5m8s
ubuntu@k8s-master:~/volume1$

We see that, we have bounded them and we see the claim in the Persistent Volume description. It is important to note that the PVC will bind to ANY PV which has the SAME or MORE of the requested storage. For example if the PVC wants to claim 50 GBs and we have PV with 20 GBs, it will bind.

However, if our PVC wants to claim 20GBs and we have only 50 GB PV, then we are screwed and the PVC won't bind. Congrats, we have a pvc to present to any pod we want to have storage.

So let's create the Pod

As always, here example of the POD YML:

Pod Specs

kind: Pod
metadata:
  name: first-pod
spec:
  volumes:
    - name: fast50g
      persistentVolumeClaim:
        claimName: ps-pvc
  containers:
    - name: ctr1
      image: ubuntu:latest
      command:
      - /bin/bash
      - "-c"
      - "sleep 60m"
      volumeMounts:
      - mountPath: "/data"
        name: fast50g

Again, I think explaination is useless here as the things are self explaining and as always we can just create:

Create Pod

ubuntu@k8s-master:~/volume1$ kubectl apply -f pod.yml
pod/first-pod created
ubuntu@k8s-master:~/volume1$ kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
first-pod                       1/1     Running   0          99s
ubuntu@k8s-master:~/volume1$

As we installed that pod on Kubernetes with 1 master and 2 workers. It didn't had to end up on the mater, in fact it ended up on Worker 1 :) So let's check it there. On Worker1, we can list all the Pods as usual and connect to our one

List all pods

root@node-1:~# docker container ls
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS               NAMES
ca7f18335d32        ubuntu                    "/bin/bash -c 'sleep…"   4 minutes ago       Up 4 minutes                            k8s_ctr1_first-pod_default_d5fc44c3-81c7-4a63-9e00-cf9650c26c58_0
d2e5fddd1d2d        k8s.gcr.io/pause:3.2      "/pause"                 5 minutes ago       Up 5 minutes                            k8s_POD_first-pod_default_d5fc44c3-81c7-4a63-9e00-cf9650c26c58_0
c6694424e858        andonovj/httpserverdemo   "dotnet HttpServerDe…"   2 hours ago         Up 2 hours                              k8s_hello-pod_hello-deploy-7f44bd8b96-qz6cr_default_6fb3b168-ab19-47e9-be9c-f016f214f092_1
60ea2607bd11        andonovj/httpserverdemo   "dotnet HttpServerDe…"   2 hours ago         Up 2 hours                              k8s_hello-pod_hello-deploy-7f44bd8b96-4c76j_default_4b9f70f8-0d5d-4a19-aea0-8f393412f939_1
3ea2eeed5344        andonovj/httpserverdemo   "dotnet HttpServerDe…"   2 hours ago         Up 2 hours                              k8s_hello-pod_hello-deploy-7f44bd8b96-7tvcs_default_f4dc2924-7a87-44c9-bb5b-c3010b0451be_1
4742768bace2        andonovj/httpserverdemo   "dotnet HttpServerDe…"   2 hours ago         Up 2 hours                              k8s_hello-pod_hello-deploy-7f44bd8b96-9lnrm_default_821d3183-7c5e-413e-99a1-144bb13caff4_1
fd7678069cdd        k8s.gcr.io/pause:3.2      "/pause"                 2 hours ago         Up 2 hours                              k8s_POD_hello-deploy-7f44bd8b96-qz6cr_default_6fb3b168-ab19-47e9-be9c-f016f214f092_1
598a580a0ab0        k8s.gcr.io/pause:3.2      "/pause"                 2 hours ago         Up 2 hours                              k8s_POD_hello-deploy-7f44bd8b96-4c76j_default_4b9f70f8-0d5d-4a19-aea0-8f393412f939_1
8cc487d0c45e        andonovj/httpserverdemo   "dotnet HttpServerDe…"   2 hours ago         Up 2 hours                              k8s_hello-pod_hello-deploy-7f44bd8b96-gnvwr_default_f467d445-34c7-4dc9-ac37-e483be950d72_1
a64e7f2c167c        k8s.gcr.io/pause:3.2      "/pause"                 2 hours ago         Up 2 hours                              k8s_POD_hello-deploy-7f44bd8b96-7tvcs_default_f4dc2924-7a87-44c9-bb5b-c3010b0451be_1
97da605cd3c7        k8s.gcr.io/pause:3.2      "/pause"                 2 hours ago         Up 2 hours                              k8s_POD_hello-deploy-7f44bd8b96-9lnrm_default_821d3183-7c5e-413e-99a1-144bb13caff4_1
e7c8dcebe1be        k8s.gcr.io/pause:3.2      "/pause"                 2 hours ago         Up 2 hours                              k8s_POD_hello-deploy-7f44bd8b96-gnvwr_default_f467d445-34c7-4dc9-ac37-e483be950d72_1
71c9f4548392        0d40868643c6              "/usr/local/bin/kube…"   2 hours ago         Up 2 hours                              k8s_kube-proxy_kube-proxy-sbtcp_kube-system_f627745a-760f-442a-a371-27350c3e638d_3
420f51aa6c56        k8s.gcr.io/pause:3.2      "/pause"                 2 hours ago         Up 2 hours                              k8s_POD_kube-proxy-sbtcp_kube-system_f627745a-760f-442a-a371-27350c3e638d_3
b9cc5715f753        3610c051aa19              "start_runit"            2 hours ago         Up 2 hours                              k8s_calico-node_calico-node-5rqvv_kube-system_add6c10e-7693-41b0-953a-0ed2c3e2f671_3
9b86b11b3b61        k8s.gcr.io/pause:3.2      "/pause"                 2 hours ago         Up 2 hours                              k8s_POD_calico-node-5rqvv_kube-system_add6c10e-7693-41b0-953a-0ed2c3e2f671_3
root@node-1:~# docker exec -it ca7f18335d32 /bin/bash
root@first-pod:/# df -h
Filesystem      Size  Used Avail Use% Mounted on
overlay         9.7G  3.9G  5.8G  40% /
tmpfs            64M     0   64M   0% /dev
tmpfs           730M     0  730M   0% /sys/fs/cgroup
/dev/sda1       9.7G  3.9G  5.8G  40% /data
shm              64M     0   64M   0% /dev/shm
tmpfs           730M   12K  730M   1% /run/secrets/kubernetes.io/serviceaccount
tmpfs           730M     0  730M   0% /proc/acpi
tmpfs           730M     0  730M   0% /proc/scsi
tmpfs           730M     0  730M   0% /sys/firmware
root@first-pod:/# cd /data
root@first-pod:/data# ls -alrt
total 8
drwxr-xr-x 2 root root 4096 May 22 15:23 .
drwxr-xr-x 1 root root 4096 May 22 15:23 ..
root@first-pod:/data# pwd
/data
root@first-pod:/data# touch test
root@first-pod:/data# ls -alrt
total 8
drwxr-xr-x 1 root root 4096 May 22 15:23 ..
-rw-r--r-- 1 root root    0 May 22 15:29 test
drwxr-xr-x 2 root root 4096 May 22 15:29 .

So we have created a simple text file on the pod, under mount: “/data”. According our logic that file should be available on the host server under the defined volume destination:

Check the Volume

root@node-1:/home/ubuntu/volume1# hostname
node-1
root@node-1:/home/ubuntu/volume1# ls -lart
total 8
drwxr-xr-x 4 ubuntu ubuntu 4096 May 22 15:23 ..
-rw-r--r-- 1 root   root      0 May 22 15:29 test
drwxr-xr-x 2 root   root   4096 May 22 15:29 .
root@node-1:/home/ubuntu/volume1# pwd
/home/ubuntu/volume1
root@node-1:/home/ubuntu/volume1#

Lo and behold, the simple text file is on persistent storage and won't be affected if the container crashes for example. It will stay there safe and sound on the host server.

As we mentioned, that kind of persistent storage allocation DOESN'T SCALE and let's see why:

You see that, mapping and mapping and mapping :) Let's see what we can do about it.

Let's configure Dynamic storage.

  • k8s_basic_storage.1590161915.txt.gz
  • Last modified: 2020/05/22 15:38
  • by andonovj