In this lesson, we will explore how to manage persistent data in Kubernetes using Volumes and PersistentVolumeClaims (PVCs). Understanding these concepts is crucial for any application that requires data to persist beyond the lifecycle of individual pods.
In Kubernetes, a Volume is a directory that is accessible to the containers in a pod. It provides a way for containers to share data and ensures that the data persists even if the container is restarted. Kubernetes supports several types of volumes, including:
A PersistentVolumeClaim (PVC) is a request for storage by a user. It allows you to request a specific amount of storage without having to know the details of the underlying storage infrastructure. When a PVC is created, Kubernetes looks for a matching PV to bind to it.
A PersistentVolume can be created using a YAML manifest. Here’s an example of a PV that uses NFS (Network File System):
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
nfs:
path: /path/on/nfs
server: nfs-server.example.com
Next, create a PVC that requests storage from the PV:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
Now, you can use the PVC in a pod definition:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx
volumeMounts:
- mountPath: /usr/share/nginx/html
name: my-volume
volumes:
- name: my-volume
persistentVolumeClaim:
claimName: my-pvc
ReadWriteOnce, ReadWriteMany).Avoid hardcoding storage details: Use PVCs to decouple your application from the underlying storage technology. Ignoring access modes: Ensure that the access modes specified in your PVC match those of the PV.
| Concept | Description |
|---|---|
| Volume | A directory accessible to containers in a pod. |
| PersistentVolume | A piece of storage in the cluster. |
| PersistentVolumeClaim | A request for storage by a user. |
Understanding these concepts will enable you to manage persistent storage effectively in your Kubernetes applications.
Create a PV using the provided YAML template. Make sure to adjust the path and server fields to your environment.
Create a PVC that requests 1Gi of storage. Use the YAML template provided in the lesson.
Modify the pod definition provided in the lesson to use your newly created PVC. Deploy the pod and verify that it can access the persistent storage.