Persistent Volume and Persistent Volume Claim in Kubernetes
What is Persistent Volume and Persistent Volume Claim
A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. It is a resource in the cluster just like a node is a cluster resource. PVs are volume plugins like Volumes, but have a lifecycle independent of any individual Pod that uses the PV. This API object captures the details of the implementation of the storage, be that NFS, iSCSI, or a cloud-provider-specific storage system.
A PersistentVolumeClaim (PVC) is a request for storage by a user. It is similar to a Pod. Pods consume node resources and PVCs consume PV resources. Pods can request specific levels of resources (CPU and Memory). Claims can request specific size and access modes (e.g., they can be mounted ReadWriteOnce, ReadOnlyMany or ReadWriteMany, see AccessModes).
While PersistentVolumeClaims allow a user to consume abstract storage resources, it is common that users need PersistentVolumes with varying properties, such as performance, for different problems. Cluster administrators need to be able to offer a variety of PersistentVolumes that differ in more ways than size and access modes, without exposing users to the details of how those volumes are implemented. For these needs, there is the StorageClass resource.
More detail please see Configure a Pod to Use a PersistentVolume for Storage
Create a Perstistent Volume
In this exercise, you create a hostPath PersistentVolume. Kubernetes supports hostPath for development and testing on a single-node cluster. A hostPath PersistentVolume uses a file or directory on the Node to emulate network-attached storage.
In a production cluster, you would not use hostPath. Instead a cluster administrator would provision a network resource like a Google Compute Engine persistent disk, an NFS share, or an Amazon Elastic Block Store volume. Cluster administrators can also use StorageClasses to set up dynamic provisioning.
Create a pv-example.yaml
configuration file for the hostPath PersistentVolume:
|
|
kubectl
create persistent volumepv-example
with labelsapp:example
kubectl
defines the storage class namemanual
kubetcl
specifies a size of 10 gibibytes and access modes isReadWriteOnce
which means the volume can be mounted as read-write by a single node.kubectl
specifies that the volume is at/var/lib/data
on the cluster node.
Create a persistent volume
|
|
Get PersistentVolume was created
|
|
We should get an output
|
|
Create a Persistent Volume Claim
Pods use PersistentVolumeClaims to request physical storage. In this exercise, you create a PersistentVolumeClaim that requests a volume of at least three gibibytes that can provide read-write access for at least one Node.
Create a pvc-example.yaml
configuration file and fill with following code :
|
|
Create a persistent volume claim
|
|
Get PersistentVolume was created
|
|
We should get an output
|
|
Check if persistent volume was claimed
|
|
We should get an output shows a STATUS
of Bound
and CLAIM
of default/pv-claim-example
|
|
Create a Pod
Create a pod pod-uses-pvc-example.yaml
configuration file that uses the PersistentVolumeClaim as a volume
|
|
Create the pod
|
|
Check pod is running
|
|
Get cluster where pods is running
|
|
We should get an output
|
|
Get external IP for gke-rattlesnake-cluster-default-pool-5da13591-6wb4
node pool
|
|
Then, ssh into cluster node, then create directory /var/lib/data
and create file index.html
|
|
Go to a shell to the container running in your Pod :
|
|
In your shell, verify that nginx is serving the index.html file from the hostPath volume :
|
|
We should get an output Hello from Kubernetes storage
Implement Node App Web Service and Database using MySQL with Persistent Volume
We continue from Learning Kubernetes with example Node.js app with MySQL database.
Create configuration file app-and-database.yml
then fill code like following below :
- Create Persistent Volume and Persistent Volume Claim
|
|
- Create Node.js app deployment
|
|
- Create database MySQL deployment with PersistentVolumeClaim
|
|
- Expose App and DB with Service
|
|
- Create Ingress for App
|
|
Apply configuration file
|
|
Create database and table on running container
Run container with creating a pod
|
|
Create database, table and insert value
|
|
Get exposed address Ingress
|
|
We should get an output
|
|
Test deployment by hitting with curl
to ingress address
|
|
We should get an output
|
|
Full Example Code
You can see my example here
|
|
Thankyou
LogRocket - Node.js, Express.js, and MySQL: A step-by-step REST API example
SQLHack - SQL Database on Kubernetes: Considerations and Best Practices
kubernetes.io - Persistent Volumes
kubernetes.io - Configure a Pod to Use a PersistentVolume for Storage