What are Persistent Volumes in k8s
Persistent Volumes (PVs) in Kubernetes (k8s) are a way to manage storage resources in a cluster. They provide a mechanism for users to request and consume storage resources without having to know the details of the underlying storage infrastructure. PVs are a part of the Kubernetes storage architecture and are used to persist data beyond the lifecycle of individual pods.
Here are some key points about Persistent Volumes:
Abstraction: PVs abstract the details of how storage is provided, allowing developers to focus on application development rather than storage management.
Lifecycle: PVs have an independent lifecycle from pods. They exist beyond the life of a pod and can be reused by different pods.
Provisioning: PVs can be either statically or dynamically provisioned. Static provisioning involves pre-creating PVs by an administrator, while dynamic provisioning allows Kubernetes to automatically create PVs based on storage class definitions.
Binding: PVs are bound to Persistent Volume Claims (PVCs), which are requests for storage by users. Once a PV is bound to a PVC, it is considered "claimed" and can be used by a pod.
Access Modes: PVs support different access modes, such as ReadWriteOnce, ReadOnlyMany, and ReadWriteMany, which define how the volume can be accessed by pods.
Reclaim Policy: PVs have a reclaim policy that determines what happens to the volume after it is released by a PVC. Common policies include Retain, Recycle, and Delete.
By using Persistent Volumes, Kubernetes provides a flexible and efficient way to manage storage resources, ensuring data persistence and availability across the cluster.
Today’s Tasks:
Task 1: Add a Persistent Volume to Your Deployment Todo App
In this task, we will add a Persistent Volume to your todo-app Kubernetes deployment. Persistent storage is crucial for any app that needs to retain data, such as databases or file storage.
Steps:
Create a Persistent Volume (PV) using a YAML file on your node.
pv.yml:
apiVersion: v1 kind: PersistentVolume metadata: name: pv-todo-app spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain hostPath: path: "/tmp/data"
Create a Persistent Volume Claim (PVC) that requests storage from the PV.
pvc.yml:
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pvc-todo-app spec: accessModes: - ReadWriteOnce resources: requests: storage: 500Mi
Update the deployment file to use the Persistent Volume Claim.
deployment.yml:
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: rishikeshops/todo-app ports: - containerPort: 8000 volumeMounts: - name: todo-app-data mountPath: /app volumes: - name: todo-app-data persistentVolumeClaim: claimName: pvc-todo-app
Apply the Persistent Volume and Claim:
kubectl apply -f pv.yml kubectl apply -f pvc.yml kubectl apply -f deployment.yml
Verify the addition of Persistent Volume:
kubectl get pv kubectl get pvc kubectl get pods
Note: Remember to apply each YAML file separately before applying the deployment changes.
Task 2: Accessing Data in the Persistent Volume
Once the Persistent Volume is added to your app, it’s time to ensure that the data can be accessed from within the pod.
Steps:
Connect to a Pod in your deployment using this command:
kubectl exec -it <pod-name> -- /bin/bash
Verify Access to the data stored in the Persistent Volume from within the Pod:
Navigate to the mounted path (
/app
) inside the container.Check if the data is present and accessible.
Need help understanding how Persistent Volumes work? Check out this helpful video tutorial.
🎉 Conclusion
Today, we explored how to use Persistent Volumes to ensure that your Kubernetes applications can store data permanently, even across pod restarts. This is a crucial feature for any stateful application.