Working with persistent volumes
Persistent volumes (PVs) can be connected to simple demo applications in various ways. Next, Persistent Volume Claims (PVCs) will be used to connect them. An Ingress resource will be created to test the functionality of the applications and the volumes connected to them.
-
Create a Kubernetes cluster of the latest version, if not done so already.
When creating the cluster:
- Select the Assign external IP option.
- Create one group of worker nodes with virtual machine type
STD3-2-8in theMS1availability area with total computing resources: 2 vCPU, 8 GB RAM (or more). This is necessary to be able to schedule all the required objects.
For example, you can create one group of nodes with virtual machine type
STD3-2-8.Other cluster parameters are at your discretion.
-
Make sure that the NGINX Ingress add-on (
ingress-nginx) is installed in a cluster with default parameters. It will be required to provide access to demo applications. -
Make sure that you can connect to the cluster using
kubectl. -
Install curl if the utility is not already installed.
The following will demonstrate how to create several NGINX-based web applications to display web pages written to the PVs connected to those applications.
The NGINX nginxdemos/nginx-hello image is used, which displays web pages from the /usr/share/nginx/html directory, so all PVs will be mounted in the application pods via this path.
You can create one or more demo applications, depending on which way you want to connect the PVs.
Block stores are connected to the cluster with Cinder CSI.
When using this type of storage:
- only one pod can access storage (multiple pods cannot use block storage at the same time);
- as a consequence, the
ReadWriteOncemode must be used to access the storage.
This example will create:
-
A dynamic PVC that will create a permanent volume based on the given parameters.
-
A
coffeeapplication as a single pod deployment, and its corresponding service.For this application there will also be an initialization container (initContainer) which will write the web page to the PV.
To connect a PV using dynamic PVC:
-
Examine the connection features:
- The required storage size is specified in the
spec.resources.requests.storageparameter of the PersistentVolumeClaim resource. In this example it is 1 GB. - For the PersistentVolumeClaim resource, specify the storage class in the
spec.storageClassNameparameter. The storage class must use the same availability zone as the worker node on which the pod (application) will reside. Otherwise, an attempt to connect a PV corresponding to the PVC to the pod on that node will fail. In this example, the pod will be placed on a group of worker nodes in theMS1availability zone and use thecsi-ceph-hdd-ms1storage class from the same zone. - The storage access mode is set in the
spec.accessModesparameter for the PersistentVolumeClaim resource. - ReclaimPolicy
Deleteis used for PV (it follows from selected storage class). When the PVC is deleted, the disk corresponding to the PV will automatically be deleted.
- The required storage size is specified in the
-
Create a manifest for the
coffeeapplication. -
Apply this manifest to the cluster to create all necessary resources:
kubectl apply -f ./coffee.yaml
File storages are connected to the cluster using a PV that is configured to use the existing storage via the required protocol, such as NFS.
When using this type of storage:
- Multiple pods can access the storage at once;
- as a consequence, the
ReadWriteManymode must be used to access the storage.
This example will create:
- NFS file storage in the Cloud Servers service.
- A PV corresponding to this storage.
- Static PVC using an already created PV.
- Application
milkshakeas a StatefulSet of two pods, as well as the corresponding services.
To connect an NFS PV using a static PVC:
-
When creating it, specify:
- Name of file storage: any name, such as
storage-milkshake. - Storage size: `10 GB'.
- Protocol:
NFS. - Network: network and subnet where the Kubernetes cluster is located. This information can be found on the cluster page.
- File storage network: existing network. If a suitable network is not on the list, select
Create new network.
- Name of file storage: any name, such as
-
View information about the created file storage.
Save the value of the Connection point parameter.
-
Examine the specifics of the connection:
-
The storage sizes specified in the
spec.capacity.storageandspec.resources.requests.storageparameters for the PersistentVolumeClaim resource must match the size of the created file storage. In this example it's 10 GB. -
For the PersistentVolumeClaim resource, use an empty value in the
storageClassNamestorage class parameter. -
For the PersistentVolume resource:
- The storage access mode is specified in the
spec.accessModesparameter for the PersistentVolume resource. - The
spec.mountOptionsparameter set must contain annfsversentry with version4.0.
- The storage access mode is specified in the
-
Instead of an initialization container to write a web page to a PV, a single-run Kubernetes task (job) is used. This approach works because in this case all pods will have access to the same PV.
-
ReclaimPolicy
Retainis used for the PV because theRecyclepolicy will not allow instant removal of the volume when it becomes unnecessary. Clearing a volume of data takes a long time. TheDeletepolicy is not used so that you can monitor the state of the storage manually and not accidentally delete it.
-
-
Create a manifest for the
milkshakeapplication.For the PersistentVolume resource, specify:
- IP address from the Connection point of the file storage as the value of the
spec.nfs.serverparameter. - Data after the IP address (
/shares/...) as a value of thespec.nfs.pathparameter.
- IP address from the Connection point of the file storage as the value of the
-
Apply this manifest to the cluster to create all necessary resources:
kubectl apply -f ./milkshake.yaml
-
Create a manifest for the Ingress resource through which application requests will go.
-
Apply this manifest to the cluster to create all necessary resources:
kubectl apply -f ./cafe-ingress.yaml -
Define the public IP address of the Ingress controller.
-
Check the availability of the applications with
curlusing the IP address of the Ingress controller.TeaCoffeeJuiceMilkshakeRun the command:
curl --resolve cafe.example.com:80:<Ingress IP address> http://cafe.example.com/teaA response should be displayed:
The tea pod says Hello World to everyone! This file is located on the statically claimed PV.
A running cluster consumes computing resources and is charged accordingly. If you no longer need the Kubernetes resources you created to test persistent volumes, delete them:
-
Delete the resources described in the manifests:
Linux/macOSWindowskubectl delete -f ./cafe-ingress.yamlkubectl delete -f ./milkshake.yamlkubectl delete -f ./juice.yamlkubectl delete -f ./coffee.yamlkubectl delete -f ./tea.yaml -
Remove unused storage:
-
The disk used by the
teaapplication. -
The NFS repository used by the
milkshakeapplication.
All other Cinder stores created with dynamic PVCs will be deleted automatically.
-
-
Stop the cluster you created to use it later or delete it permanently.