LATEST VERSION: v1.2 - RELEASE NOTES
Pivotal Container Service v1.2

Configuring PersistentVolumes

Page last updated:

This topic describes how to provision static and dynamic PersistentVolumes (PVs) for Pivotal Container Service (PKS) to run stateful apps.

For more information about the supported vSphere topologies for PV storage, see vSphere PersistentVolume Storage Options on vSphere.

For static PV provisioning, you do not need to specify a StorageClass. The PersistentVolumeClaim (PVC) does not need to reference a StorageClass. For dynamic PV provisioning, you must specify a StorageClass and define the PVC using a reference to that StorageClass.

Provision a Static PV

To provision a static PV, you manually create a Virtual Machine Disk (VMDK) file to use as a storage backend for the PV. When the PV is created, Kubernetes knows which volume instance is ready for use. When a PVC or volumeClaimTemplate is requested, Kubernetes chooses an available PV in the system and allocates it to the Deployment or StatefulSets workload.

Provision a Static PV for a Deployment Workload

To provision a static PV for a Deployment workload, the procedure is as follows:

  1. Create VMDK files, replacing DATASTORE with your datastore directory name:

    [root@ESXi-1:~] cd /vmfs
    [root@ESXi-1:/vmfs] cd volumes/
    [root@ESXi-1:/vmfs/volumes] cd DATASTORE/
    [root@ESXi-1:/vmfs/volumes/DATASTORE] cd kubevols/
    [root@ESXi-1:/vmfs/volumes/DATASTORE/kubevols] vmkfstools -c 2G redis-master.vmdk
    
  2. Define a PV using a YAML manifest file that contains a reference to the VMDK file. For example, create a file named redis-master-pv.yaml with the following contents:

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: redis-master-pv
    spec:
      capacity:
        storage: 2Gi
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Retain
      vsphereVolume:
        volumePath: "[DATASTORE] kubevols/redis-master"
        fsType: ext4
    
  3. Define a PVC using a YAML manifest file. For example, create a file named redis-master-claim.yaml with the following contents:

    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: redis-master-claim
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
        storage: 2Gi
    
  4. Define a deployment using a YAML manifest file that references the PVC. For example, create a file named redis-master.yaml with the following contents:

    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: redis-master
    
    spec:
      template:
        spec:
          volumes:
          - name: redis-master-data
          persistentVolumeClaim:
            claimName: redis-master-claim
    

Provision a Static PV for a StatefulSets Workload

To provision a static PV for a StatefulSets workload with three replicas, the procedure is as follows:

  1. Create VMDK files, replacing DATASTORE with your datastore directory name:

    [root@ESXi-1:~] cd /vmfs
    [root@ESXi-1:/vmfs] cd volumes/
    [root@ESXi-1:/vmfs/volumes] cd DATASTORE/
    [root@ESXi-1:/vmfs/volumes/DATASTORE] cd kubevols/
    [root@ESXi-1:/vmfs/volumes/DATASTORE/kubevols] vmkfstools -c 10G mysql-pv-1.vmdk
    [root@ESXi-1:/vmfs/volumes/DATASTORE/kubevols] vmkfstools -c 10G mysql-pv-2.vmdk
    [root@ESXi-1:/vmfs/volumes/DATASTORE/kubevols] vmkfstools -c 10G mysql-pv-3.vmdk
    
  2. Define a PV for the first replica using a YAML manifest file that contains a reference to the VMDK file. For example, create a file named mysql-pv-1.yaml with the following contents:

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: mysql-pv-1
    spec:
      capacity:
        storage: 10Gi
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Retain
      vsphereVolume:
        volumePath: "[DATASTORE] kubevols/mysql-pv-1"
        fsType: ext4
    
  3. Define a PV for the second replica using a YAML manifest file that contains a reference to the VMDK file. For example, create a file named mysql-pv-2.yaml with the following contents:

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: mysql-pv-2
    spec:
      capacity:
        storage: 10Gi
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Retain
      vsphereVolume:
        volumePath: "[DATASTORE] kubevols/mysql-pv-2"
        fsType: ext4
    
  4. Define a PV for the third replica using a YAML manifest file that contains a reference to the VMDK file. For example, create a file named mysql-pv-3.yaml with the following contents:

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: mysql-pv-3
    spec:
      capacity:
        storage: 10Gi
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Retain
      vsphereVolume:
        volumePath: "[DATASTORE] kubevols/mysql-pv-3"
        fsType: ext4
    
  5. Define a StatefultSets object using a YAML manifest file. For example, create a file named mysql-statefulsets.yaml with the following contents:

    piVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: mysql
    spec:
      selector:
        matchLabels:
          app: mysql
      serviceName: mysql
      replicas: 3
    ...
    volumeClaimTemplates:
      - metadata:
        name: data
        spec:
        accessModes: ["ReadWriteOnce"]
        resources:
            requests:
            storage: 10Gi
    

    Note: In previous steps you created a total of three PVs. The spec.replicas: 3 field defines three replicas. Each replica is attached to one PV.

    Note: In the volumeClaimTemplates section, you must specify the required storage size for each replica. Do not to refer to a StorageClass.

