Lab11a - Working With PersistentVolume and PersistentVolumeClaim
Lab11a - Working With PersistentVolume and PersistentVolumeClaim
Introduction
The PersistentVolume subsystem provides an API for users and administrators that abstracts
details of how storage is provided from how it is consumed. To do this, there are two new API
resources: PersistentVolume and PersistentVolumeClaim.
A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an
administrator or dynamically provisioned using Storage Classes. It is a resource in the cluster
just like a node is a cluster resource. PVs are volume plugins like Volumes, but have a lifecycle
independent of any individual Pod that uses the PV. This API object captures the details of the
implementation of the storage, be that NFS, iSCSI, or a cloud-provider-specific storage system.
A PersistentVolumeClaim (PVC) is a request for storage by a user. It is similar to a Pod. Pods
consume node resources and PVCs consume PV resources. Pods can request specific levels of
resources (CPU and Memory). Claims can request specific size and access modes (e.g., they can
be mounted ReadWriteOnce, ReadOnlyMany or ReadWriteMany).
In this Lab, you will learn below items:
Objective:
• Create PersistentVolume.
• Create PersistentVolumeClaim.
• Use PersistentVolumeClaim.
• Cleanup
1.1 Let us clone the git repository which contains manifests required for this exercise, by executing the
below command.
# cat -n ~/k8s-storage/demo-pv.yaml
Output:
Output:
Create a PersistentVolumeClaim:
The next step is to create a PersistentVolumeClaim. Pods use PersistentVolumeClaims to
request physical storage. In this exercise, you will create a PersistentVolumeClaim that requests
a volume of at least three gibibytes that can provide read-write access for at least one Node.
1.5 Let us view the manifest file.
# cat -n ~/k8s-storage/demo-pvc.yaml
Output:
1.7 Let us list the created pvc, by executing the below command.
1.8 Let us now list the pv, by executing the below command.
# kubectl get pv
Output:
Note: The status of the PVC is now bound & it is claimed by demo-pv.
2. Let us view the manifest file.
# cat -n ~/k8s-storage/persistent-pod1.yaml
Output:
Output:
Note: The Pod's configuration file specifies a PersistentVolumeClaim, but it does not specify a
PersistentVolume. From the Pod's point of view, the claim is a volume.
4. Let us verify the status of the Pod, by executing the below command.
# kubectl get pod persistent-pod1 -o wide
Output:
5. Let us create the index.html file on node1, by executing the below command.
6. Let us access the shell of the Pod, by executing the below command
# kubectl exec -it persistent-pod1 -- /bin/bash
Output:
Note: The output shows the text that you wrote to the index.html file on the hostPath volume
You have successfully configured a Pod to use storage from a PersistentVolumeClaim.
8. Let’s exit from the pod, by executing the below pod.
# exit
Output:
# cat -n ~/k8s-storage/persistent-pod2.yaml
Output:
11. Let us create another pod using the manifest file persistent-pod2.yaml, by executing the
below command
Output:
13. Let us access the shell of the Pod, by executing the below command.
# kubectl exec -it persistent-pod2 -- /bin/bash
14. Let us access the index.html, by executing the below command.
# curl http://localhost/
Output:
Note: The output shows the text which was written to index.html file on the hostPath volume.
15. Exit from the pod, by executing the below command.
# exit
Note: Error while deleting persistent-pod1 can be ignored, as we have manually deleted this
pod in above steps.