Stateful applications, for example databases, save data between sessions and require persistent storage to store the data. The retained data is called the application's state. You can later retrieve the data and use it in the next session. Kubernetes offers persistent volumes as objects capable of retaining their state and data.

In the vSphere environment, the persistent volume objects are backed by virtual disks that reside on datastores. Datastores are represented by storage policies. After the vSphere administrator creates a storage policy, for example gold, and assigns it to a namespace in a Supervisor, the storage policy appears as a matching Kubernetes storage class in the vSphere Namespace and any available TKG clusters.

As a DevOps engineer, you can use the storage class in your persistent volume claim specifications. You can then deploy an application that uses storage from the persistent volume claim. In this example, the persistent volume for the application is created dynamically.

Prerequisites

Make sure that your vSphere administrator has created an appropriate storage policy and assigned the policy to the namespace.

Procedure

  1. Access your namespace in the vSphere Kubernetes environment.
  2. Verify that the storage classes are available.
  3. Create a persistent volume claim.
    1. Create a YAML file that contains the persistent volume claim configuration.
      In this example, the file references the gold storage class.
      To provision a ReadWriteMany persistent volume, set accessModes to ReadWriteMany.
      apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: my-pvc
      spec:
        accessModes:
          - ReadWriteOnce
        storageClassName: gold
        resources:
          requests:
              storage: 3Gi
                 
    2. Apply the persistent volume claim to the TKG cluster.
      kubectl apply -f pvc_name.yaml
      This command dynamically creates a Kubernetes persistent volume and a vSphere volume with a backing virtual disk that satisfies the claim's storage requirements.
    3. Check the status of the persistent volume claim.
      kubectl get pvc my-pvc

      The output shows that the volume is bound to the persistent volume claim.

      NAME     STATUS    VOLUME   CAPACITY   ACCESSMODES   STORAGECLASS   AGE
      my-pvc   Bound     my-pvc   2Gi        RWO           gold           30s
  4. Create a pod that mounts your persistent volume.
    1. Create a YAML file that includes the persistent volume.
      The file contains these parameters.
      ...
      volumes:
          - name: my-pvc
            persistentVolumeClaim:
              claimName: my-pvc
    2. Deploy the pod from the YAML file.
      kubectl create -f pv_pod_name.yaml
    3. Verify that the pod has been created.
      kubectl get pod
      NAME       READY   STATUS    RESTARTS   AGE
      pod_name   1/1     Ready     0          40s

Results

The pod that you configured uses persistent storage described in the persistent volume claim.