Sub topic 11
Persistent Volumes with EBS and EFS
This chapter assumes you know pods, Deployments, namespaces, and basic kubectl. It also assumes you have a running EKS cluster with managed nodes and pod IAM set up (earlier chapters). Pods are ephemeral, so data written to a container's filesystem disappears when the pod is replaced. A PersistentVolume (PV) keeps data beyond a pod's life.
The Working Model
Three Kubernetes objects work together:
- A StorageClass describes how to create storage (which driver, which parameters).
- A PersistentVolumeClaim (PVC) is a pod's request for storage of a given size and access mode.
- A PersistentVolume is the actual volume. With dynamic provisioning, the driver creates it when a PVC needs it.
On EKS, the Amazon EBS CSI driver (installed as an EKS add-on) creates EBS volumes. The Amazon EFS CSI driver mounts EFS file systems. Neither is installed by default on current EKS versions. The EBS CSI driver's controller also needs AWS permissions to create and attach volumes. Grant them with IRSA or Pod Identity, using the AmazonEBSCSIDriverPolicy managed policy.
EBS or EFS
| Row | EBS | EFS |
|---|---|---|
| Type | Block volume | Shared NFS file system |
| Scope | One Availability Zone | Whole region, many AZs |
| Access mode | `ReadWriteOnce` | `ReadWriteMany` |
| Typical use | Databases, single-writer data | Shared content, many pods reading and writing |
An EBS volume can attach only to a node in its own AZ. This is the most common source of storage problems on EKS.
Creating an EBS-backed Volume
Define a StorageClass that uses gp3 volumes:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: ebs-gp3
provisioner: ebs.csi.aws.com
parameters:
type: gp3
volumeBindingMode: WaitForFirstConsumerWaitForFirstConsumer delays volume creation until a pod is scheduled. The volume is then created in the same AZ as the pod. With the default Immediate mode, the volume may be created in an AZ where the pod cannot run, and the pod stays Pending.
Next, request storage and mount it:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: data
spec:
accessModes:
- ReadWriteOnce
storageClassName: ebs-gp3
resources:
requests:
storage: 10Gi
---
apiVersion: v1
kind: Pod
metadata:
name: writer
spec:
containers:
- name: app
image: busybox:stable
command: ["sh", "-c", "date >> /data/log.txt; sleep 3600"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: dataApply it and check the result:
kubectl apply -f storage.yaml
kubectl get pvc data
kubectl get pvThe PVC shows Pending until the pod is scheduled, then Bound. This is expected with WaitForFirstConsumer.
Using EFS
For shared storage, create an EFS file system in the cluster's VPC, add mount targets in each AZ, and allow NFS (TCP 2049) from the node security group. Install the EFS CSI driver, then reference the file system in a StorageClass using provisioner: efs.csi.aws.com and fileSystemId. EFS volumes can be mounted by pods in many AZs at once.
Common Errors
- PVC stuck in
Pending: the EBS CSI driver is missing, or its role lacks permissions. Runkubectl describe pvcand check the controller logs. - Pod
Pendingwith a volume node affinity conflict: the volume's AZ has no available node. UseWaitForFirstConsumer. - EFS mount timeout: a security group or mount target is missing for that AZ.
- Data lost after deleting a PVC: the default
reclaimPolicyisDelete, which removes the EBS volume. SetRetainfor important data.
Exercise
A pod using an EBS-backed PVC stays Pending, and the event says the volume has a node affinity conflict. What is the likely cause, and what StorageClass setting prevents it?
Show answer
The EBS volume was created in one Availability Zone, but the pod was scheduled where no node exists in that zone. Setting volumeBindingMode to WaitForFirstConsumer makes the volume get created after scheduling, in the pod's AZ.