Persistent Volume Demo Using Local Storage¶
Let's see how we can create a Persistent Volume from a local storage using the hostPath provisioner.
Step 1: Verify Directory on Worker Nodes¶
We'll use /mnt/nginx directory on worker nodes for Persistent Volume.
Verify that /mnt/nginx directory doesn't exist on any worker node:
You'll find that there is no directory named nginx in /mnt directory on worker nodes.
Step 2: Create a Persistent Volume¶
Let's create a Persistent Volume from local storage using hostPath as follows:
Observe the following:
- The reclaim policy is set to
Retain - The access mode is set to
ReadWriteOnce - The PV uses the
hostPathprovisioner
Create Persistent Volume:
List Persistent Volumes:
Describe a persistent Volume:
kubectl describe pv <pv-name>
{OR}
kubectl describe persistentvolume <pv-name>
{OR}
kubectl describe persistentvolumes <pv-name>
Note
The default Reclaim Policy for Persistent Volume is Retain.
Step 3: Create a Persistent Volume Claim¶
Now, let's create a PVC to request storage needed for the pod we'll create in next step:
Create Persistent Volume Claim:
The default Storage Class will be used if you don't explicitly set storageClassName to "". We don't want that since we are not using dynamic provisioning.
A default storage class is automatically created when you create an Amazon EKS cluster. You'll notice that it is marked default.
View the default storage class:
You'll see the following output:
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
gp2 (default) kubernetes.io/aws-ebs Delete WaitForFirstConsumer false 30d
Also, all PVCs that have storageClassName set to "" can be bound only to PVs that have storageClassName also set to "".
List Persistent Volume Claims:
Describe a persistent Volume Claim:
kubectl describe pvc <pvc-name>
{OR}
kubectl describe persistentvolumeclaim <pv-name>
{OR}
kubectl describe persistentvolumeclaims <pv-name>
Observe the following:
- The status of
my-pvcisBound - The Persistent Volume Claim (PVC)
my-pvcis bound tomy-pvPersistent Volume (PV)
Step 4: Create Pods That Uses the Persistent Volume Claim¶
Let's create pods that uses the Persistent Volume Claim we created in the previous step. We'll use a deployment to create pods:
Note
While it is possible for multiple pods to utilize the same PVC, the practical implementation can be more intricate. When multiple pods need to access a Persistent Volume mounted with a ReadWriteOnce access mode, they must be scheduled on the same node to enable simultaneous access to the volume.
To keep it simple we'll create deployment with only 1 replica pod as follows:
Obeserve the following:
- The deployment creates pods with 1 replica
- Each pod has one container named
nginx - A volume named
my-volumeis created from thePersistent Volume Claim - The volume
my-volumeis mounted on/usr/share/nginx/htmldirectory of thenginxcontainer
Apply the manifest to create the deployment:
Step 5: Verify Deployment and Pods¶
Step 6: View the Page Served By Nginx Container¶
-
Open a shell session inside the
nginxcontainer of one of the pods: -
Get the nginx page:
You'll receive 403 error page because there is nothing at /usr/share/nginx/html.
Usually there is a default index.html file at /usr/share/nginx/html but since we mounted the local storage on worker node to /usr/share/nginx/html, the content of /usr/share/nginx/html in the nginx container is overwritten.
In Summary, whatever is present on /mnt/nginx on worker node, the same will be available to nginx container on /usr/share/nginx/html.
Step 7: Add the Nginx Page¶
Connect to the worker node where the pod is running using SSH or session manager.
You'll see the directory /mnt/nginx is created as soon as the pod comes up.
Create a file called index.html with the content below at /mnt/nginx:
Access the nginx page again:
# Open a shell session inside the same nginx container
kubectl exec -it <pod-name> -- bash
# Get the nginx page
curl localhost
You'll observe that the nginx serves the html page we created.
Step 8: Delete the Deployment and Persistent Volume Claim (PVC)¶
You need to delete the deployment before you can delete the PVC because pods uses the claim as volume.
-
Delete the deployment:
-
Delete the PVC:
Step 9: Verify the Status of Persistent Volume (PV)¶
List PVs:
You'll observe that the status of PV is Released because the claim bound to this PV has been deleted.
Step 10: Delete the Persistent Volume (PV)¶
Delete the PV we created:
Step 11: Verify That Data is Retained¶
Since the reclaim policy is set to the default value Retain, the data on worker node will be retained.
You can verify it by logging into the worker node.