Provision a Dynamic PV

For dynamic PV provisioning, the procedure is to define and create a PVC that automatically triggers the creation of the PV and its backend VMDK file. When the PV is created, Kubernetes knows which volume instance is available for use. When a PVC or volumeClaimTemplate is requested, Kubernetes chooses an available PV and allocates it to the Deployment or StatefulSets workload.

For usage instructions, see Using Dynamic PersistentVolumes.

Provision a Dynamic PV for Deployment Workloads

For the Deployment workload with dynamic PV provisioning, the procedure is as follows:

  1. Define a StorageClass using a YAML manifest file. For example, create a file named redis-sc.yaml with the following contents:

    kind: StorageClass
    apiVersion: storage.k8s.io/v1
    metadata:
      name: thin-disk
    provisioner: kubernetes.io/vsphere-volume
    parameters:
        datastore: Datastore-NFS-VM
        diskformat: thin
        fstype: ext3
    
  2. Define a PVC using a YAML manifest file that references the StorageClass. For example, create a file named redis-master-claim.yaml with the following contents:

    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: redis-master-claim
      annotations:
        volume.beta.kubernetes.io/storage-class: thin-disk
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
        storage: 2Gi
    

    Note: When you deploy the PVC, the vSphere Cloud Provider plugin automatically creates the PV and associated VMDK file.

  3. Define a Deployment using a YAML manifest file that references the PVC. For example, create a file named redis-master.yaml with the following contents:

    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: redis-master
    
    spec:
      template:
        spec:
          volumes:
          - name: redis-master-data
            persistentVolumeClaim:
            claimName: redis-master-claim
    

Provision a Dynamic PV for StatefulSets Workloads

To provision a static PV for a StatefulSets workload with three replicas, the procedure is as follows:

  1. Define a StorageClass using a YAML manifest file. For example, create a file named mysql-sc.yaml with the following contents:

    kind: StorageClass
    apiVersion: storage.k8s.io/v1
    metadata:
      name: my-storage-class
    provisioner: kubernetes.io/vsphere-volume
    parameters:
        datastore: Datastore-NFS-VM
        diskformat: thin
        fstype: ext3
    
  2. Define a StatefultSets object using a YAML manifest file that references the StorageClass. For example, create a file named mysql-statefulsets.yaml with the following contents:

    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: mysql
    spec:
    ...
    volumeClaimTemplates:
      - metadata:
        name: data
        spec:
        accessModes: ["ReadWriteOnce"]
          storageClassName: "my-storage-class"
        resources:
            requests:
            storage: 10Gi
    

    Note: In the volumeClaimTemplates, specify the required storage size for each replica. Unlike static provisioning, you must explicitly refer to the desired StorageClass when you use dynamic PV provisioning.


Please send any feedback you have to pks-feedback@pivotal.io.

Create a pull request or raise an issue on the source for this page in GitHub