Kubernetes PersistentVolume and PersistentVolumeClaim to storage data
Introduction
In previous articles, I've guided you through using Kubernetes (K8s) to create resources from Docker images, resulting in stateless applications. This means no data is retained during usage, and restarting resources resets the application to its initial state.
If you're familiar with Docker, you might know about mounting volumes to save data externally and reattach it to a Docker container as needed. In Kubernetes, you can achieve a similar result using PersistentVolume (PV) and PersistentVolumeClaim (PVC) to build stateful applications.
Using PV and PVC in Kubernetes is crucial for real-world applications because they allow your data to persist across frequent deployments and restarts. Applications often face crashes or restarts due to issues, and having persistent data ensures seamless operation.
- PersistentVolume (PV): A storage resource provisioned by an administrator. It exists independently of the pod lifecycle
- PersistentVolumeClaim (PVC): A request for storage by a user. It binds to a PV and is attached to a pod for data storage.
In Summary:
- Create a PV with a specified volume configuration.
- Create a PVC to bind to the PV.
- Configure your Pod to mount the volume from the PVC for data storage.
Preparing the Docker Image
You can use the following code block to build a NodeJS TypeScript application for this guide:
Note: Make sure to create a 'data' folder to mount the volume when starting the Pod.
Next, build the Docker image and push it to Google Container Registry or Docker Hub. You can also use an existing Docker image with similar functionality.
Practice K8s
To create the resources, you'll need to set up a cluster first. You can use Google Kubernetes Engine (GKE) or a local Kubernetes cluster with Kind.
After that, create a `deployment.yml` file with the following content:
- PersistentVolumeClaim: When you create a PersistentVolumeClaim (PVC), it will automatically bind to a PersistentVolume (PV) with a storage capacity of "2Gi".
- I've already written a guide on how to create a Deployment and Service, which you can find here.
- In the Deployment, there's an additional field called `volumes` which references the PVC defined above. The `volumeMounts` field specifies the volume name and the `mountPath`, which is the path defined when building the Docker Image.
To apply and create the resource:
Note: I've defined all resources in a single file for simplicity, but in practice, you should separate each resource into individual YAML files for better management.
Checking resource provision
Checking if PV and PVC have been created
Deployment results
Next, restart the deployment to test if the volume mounts successfully so that no data is lost.
Conclusion
The above is a simple example of using PV (PersistentVolume) and PVC (PersistentVolumeClaim) for data storage. In this example, I stored the data in a JSON file, but in a real-world scenario, you would likely store data in a database. The concept remains the same, and you can try implementing it yourself.
Happy coding!
Comments
Post a Comment