Persistent volumes in a Managed Kubernetes cluster
Cloud platform network volume-based persistent volume connectivity is not available in Managed Kubernetes clusters on dedicated servers.
A Persistent Volume is used for long-term data storage in a Managed Kubernetes cluster. To manage persistent volumes in Kubernetes, you use PersistentVolume (PV), PersistentVolumeClaim (PVC), and StorageClass objects.
For persistent volumes in Managed Kubernetes, we recommend using network volumes from the Selectel cloud platform. You can create a persistent volume on a local disk, but the data will be deleted when the node is deleted.
After creating a persistent volume, you can increase and delete it.
You can view all persistent volumes in the control panel: in the top menu, click Products and select Cloud Servers → Disks.
Create a persistent volume
Creation via the Topology-Aware Volume Provisioning mechanism is not available.
- Create a StorageClass or use an existing one.
- Create a PersistentVolumeClaim.
- Create a pod with a persistent volume.
1. Create a StorageClass
To create a PersistentVolume, use the StorageClass object. StorageClass allows you to predefine the configuration of persistent volumes required for cluster operations.
When a cluster is created, a single StorageClass will be automatically created with a fast (fast) network volume in the pool where the cluster node group is located.
-
Create a yaml file with a manifest for the StorageClass object.
An example of a StorageClass manifest for a fast disk in the ru-1a pool:
kind: StorageClassapiVersion: storage.k8s.io/v1metadata:name: fast.ru-1aprovisioner: cinder.csi.openstack.orgparameters:type: fast.ru-1aavailability: ru-1afsType: ext4allowVolumeExpansion: trueHere,
fast.ru-1ais a StorageClass type.You can use other predefined StorageClass manifests.
-
Apply the manifest:
kubectl apply -f <file_name>Specify
<file_name>— the name of the yaml file with the manifest to create a new StorageClass. For example,storage-class.yaml. -
Make sure that the StorageClass object is created:
kubectl get scThe response will contain a list of created StorageClass objects. For example:
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGEfast.ru-1a cinder.csi.openstack.org Delete Immediate true 16m
StorageClass type
The StorageClass type format is <disk type>.<pool segment where it is located>.
Disk types correspond to network volumes of the Selectel cloud platform:
For example, to create a fast disk in a pool segment of the ru-1a pool, you must add the following to the StorageClass description:
parameters:
type: fast.ru-1a
availability: ru-1a
2. Create a PersistentVolumeClaim
Volumes can only be used in ReadWriteOnce mode — a single volume can be mounted to only one node, and only one pod can be connected to the persistent volume. Connecting multiple pods to a single PV may result in data corruption. To use ReadWriteMany mode (mounting a volume to multiple nodes), you can connect network file storage to cluster nodes.
-
Create a yaml file with a manifest for the PersistentVolumeClaim (PVC) object.
Example of a manifest:
apiVersion: v1kind: PersistentVolumeClaimmetadata:name: my-pv-claimspec:storageClassName: fast.ru-1aaccessModes:- ReadWriteOnceresources:requests:storage: 10GiThe pool in the PVC manifest must match the pool of the node to which you plan to connect this PVC. If you use multiple pools for cluster nodes and PVCs, specify their pool binding in the Pod object descriptions.
-
Apply the manifest:
kubectl apply -f <file_name>Specify
<file_name>— the name of the yaml file with the manifest to create a new PersistentVolumeClaim. For example,pvc.yaml.
3. Create a pod with a persistent volume
If you create a pod (Pod) with a persistent volume, the volume is preserved when the pod is deleted.
-
Create a yaml file with a manifest to create a new pod with a persistent volume.
Example of a manifest:
apiVersion: v1kind: Podmetadata:name: nginxlabels:app: webservicespec:containers:- name: nginximage: library/nginx:1.17-alpineports:- containerPort: 80volumeMounts:- mountPath: "/var/www/html"name: datavolumes:- name: datapersistentVolumeClaim:claimName: my-pv-claimWhen creating a pod with the
securityContext.fsGroupparameter, the persistent volume will not be mounted with the corresponding GID. To resolve this issue, addfsType: ext4to the StorageClass parameters. -
Apply the manifest:
kubectl apply -f <file_name>Specify
<file_name>— the name of the yaml file with the manifest to create a new pod with a persistent volume. For example,pod-with-pv.yaml. -
Check that the PersistentVolume is created:
kubectl get pvThe response will contain a list of PersistentVolumes. For example:
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGEpvc-f171f94c-0d38-41be-947e-2f5d7e46a6c3 10Gi RWO Delete Bound default/my-pv-claim fast.ru-1a 97s
Increase a persistent volume
- Check the occupied space in the PersistentVolumeClaim.
- Make sure you have enough quotas to increase the PersistentVolume.
- Allow volume expansion in the StorageClass settings.
- Delete the pods using the volume you need to increase.
- Edit the PersistentVolumeClaim manifest.
1. Check the occupied space in the PVC
Find out the amount of occupied space in the PVC:
kubectl -n <namespace> exec <pod_name> -- df -ah
Specify:
<namespace>— the namespace where the PVC is located;<pod_name>— the name of the pod using the PVC.
2. Check quotas
To ensure there are enough resources to increase the persistent volume, check your quotas and change them if necessary.
3. Allow increasing the persistent volume
In the StorageClass object parameters, specify allowVolumeExpansion: true.
Example of a manifest:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: example-vol-default
provisioner: vendor-name.example/magicstorage
parameters:
resturl: "http://192.168.10.100:8080"
restuser: ""
secretNamespace: ""
secretName: ""
allowVolumeExpansion: true
reclaimPolicy: Delete
4. Delete pods with the volume that needs to be increased
-
Check which pods are using the PVC:
kubectl describe pvc <pvc_name>Specify
<pvc_name>— the name of the PersistentVolumeClaim. -
Delete the pods that are using the PVC:
kubectl delete pod <pod_name>Specify
<pod_name>— the name of the pod.
5. Edit the PersistentVolumeClaim manifest
-
Open the yaml file with the PersistentVolumeClaim manifest and change the
storage:parameter:apiVersion: v1kind: PersistentVolumeClaimmetadata:name: my-pv-claimspec:storageClassName: fast.ru-1aaccessModes:- ReadWriteOnceresources:requests:storage: 10Gi -
Create a PersistentVolumeClaim — apply the manifest:
kubectl apply -f <pvc_name>Specify
<pvc_name>— the name of the PersistentVolumeClaim. -
Run the pod using this PVC.
Example of a manifest:
apiVersion: v1kind: Podmetadata:name: nginxlabels:app: webservicespec:containers:- name: nginximage: library/nginx:1.17-alpineports:- containerPort: 80volumeMounts:- mountPath: "/var/www/html"name: datavolumes:- name: datapersistentVolumeClaim:claimName: my-pv-claim -
Check that the PersistentVolumeClaim is created:
kubectl get pvc
Delete a persistent volume
If you no longer need the persistent volume, delete the PersistentVolumeClaim that was used to create this volume.
The PV will be deleted immediately if the persistentVolumeReclaimPolicy: Delete parameter is specified in the PVC manifest. Learn more about the, return policy (Reclaim policy) in the Reclaiming article in the Kubernetes documentation.
-
Check which pods are bound to the PVC:
kubectl describe pvc <pvc_name>Specify
<pvc_name>— the name of the PersistentVolumeClaim. -
Delete the pods that are using the PVC:
kubectl delete pod <pod_name>Specify
<pod_name>— the name of the pod. -
Delete the PVC to which the PV is bound:
kubectl delete pvc <pvc_name>Specify
<pvc_name>— the name of the PersistentVolumeClaim.