What are Persistent Volumes in k8s?
In Kubernetes, a Persistent Volume (PV) is a piece of storage in the cluster that has been provisioned by an administrator. A Persistent Volume Claim (PVC) is a request for storage by a user. The PVC references the PV, and the PV is bound to a specific node. Read official documentation of Persistent Volumes.
Task 1:
Add a Persistent Volume to your Deployment todo app.
- Create a Persistent Volume using a file on your node.
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-todo-app
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/tmp/data"
- Apply the updates using the below command:
kubectl apply -f pv.yml
- Create a Persistent Volume Claim that references the Persistent Volume.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-todo-app
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
- Apply the updates using the below command:
kubectl apply -f pvc.yml
- Update your deployment.yml file to include the Persistent Volume Claim. After Applying pv.yml, and pvc.yml your deployment file looks like this:
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-app-deployment
spec:
replicas: 1
selector:
matchLabels:
app: todo-app
template:
metadata:
labels:
app: todo-app
spec:
containers:
- name: todo-app
image: trainwithshubham/django-todo:latest
ports:
- containerPort: 8000
volumeMounts:
- name: todo-app-data
mountPath: /app
volumes:
- name: todo-app-data
persistentVolumeClaim:
claimName: pvc-todo-app
- Apply the updated deployment using the command:
kubectl apply -f deployment.yml
- Verify that the Persistent Volume has been added to your Deployment by checking the status of the Pods and Persistent Volumes in your cluster. Use this commands
kubectl get pods
,kubectl get pv
Task 2:
Accessing data in the Persistent Volume.
- Connect to a Pod in your Deployment using the command:
kubectl exec -it <pod_name> -- sh
- Verify that you can access the data stored in the Persistent Volume from within the Pod.
Now, let's confirm our ability to access stored data. We'll initiate the auto-scaling feature of the Kubernetes deployment by deleting the existing pod. Afterward, we'll connect to the newly created pod and verify whether the file we previously created in the initial pod is still present.
Thank you!