In this tutorial, we are going to learn how to configure persistent storage in Kubernetes.
By the end of this tutorial, you will be able to:
- Understand persistent storage and its importance in Kubernetes.
- Set up and configure persistent storage in Kubernetes.
- Understand how to choose the best method for persistent storage based on your requirements.
To follow this tutorial, you should have:
- Basic knowledge of Kubernetes and its components.
- A running Kubernetes cluster to practice the examples. You can use Minikube for a local setup or any cloud-based Kubernetes service.
In Kubernetes, a volume can be thought of as a directory, possibly with some data in it, which is accessible to the Containers in a Pod. A Kubernetes volume lives as long as the Pod that encloses it is alive. Hence, a volume outlives any Containers that run within the Pod, and data is preserved across Container restarts.
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.
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.
Let's create a Persistent Volume and a Persistent Volume Claim.
pv.yaml
and add the following content:apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 1Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: slow
hostPath:
path: /tmp/data
This YAML file defines a PersistentVolume named my-pv
with a storage capacity of 1Gi
, which is accessible in ReadWriteOnce
mode. The hostPath
volume mounts the directory from the host node's filesystem.
pvc.yaml
and add the following content:apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
storageClassName: slow
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
This YAML file defines a PersistentVolumeClaim named my-pvc
, which requests for 1Gi
storage that is accessible in ReadWriteOnce
mode.
kubectl apply -f pv.yaml
kubectl apply -f pvc.yaml
This will create the PersistentVolume and PersistentVolumeClaim in your Kubernetes cluster.
In this tutorial, we have learned about:
Next steps:
- Learn about StatefulSets in Kubernetes, which are intended to be used with stateful applications and distributed systems.
- Explore Dynamic Volume Provisioning in Kubernetes, which allows storage volumes to be created on-demand.
Additional resources:
- Kubernetes Documentation - Storage
- Kubernetes Documentation - Persistent Volumes
Solutions:
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 2Gi
volumeMode: Filesystem
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: slow
hostPath:
path: /tmp/data
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
storageClassName: slow
accessModes:
- ReadWriteMany
resources:
requests:
storage: 2Gi
Remember, the storage class and access modes must match between the PV and PVC for a claim to be satisfied.