The cloud platform is indeed a model of cloud computing subject in which the server creates hardware features as well as some tools to increase the software development speed and resolve the developer concerns caused by the product required infrastructures. We have dedicated this article to explain and instruct how to use PersistentVolumeClaim, introduce its components, and how to define each one.

Prerequisites of Using PersistentVolumeClaim on the ArvanCloud Cloud Platform

The only prerequisite of using this system is to have an ArvanCloud account and access to the ArvanCloud cloud platform. You may complete the following steps:

  • Open the ArvanCloud website: www.arvancloud.ir,
  • Create an account or log in to your account,
  • Go to the Profile section,
  • Create an API Key in the API KEYS tab,
  • Save the API Key.

To do the steps, you will require the ArvanCloud command line. Therefore,

  • Use this link to download the command line,
  • (Put it in your PATH if needed),
  • And log in via the command line:
  • Paste the API KEY that you received from the site here.

What is PersistentVolumeClaim?

The Pod disk is not persistent; it means if you restart the Pod, you will lose the entire saved data, and the container returns to its original status. However, some programs, such as databases, require a persistent disk to keep the stored data after restarting the Pod. In such a case, you should employ the PersistentVolumeClaim (PVC), which is one of the significant and helpful elements of the ArvanCloud cloud platform. The PersistentVolumeClaim makes it possible for the user to connect a persistent disk to the Pod for storing the data permanently.

Indeed, PVC determines the request for a persistent disk from the ArvanCloud cloud platform. When applying the PersistentVolumeClaim on the ArvanCloud cloud platform, be noted that every PVC can only attach to one container. In other words, you cannot simultaneously apply several ones to a PersistentVolumeClaim.

When developing the program and creating a deployment for persistent data storing, you are supposed to define a PersistentVolumeClaim.

How to Create a PersistentVolumeClaim

To create a PVC, you have to insert the required information in the YAML format into a file and then use the command line to introduce it to the ArvanCloud cloud platform. You are going to read a simple example of a PersistentVolumeClaim for an Nginx service – for which you have created a service, deployment, and route in the previous articles. There is also an explanation of its parts.

Be noted that you need to make some changes in the deployment explained in the following lines.

Hint: Notice that indentation is a significant feature of YAML files, and even the smallest movement may cause an error or undesired settings to revert.

Keep on reading to apprehend the relevant fields.

  • kind: It determines the nature type. The field may include values such as PersistentVolumeClaim, PodStatefulSet, Service, so on. In this example, the definition target is PersistentVolumeClaim. Thus, we have determined PersistentVolumeClaim for the field.
  • metadata.name: This field determines the PersistentVolumeClaim name.
  • spec.accessMode: It determines the access type to the disk – you should always set it on the ReadWriteOnce on the ArvanCloud cloud platform. This value means that each disk can be connected to only one container concurrently.
  • spec.resources.requests: This field determines the requested disk volume.

Insert the preceding lines in a file named nginx-persistentVolumeClaim.yaml and save it. Then, write the following command in the command line to introduce your PersistentVolumeClaim to the ArvanCloud cloud platform.

Use the following command to get aware of your PersistentVolumeClaim and its implementation on the ArvanCloud cloud platform.

The output will be something similar to the following figure:

In the preceding output,

  • The Name field determines the PersistentVolumeClaim name.
  • The Status field determines the PersistentVolumeClaim status (bound means the PersistentVolumeClaim is ready).
  • The Volume column defines the PersistentVolumeClaim ID.
  • And, the Capacity determines the PersistentVolumeClaim volume.
  • The Access Mode is equivalent to spec.accessModes value in the description file.
  • (Explaining the StorageClass is beyond the scope of this article).
  • The Age also determines the time during which the PersistentVolumeClaim is active.

If you apply the above file, the ArvanCloud cloud platform considers a disk for you. However, whenever you want to use the disk, you have to connect it to a container in a Pod. We are going to explain the changes that you should apply to the deployment to be connected.

How to Use the PersistentVolumeClaim

As you know, the PersistentVolumeClaim should be connected to a Pod. In this section, we explain how to use the PersistentVolumeClaim in a deployment.

Insert the following information into a file named nginx-deployment-persist.yaml.

Hint: Notice that indentation is a significant feature of YAML files, and even the smallest movement may cause an error or undesired settings to revert.

In the following lines, you are going to get familiar with the fields that you should add to the deployment to connect the disk to the Pod.

  • spec.strategy: As mentioned earlier, you can connect each PersistentVolumeClaim to one container on the ArvanCloud cloud platform. Therefore, you must quantify this field if you are going to erase the previous Pod, free the disk, and then create a new Pod and connect the disk to it.
  • spec.template.spec.containers.volumeMounts: This field determines the path in which the disk has to be mounted.
  • spec.template.spec.containers.volumeMounts.name: You have to name the paths because you can connect more than one container to a disk.
  • spec.template.spec.containers.volumeMounts.mountPath: It determines the path address on the disk that you want to write and read its data on the persistent disk.
  • spec.template.spec.volumes: This field determines the relationship between the persistent disk and the path inside the container, in which the system should mount the disk.
  • spec.template.spec.volumes.name: This field holds the volume name by which the container path has been determined. The same value is determined by the spec.template.containers.volumeMounts.name.
  • spec.template.spec.volumes.persistentVolumeClaim.claimName: This is the disk or PersistentVolumeClaim name on which the data will be persisted.

Use the command line to introduce the above file to the ArvanCloud cloud platform. You will see that even if you restart the Pod, the data on the var/log/nginx/ path that is determined by the spec.template.spec.containers.volumeMounts.mountPath field will remain.

Be noted that you cannot increase the number of replicas of a deployment that has a PersistentVolumeClaim because each disk can only connect to one container on the ArvanCloud cloud platform. If you require several Pods similar to a persistent disk, you should employ the StateFulSet. Read a brief introduction to this concept in the following section.

How to use PersistentVolumeClaim in the StateFulSet

As mentioned earlier, a disk can be connected to only one container on the ArvanCloud cloud platform. On the other hand, you need some similar Pods to have a persistent disk on some occasions (the data stored on a persistent disk may be different). There is a useful example in the ElasticSearch Cluster Installation on the ArvanCloud Cloud Platform article.

The difference between using a PersistentVolumeClaim in the StateFulSet and the Deployment is that when using the StateFulSet, you do not have to define the PersistentVolumeClaim separately – you can employ the VolumeClaimTemplate in the same StateFulSet description to create a separate disk for every Pod.

The following lines show how to apply the VolumeClaimTemplate. The settings are similar to PersistentVolumeClaim ones.

As you see in the following figure, a disk has been created for each Pod. It is also possible to increase the number of replicas.

See the OKD and Kubernetes documents for more information.