Skip to main content

Persistent volumes in a Managed Kubernetes cluster

Last update:
For your information

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 ServersDisks.

Create a persistent volume

For your information

Creation via the Topology-Aware Volume Provisioning mechanism is not available.

  1. Create a StorageClass or use an existing one.
  2. Create a PersistentVolumeClaim.
  3. 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.

  1. 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: StorageClass
    apiVersion: storage.k8s.io/v1
    metadata:
    name: fast.ru-1a
    provisioner: cinder.csi.openstack.org
    parameters:
    type: fast.ru-1a
    availability: ru-1a
    fsType: ext4
    allowVolumeExpansion: true

    Here, fast.ru-1a is a StorageClass type.

    You can use other predefined StorageClass manifests.

  2. 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.

  3. Make sure that the StorageClass object is created:

    kubectl get sc

    The response will contain a list of created StorageClass objects. For example:

    NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
    fast.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:

Network volume typeName in StorageClass
Fast SSDfast
Basic HDDbasic
Basic SSDbasicssd
Universal SSDuniversal

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

For your information

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.

  1. Create a yaml file with a manifest for the PersistentVolumeClaim (PVC) object.

    Example of a manifest:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
    name: my-pv-claim
    spec:
    storageClassName: fast.ru-1a
    accessModes:
    - ReadWriteOnce
    resources:
    requests:
    storage: 10Gi

    The 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.

  2. 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.

  1. Create a yaml file with a manifest to create a new pod with a persistent volume.

    Example of a manifest:

    apiVersion: v1
    kind: Pod
    metadata:
    name: nginx
    labels:
    app: webservice
    spec:
    containers:
    - name: nginx
    image: library/nginx:1.17-alpine
    ports:
    - containerPort: 80
    volumeMounts:
    - mountPath: "/var/www/html"
    name: data
    volumes:
    - name: data
    persistentVolumeClaim:
    claimName: my-pv-claim

    When creating a pod with the securityContext.fsGroup parameter, the persistent volume will not be mounted with the corresponding GID. To resolve this issue, add fsType: ext4 to the StorageClass parameters.

  2. 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.

  3. Check that the PersistentVolume is created:

    kubectl get pv

    The response will contain a list of PersistentVolumes. For example:

    NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
    pvc-f171f94c-0d38-41be-947e-2f5d7e46a6c3 10Gi RWO Delete Bound default/my-pv-claim fast.ru-1a 97s

Increase a persistent volume

  1. Check the occupied space in the PersistentVolumeClaim.
  2. Make sure you have enough quotas to increase the PersistentVolume.
  3. Allow volume expansion in the StorageClass settings.
  4. Delete the pods using the volume you need to increase.
  5. 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

  1. Check which pods are using the PVC:

    kubectl describe pvc <pvc_name>

    Specify <pvc_name> — the name of the PersistentVolumeClaim.

  2. 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

  1. Open the yaml file with the PersistentVolumeClaim manifest and change the storage: parameter:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
    name: my-pv-claim
    spec:
    storageClassName: fast.ru-1a
    accessModes:
    - ReadWriteOnce
    resources:
    requests:
    storage: 10Gi
  2. Create a PersistentVolumeClaim — apply the manifest:

    kubectl apply -f <pvc_name>

    Specify <pvc_name> — the name of the PersistentVolumeClaim.

  3. Run the pod using this PVC.

    Example of a manifest:

    apiVersion: v1
    kind: Pod
    metadata:
    name: nginx
    labels:
    app: webservice
    spec:
    containers:
    - name: nginx
    image: library/nginx:1.17-alpine
    ports:
    - containerPort: 80
    volumeMounts:
    - mountPath: "/var/www/html"
    name: data
    volumes:
    - name: data
    persistentVolumeClaim:
    claimName: my-pv-claim
  4. 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.

  1. Check which pods are bound to the PVC:

    kubectl describe pvc <pvc_name>

    Specify <pvc_name> — the name of the PersistentVolumeClaim.

  2. Delete the pods that are using the PVC:

    kubectl delete pod <pod_name>

    Specify <pod_name> — the name of the pod.

  3. Delete the PVC to which the PV is bound:

    kubectl delete pvc <pvc_name>

    Specify <pvc_name> — the name of the PersistentVolumeClaim.