Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Lab: 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

Student Material – Do Not Re-distribute. For any queries contact:


naushad.p.tech@gmail.com or https://www.linkedin.com/in/naushadpasha/
Create PersistentVolume:
In this lab, you will create a hostPath PersistentVolume. Kubernetes supports hostPath for
development and testing on a single-node cluster
Note: Ensure you have running cluster deployed
1. Ensure that you have logged-in as root user with password as linux on kube-master node.

1.1 Let us clone the git repository which contains manifests required for this exercise, by executing the
below command.

# git clone https://github.com/EyesOnCloud/k8s-storage.git


Output:

1.2 Let us view the manifest file.

# cat -n ~/k8s-storage/demo-pv.yaml

Output:

Student Material – Do Not Re-distribute. For any queries contact:


naushad.p.tech@gmail.com or https://www.linkedin.com/in/naushadpasha/
1.3 Let us create the the PersistentVolume with help of “demo-pv.yaml”, by executing the below
command.

# kubectl apply -f ~/k8s-storage/demo-pv.yaml

Output:

Note: "/mnt/data" directory will be created if doesn’t exist.


The configuration file specifies that the volume is at /mnt/data on the cluster's Node. The
configuration also specifies a size of 10 gibibytes and an access mode of ReadWriteOnce, which
means the volume can be mounted as read-write by a single Node. It defines the StorageClass
name manual for the PersistentVolume, which will be used to bind PersistentVolumeClaim
requests to this PersistentVolume.
1.4 Let us view information about the PersistentVolume, by executing the below command.
# kubectl get pv
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:

Student Material – Do Not Re-distribute. For any queries contact:


naushad.p.tech@gmail.com or https://www.linkedin.com/in/naushadpasha/
1.6 Let us create the persistentVolumeClaim using the manifest file “demo-pvc.yaml”, by
executing the below command.

# kubectl apply -f ~/k8s-storage/demo-pvc.yaml


Output:

1.7 Let us list the created pvc, by executing the below command.

# kubectl get pvc


Output:

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:

Student Material – Do Not Re-distribute. For any queries contact:


naushad.p.tech@gmail.com or https://www.linkedin.com/in/naushadpasha/
3. Let us create a pod using the manifest file persistent-pod1.yaml, by executing the below
command.

# kubectl apply -f ~/k8s-storage/persistent-pod1.yaml

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.

# ssh kube-node1 "echo "Hello from Kubernetes Storage" >


/mnt/data/index.html"

6. Let us access the shell of the Pod, by executing the below command
# kubectl exec -it persistent-pod1 -- /bin/bash

7. Let us access the index.html, by executing the below command.


# curl http://localhost/

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

Student Material – Do Not Re-distribute. For any queries contact:


naushad.p.tech@gmail.com or https://www.linkedin.com/in/naushadpasha/
9. Let us delete the pod named persistent-pod1, by executing the below command.

# kubectl delete pod persistent-pod1

Output:

10. Let us view the manifest file.

# 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

# kubectl apply -f ~/k8s-storage/persistent-pod2.yaml

Output:

Student Material – Do Not Re-distribute. For any queries contact:


naushad.p.tech@gmail.com or https://www.linkedin.com/in/naushadpasha/
12. Let us verify the pod status, by executing the below command.
# kubectl get pod persistent-pod2 -o wide
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

16. Let us clean up the resources, by executing the below commands.

# kubectl delete -f ~/k8s-storage/


Output:

Note: Error while deleting persistent-pod1 can be ignored, as we have manually deleted this
pod in above steps.

Student Material – Do Not Re-distribute. For any queries contact:


naushad.p.tech@gmail.com or https://www.linkedin.com/in/naushadpasha/

You might also like