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
- Access your namespace in the vSphere Kubernetes environment.
- Verify that the storage classes are available.
- Create a persistent volume claim.
- 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
toReadWriteMany
.apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-pvc spec: accessModes: - ReadWriteOnce storageClassName: gold resources: requests: storage: 3Gi
- 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. - 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
- Create a YAML file that contains the persistent volume claim configuration.
- Create a pod that mounts your persistent volume.
- Create a YAML file that includes the persistent volume.
The file contains these parameters.
... volumes: - name: my-pvc persistentVolumeClaim: claimName: my-pvc
- Deploy the pod from the YAML file.
kubectl create -f pv_pod_name.yaml
- Verify that the pod has been created.
kubectl get pod
NAME READY STATUS RESTARTS AGE pod_name 1/1 Ready 0 40s
- Create a YAML file that includes the persistent volume.
Results
The pod that you configured uses persistent storage described in the persistent volume claim.