Edit me

With PV and PVCs one can only provision storage statically i.e. PVs first needs to be created before a Pod claims it. However, with StorageClass API Kubernetes enables dynamic volume provisioning. This avoids pre-provisioning of storage and storage is provisioned automatically when a user requests it.

StorageClass API object specifies a provisioner and parameters which are used to decide which volume plugin to be used and provisioner specific parameters. Provisioner could be AWS EBS, vSphere, OpenStack and so on.

vSphere is one of the provisioners and it allows following parameters:

  • diskformat which can be thin(default), zeroedthick and eagerzeroedthick

  • datastore is an optional field which can be VMFSDatastore or VSANDatastore. This allows user to select the datastore to provision PV from, if not specified the default datastore from vSphere config file is used.

  • storagePolicyName is an optional field which is the name of the SPBM policy to be applied. The newly created persistent volume will have the SPBM policy configured with it.

  • VSAN Storage Capability Parameters (cacheReservation, diskStripes, forceProvisioning, hostFailuresToTolerate, iopsLimit and objectSpaceReservation) are supported by vSphere provisioner for vSAN storage. The persistent volume created with these parameters will have these vSAN storage capabilities configured with it. For more detail about these parameters go to Storage Policy Management section.


All the example yamls can be found here unless otherwise specified. Please download these examples.

Let us look at an example of how to use StorageClass for dynamic provisioning.

Note: With StorageClass, VMDK need not be created manually for Persistent Volume. VMDK will be created dynamically by vsphere-volume provisioner.

Create Storage Class

$ cat vpshere-volume-sc-fast.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
  name: fast
provisioner: kubernetes.io/vsphere-volume
  datastore: VSANDatastore
  diskformat: thin
  fstype: ext3

If datastore is a member of datastore Cluster or within some subfolder, the datastore folder path needs to be provided in the datastore parameter as below.

   datastore:	DatastoreCluster/VSANDatastore

Create the Storageclass

$ kubectl create -f examples/volumes/vsphere/vsphere-volume-sc-fast.yaml

Verify storage class is created

$ kubectl describe storageclass fast
Name:           fast
IsDefaultClass: No
Annotations:    <none>
Provisioner:    kubernetes.io/vsphere-volume
Parameters:     diskformat=thin,fstype=ext3,datastore=VSANDatastore
No events.

Create Persistent Volume Claim

$ cat Vsphere-volume-pvcsc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
  name: pvcsc001
    volume.beta.kubernetes.io/storage-class: fast
    - ReadWriteOnce
      storage: 2Gi

Create the persistent volume claim

$ kubectl create -f examples/volumes/vsphere/vsphere-volume-pvcsc.yaml

Verify persistent volume claim is created

$ kubectl describe pvc pvcsc001
Name:           pvcsc001
Namespace:      default
StorageClass:   fast
Status:         Bound
Volume:         pvc-83295256-f8e0-11e6-8263-005056b2349c
Labels:         <none>
Capacity:       2Gi
Access Modes:   RWO
  FirstSeen LastSeen Count  From        SubObjectPath   Type  Reason Message
  1m          1m      1   persistentvolume-controller  Normal  Provisioning Succeeded

  Successfully provisioned volume pvc-83295256-f8e0-11e6-8263-005056b2349c using Kubernetes.io/vsphere-volume

Persistent Volume is automatically created and is bounded to this pvc.

Verify persistent volume is created

$ kubectl describe pv pvc-83295256-f8e0-11e6-8263-005056b2349c
Name:           pvc-83295256-f8e0-11e6-8263-005056b2349c
Labels:         <none>
StorageClass:   fast
Status:         Bound
Claim:          default/pvcsc001
Reclaim Policy: Delete
Access Modes:   RWO
Capacity:       2Gi
    Type:       vSphereVolume (a Persistent Disk resource in vSphere)
    VolumePath: [VSANDatastore] kubevols/kubernetes-dynamic-pvc-83295256-f8e0-11e6-8263-005056b2349c.vmdk
    FSType:     ext3
No events.

Note: VMDK is created inside kubevols folder in datastore which is mentioned in ‘vsphere’ cloudprovider configuration. The cloudprovider config is created during setup of Kubernetes cluster on vSphere.

Create Pod which uses Persistent Volume Claim with storage class.

$ cat vsphere-volume-pvcscpod.yaml
apiVersion: v1
kind: Pod
  name: pvpod
  - name: test-container
    image: gcr.io/google_containers/test-webserver
    - name: test-volume
      mountPath: /test-vmdk
  - name: test-volume
      claimName: pvcsc001

Create the pod

$ kubectl create -f vsphere-volume-pvcscpod.yaml

Verify pod is created

$ kubectl get pod pvpod
pvpod       1/1      Running   0          48m