[Yandex Cloud documentation](../../../index.md) > [Yandex Managed Service for Kubernetes](../../index.md) > [Step-by-step guides](../index.md) > Working with persistent volumes > Dynamic volume provisioning

# Dynamic volume provisioning


Create a [pod](../../concepts/index.md#pod) with a dynamically provisioned [volume](../../concepts/volume.md):
1. [Create a PersistentVolumeClaim](#create-pvc).
1. [Create a pod](#create-pod).

First, [install kubectl](https://kubernetes.io/ru/docs/tasks/tools/install-kubectl/) and [configure it to work with the Managed Service for Kubernetes cluster you created](../connect/index.md#kubectl-connect).

{% note tip %}

You can use a [Yandex Object Storage](../../../storage/index.md) [bucket](../../../storage/concepts/bucket.md) to store your pod. For more information, see [Integration with Object Storage](s3-csi-integration.md).

{% endnote %}

## Create a PersistentVolumeClaim {#create-pvc}

1. Save the following `PersistentVolumeClaim` creation specification to a YAML file named `pvc-dynamic.yaml`.

   {% note info %}

   If the `storageClassName` parameter is not specified, the default storage class, `yc-network-hdd`, will be used. Learn how to change the default class in [Update the default storage class](manage-storage-class.md#sc-default).

   {% endnote %}

   Learn more about the `PersistentVolumeClaim` creation specification in the [Kubernetes guide](https://kubernetes.io/docs/reference/kubernetes-api/config-and-storage-resources/persistent-volume-claim-v1/).

   ```yaml
   apiVersion: v1
   kind: PersistentVolumeClaim
   metadata:
     name: pvc-dynamic
   spec:
     accessModes:
       - ReadWriteOnce
     storageClassName: yc-network-hdd
     resources:
       requests:
         storage: 4Gi
   ```

1. Run this command:

   ```bash
   kubectl create -f pvc-dynamic.yaml
   ```

   Result:

   ```text
   persistentvolumeclaim/pvc-dynamic created
   ```

1. View the information about the new `PersistentVolumeClaim` object:

   ```bash
   kubectl describe persistentvolumeclaim pvc-dynamic
   ```

   Result:

   ```text
   Name:          pvc-dynamic
   Namespace:     default
   StorageClass:  yc-network-hdd
   ...
   Type    Reason                Age               From                         Message
   ----    ------                ----              ----                         -------
   Normal  WaitForFirstConsumer  9s (x3 over 15s)  persistentvolume-controller  waiting for first consumer to be created before binding
   ```

## Create a pod with a dynamically provisioned volume {#create-pod}

1. Save the following pod creation specification to a YAML file named `pod.yaml`.

   Learn more about the pod creation specification in [this Kubernetes guide](https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/).

   ```yaml
   apiVersion: v1
   kind: Pod
   metadata:
     name: pod
   spec:
     containers:
     - name: app
       image: ubuntu
       command: ["/bin/sh"]
       args: ["-c", "while true; do echo $(date -u) >> /data/out.txt; sleep 5; done"]
       volumeMounts:
       - name: persistent-storage
         mountPath: /data
     volumes:
     - name: persistent-storage
       persistentVolumeClaim:
         claimName: pvc-dynamic
   ```

1. Run this command:

   ```bash
   kubectl create -f pod.yaml
   ```

   Result:

   ```text
   pod/pod created
   ```

1. View the information about the new pod:

   ```bash
   kubectl describe pod pod
   ```

   Result:

   ```text
   Name:         pod
   Namespace:    default
   Priority:     0
   ...
     Normal  Pulled                  11s   kubelet, cl1gqrct5oie********-ytas  Successfully pulled image "ubuntu"
     Normal  Created                 10s   kubelet, cl1gqrct5oie********-ytas  Created container
     Normal  Started                 10s   kubelet, cl1gqrct5oie********-ytas  Started container
   ```

   After creating the pod:
   * In the [management console](https://console.yandex.cloud) in **Compute Cloud** under **Disks**, a new [disk](../../../compute/concepts/disk.md) will appear with the `k8s-csi` prefix in its name.
   * You can find the disk provisioning information in the `PersistentVolumeClaim` events:

     ```bash
     kubectl describe persistentvolumeclaim pvc-dynamic
     ```

     Result:

     ```text
     Name:          pvc-dynamic
     Namespace:     default
     StorageClass:  yc-network-hdd
     ...
       Normal  ExternalProvisioning   4m10s (x3 over 4m10s)  persistentvolume-controller                                                              waiting for a volume to be created, either by external provisioner "disk-csi-driver.mks.ycloud.io" or manually created by system administrator
       Normal  Provisioning           4m10s                  disk-csi-driver.mks.ycloud.io_cat1h5l0v862oq74cp8j_d0f0b837-a875-11e9-b6cb-d00d********  External provisioner is provisioning volume for claim "default/pvc-dynamic"
       Normal  ProvisioningSucceeded  4m7s                   disk-csi-driver.mks.ycloud.io_cat1h5l0v862oq74cp8j_d0f0b837-a875-11e9-b6cb-d00d********  Successfully provisioned volume pvc-c4794058-ad68-11e9-b71a-d00d********
     ```

## How to delete a volume {#delete-volume}

To delete a dynamically provisioned volume, delete the `PersistentVolumeClaim`:

```bash
kubectl delete pvc <PersistentVolumeClaim_ID>
```

The disk will be deleted automatically from [Yandex Compute Cloud](../../../compute/index.md).

### See also {#see-also}

* [Volume](../../concepts/volume.md)
* [Using encrypted disks for persistent volumes](encrypted-disks.md)
* [Static volume provisioning](static-create-pv.md)
* [Managing storage classes](manage-storage-class.md)