Sub topic 11
Persistent Volumes with EBS and EFS
This chapter assumes you can read Kubernetes manifests and know the basics of pods, Deployments, and namespaces. You should also know what an EBS volume is, what an Availability Zone (AZ) is, and how IAM roles work. You should have a running EKS cluster with managed node groups from earlier chapters, and a way to give pods IAM permissions (IRSA or EKS Pod Identity).
Why pods need persistent volumes
A container's filesystem disappears when the pod is replaced. Databases, queues, and upload directories need data that outlives the pod. Kubernetes handles this with three objects:
- A PersistentVolume (PV) is a piece of storage in the cluster.
- A PersistentVolumeClaim (PVC) is a request for storage made by a workload.
- A StorageClass describes how to create storage on demand. This is called dynamic provisioning.
On EKS, the actual storage comes from AWS through Container Storage Interface (CSI) drivers. A CSI driver is a plugin that lets Kubernetes create, attach, and mount storage from an external system. The two you will use most are the EBS CSI driver and the EFS CSI driver.
Choosing between EBS and EFS
The two services behave very differently, and the choice follows from how your workload reads and writes data.
| Property | EBS (block storage) | EFS (NFS file system) |
|---|---|---|
| Scope | One Availability Zone | Whole region, across AZs |
| Access mode | ReadWriteOnce (one node at a time) | ReadWriteMany (many nodes at once) |
| Typical use | Databases, single-writer state | Shared content, ML datasets, CMS uploads |
| Latency | Low | Higher, network file system |
| Capacity | You choose a size | Grows and shrinks automatically |
| Fargate support | No | Yes (EFS only) |
If a single pod owns the data and needs fast disk, use EBS. If many pods on different nodes must read and write the same files, use EFS.
How the pieces connect
The driver has two parts. A controller runs as a Deployment and calls the EC2 API to create, attach, and delete volumes. A node component runs as a DaemonSet on every node and formats and mounts the device into the pod.
Installing the EBS CSI driver
The driver is not installed by default on current EKS clusters. Install it as an EKS add-on (covered in the previous chapter). The controller needs IAM permission to call EC2, so create a role for its service account first.
eksctl create iamserviceaccount \
--cluster demo \
--namespace kube-system \
--name ebs-csi-controller-sa \
--role-only \
--role-name AmazonEKS_EBS_CSI_DriverRole \
--attach-policy-arn arn:aws:iam::aws:policy/service-role/AmazonEBSCSIDriverPolicy \
--approve
eksctl create addon \
--cluster demo \
--name aws-ebs-csi-driver \
--service-account-role-arn arn:aws:iam::111122223333:role/AmazonEKS_EBS_CSI_DriverRoleReplace the account ID with your own. Verify that the pods are running:
kubectl get pods -n kube-system -l app.kubernetes.io/name=aws-ebs-csi-driverYou should see controller pods and one node pod per worker node.
A gp3 StorageClass
This StorageClass is from the tutorial plan. It creates gp3 volumes and delays creation until a pod is scheduled.
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: ebs-gp3
provisioner: ebs.csi.aws.com
parameters:
type: gp3
volumeBindingMode: WaitForFirstConsumerTwo settings matter here.
type: gp3selects the gp3 volume type. gp3 gives a baseline of 3,000 IOPS and 125 MiB/s regardless of size, and it costs less per GiB than gp2. You can raise IOPS and throughput separately with theiopsandthroughputparameters.volumeBindingMode: WaitForFirstConsumerpostpones volume creation until the scheduler has chosen a node. An EBS volume lives in one AZ. If the volume were created first, in a random zone, the pod might be scheduled in a different zone and fail to attach. Waiting means the volume is created in the zone of the chosen node.
You can also add allowVolumeExpansion: true so you can grow a volume later by editing the PVC. Volumes can be grown but never shrunk. Setting reclaimPolicy to Retain keeps the EBS volume after you delete the PVC. The default for dynamically provisioned volumes is Delete.
Using the volume in a workload
Create a PVC and a pod that writes to 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 watch the claim:
kubectl apply -f ebs-demo.yaml
kubectl get pvc data
kubectl get pods writerBecause of WaitForFirstConsumer, the PVC stays Pending until the pod is scheduled. That is expected. Once the pod runs, the PVC shows Bound. Delete the pod, recreate it, and cat /data/log.txt shows the earlier lines. The data survived because the PVC and its volume still exist.
StatefulSets and per-replica volumes
A Deployment with one PVC shares that claim among all replicas. With ReadWriteOnce, replicas on different nodes cannot mount it. For databases and similar workloads, use a StatefulSet with volumeClaimTemplates. Kubernetes then creates one PVC per replica, named like data-db-0 and data-db-1. Each pod keeps its own volume when it is rescheduled.
Shared storage with EFS
EFS is a managed NFS file system. The EFS CSI driver mounts it into pods, so many pods across nodes and AZs can read and write the same directory.
The setup has these steps:
- Create an EFS file system in the cluster's VPC, with a mount target in each AZ your nodes use.
- Allow inbound NFS (TCP 2049) to the mount targets from the node security group.
- Install the
aws-efs-csi-driveradd-on with an IAM role, as you did for EBS. - Create a StorageClass that points at the file system.
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: efs-sc
provisioner: efs.csi.aws.com
parameters:
provisioningMode: efs-ap
fileSystemId: fs-0123456789abcdef0
directoryPerms: "700"With provisioningMode: efs-ap, the driver creates an EFS access point for each PVC. An access point gives each claim its own root directory and POSIX identity. A PVC using this class sets accessModes: [ReadWriteMany] and requests a size. EFS does not enforce the size, since the file system grows as needed, but the field is required.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: shared
spec:
accessModes:
- ReadWriteMany
storageClassName: efs-sc
resources:
requests:
storage: 5GiCommon errors
- PVC stuck in Pending with no pod. With
WaitForFirstConsumerthis is normal. Create the pod that uses the claim. - Pending even with a pod. Run
kubectl describe pvc <name>. Messages such as "failed to provision volume" usually mean the controller lacks IAM permissions or the add-on is not installed. - Pod stuck in ContainerCreating with a "volume node affinity conflict" or attach error. The pod was scheduled in a different AZ from its EBS volume. This happens when the pod is rescheduled and no node in the volume's zone has capacity. Make sure your node groups span the zones where volumes exist.
- Multi-Attach error. A ReadWriteOnce EBS volume is still attached to the old node while a new pod starts on another node. It usually clears after the old attachment is released.
- EFS mount times out. Check that a mount target exists in the node's AZ and that the security group allows TCP 2049.
- Data lost after deleting a PVC. The default
Deletereclaim policy removes the EBS volume. UseRetainor take snapshots for data you cannot lose.
Edge cases to plan for
- EBS volumes cannot span AZs. If an AZ fails, the volume is unavailable there. For recovery, use EBS snapshots (with the CSI snapshot support) or replicate data at the application level.
- Fargate pods cannot use EBS volumes. Use EFS or choose managed node groups for those workloads.
- Resizing an EBS volume is limited by EC2: you can modify a given volume only once every six hours.
- Deleting a cluster does not always delete leftover volumes. Delete workloads and PVCs first, then check the EC2 console for orphaned volumes to avoid ongoing charges.
Exercise
A pod using a PVC from the ebs-gp3 StorageClass has been created, but kubectl get pvc shows Pending and the pod is Pending too. kubectl describe pvc reports a provisioning failure with an authorization error from EC2. What is the most likely cause, and how do you fix it?
Show answer
The EBS CSI controller does not have IAM permission to call the EC2 API, or the add-on is missing its role. Create an IAM role with the AmazonEBSCSIDriverPolicy for the ebs-csi-controller-sa service account (using IRSA or Pod Identity), set it on the aws-ebs-csi-driver add-on, and let the controller pods restart. The PVC then provisions on its own.
Exercise
Why does the ebs-gp3 StorageClass set volumeBindingMode: WaitForFirstConsumer, and what would go wrong with the default Immediate mode in a cluster that spans three AZs?
Show answer
An EBS volume exists in only one AZ. With Immediate, the volume is created before the scheduler picks a node, so it may land in a zone different from the one where the pod is placed. The pod then cannot attach the volume and stays stuck. WaitForFirstConsumer creates the volume in the AZ of the node chosen for the pod.
Exercise
Three replicas of a web app on different nodes must serve and upload files to the same directory. Which storage do you use, and which access mode do you set on the PVC?
Show answer
Use EFS with the EFS CSI driver and set accessModes to ReadWriteMany. EBS volumes are ReadWriteOnce and attach to one node at a time, so they cannot be shared across nodes.