Why Persistent Volume?

The need for persistent storage in Kubernetes arises for two reasons:

Kubernetes doesn't provide data persistence out of the box, which means when a pod is re-created, the data is gone. So, you need to create and configure the actual physical storage and manage it by yourself. Once configured, you can use that physical storage using Kubernetes storage components.

How does storage work in Kubernetes?

To fulfill this work, Kubernetes provides 3 components, that you need to use to connect the actual physical storage to your pod, so that the application inside the container can access it. They are (references to the official guide in the list):

From the descriptions of the 3 components it is clear that PV and SC are managed by the administrators (backend), while PVC concerns the user (frontend). Furthermore, it is clear that there are two ways of implementing the resources useful for storage, namely PVs:

In the static approach, if no PV present on the cluster meets the user's needs, the latter will have to contact the administrator and request the creation of a new PV with the desired specifications. This scenario, although more controlled than the dynamic one, can be inefficient if the requests become numerous.

Parameterization

These Kubernetes components can be finely customized thanks to a number of parameters. We briefly present the theoretical aspects concerning their personalization.

PersistentVolume types are implemented as plugins. Kubernetes currently supports a large number of plugins (GCEPersistentDisk, AWSElasticBlockStore, AzureDisk, NFS, CephFS, Cinder, etc.). A broad overview of the parameters that the different components can adopt is presented below. It is not certain that all plugins support all the parameters listed here.

Storage Class

Each SC contains fields which are used when a PV belonging to the class needs to be dynamically provisioned. The name of a SC object is significant, and is how users can request a particular class. Administrators set the name and other parameters of a class when first creating SC objects, and the objects cannot be updated once they are created. The aspects concerning the SC are:

Persistent Volume

The following parameters can be managed only if the static mode is chosen, since in the dynamic case the PVs are generated on the basis of the SC and the PVCs. The aspects concerning the PV are:

Persistent Volume Claim

Now let's move on to the component that deals with claiming pieces of storage. PVC can be used both in static and dynamic mode, but it was mainly born to be used on the frontend side in dynamic mode. The aspects concerning the PVC are:

To activate this plugin the administrator needs to edit the /etc/kubernetes/manifests/kube-apiserver.yaml file (the path may be different), inserting DefaultStorageClass in the specifications.

spec:
  containers:
  - command:
    - kube-apiserver
#   other commands...
    - --enable-admission-plugins=NodeRestriction,DefaultStorageClass

Lifecycle of a volume and claim

We conclude by investigating in more detail the link between PV and PVC. In particular, we deepen the concepts of binding and storage object in Use Protection.

Binding

For the union between PV and PVC, two of the parameters described above must be in agreement: storage and access mode

If the PVC requires more memory than the one made available by the PV, the binding does not take place and will remain unbound indefinitely if a matching volume does not exist. Likewise, if the access mode of the two components are not compatible, they will remain unconnected. I used the term compatible, because the two values don't necessarily have to be the same. In some providers, the binding can still take place if the PVC requires a lower level of permissions (eg. RWO) than that of the PV (eg. RWX).

We have said that the bond between PV and PVC is one-to-one. Therefore if the PVC requires, for example, 5Gi and the PV makes 8Gi available, there is a "waste" of 3Gi. Unfortunately, this can happen in the case of static provisioning. Dynamic mode is more "economical", because the PV is sewn exactly to the requirements of the PV.

Storage Object in Use Protection

The purpose of the storage object in use protection feature is to ensure that PVCs, in active use by a Pod, and PVs, that are bound to PVCs, are not removed from the system, as this may result in data loss. If a user deletes a PVC in active use by a Pod, the PVC is not removed immediately. PVC removal is postponed until the PVC is no longer actively used by any Pods. Also, if an admin deletes a PV that is bound to a PVC, the PV is not removed immediately. PV removal is postponed until the PV is no longer bound to a PVC.