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.
Today's tasks:
Task 1:
Add a Persistent Volume to your Deployment todo app.
Create a file
pv.yaml
and write the code for Persistent Volume.apiVersion: v1 kind: PersistentVolume metadata: name: pv-todo-app spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain hostPath: path: "/tmp/data"
kubectl apply -f pv.yaml
Create a file
pvc.yaml
and write the code for Persistent Volume Claim.apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pvc-todo-app spec: accessModes: - ReadWriteOnce resources: requests: storage: 500Mi
- Create a file
deploymentvolumes.yaml
and write the code for Deployment.
- Create a file
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-app
labels:
app: todo
spec:
replicas: 1
selector:
matchLabels:
app: todo
template:
metadata:
labels:
app: todo
spec:
containers:
- name: todo
image: agnes1/todo
ports:
- containerPort: 8000
volumeMounts:
- name: todo-app-data
mountPath: /app
volumes:
- name: todo-app-data
persistentVolumeClaim:
claimName: pvc-todo-app
kubectl apply -f Deployment.yml
Verify that the Persistent Volume has been added to your Deployment by checking the Pods and Persistent Volumes status in your cluster. Use these commands.
Task 2:
Accessing data in the Persistent Volume,
kubectl exec -it <pod-name> -- /bin/bash
delete the pod that we used in the above step and create a new pod using the command kubectl apply -f deployment.yaml
Go inside a pod using kubectl exec command, then go to the app folder and check the todo.txt file.
Hope you find this article helpful!
Happy Learning!