In this blog, you will learn how to resize the persistent volume in AWS EKS cluster by using a custom storage class with the allowVolumeExpansion
parameter enabled.
Using Default StorageClass
In every EKS cluster, there is default Storage class with allowVolumeExpansion
false. Which means you cannot update a persistent volume with the default storage class.
Lets look at the default storage class to understand more.
Run the following command to get the default storageclass name.
kubectl get sc
The name of my default storage class is given below.
As you can see in the default storage class, the allowVolumeExpansion
parameter is set as false.
If we try to resize the persistence volume, which uses the default storage class, we will get the below forbidden error.
error: persistentvolumeclaims "expandable-pvc" could not be patched: persistentvolumeclaims "expandable-pvc" is forbidden: only dynamically provisioned pvc can be resized and the storageclass that provisions the pvc must support resize
You can run `kubectl replace -f /var/folders/w8/zfm6dvnj5ng6cmpv61_nydh00000gn/T/kubectl-edit-57474951.yaml` to try this update again.
Using Custom StorageClass
Let’s try to expand a PVC that uses custom storageclass with allowVolumeExpansion
parameter set as true.
Step 1: Create Custom StorageClass
Now, let’s create a custom storage class and create the PVC with the custom storageclass.
Create a YAML file sc.yaml and copy the below manifest file content
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: expandable-sc
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp2
allowVolumeExpansion: true
You can see in the above manifest file, the allowVolumeExpansion
is set as true.
The EBS volume type is set to gp2
, if you want to use other volume types refer to this guide.
Let’s create the PVC using the custom storageclass and try to expand the volume size.
Run the following command to create the custom storageclass
kubectl apply -f sc.yaml
Now, run the below command to check if the storageclass is created
kubectl get sc
The custom storageclass volumeBindingMode
is set as Immediate, which means the PVC will be created and bound with a PV immediately after it’s created.
Step 2: Create PVC
Create a manifest file pvc.yaml and copy the below PVC manifest content
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: expandable-pvc
spec:
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
resources:
requests:
storage: 1Gi
storageClassName: expandable-sc
You can see the storage requested is 1Gi
. Run the following command to create a PVC
kubectl apply -f pvc.yaml
Run the following command to check if the PVC is created.
As you can see, the PVC is in the bound state even without being claimed by a pod because the custom storageclass we created has the volumeBindingMode
as Immediate
.
Step 3: Deploy a Pod
Now, create a manifest file pod.yaml and copy the below pod deployment manifest content
apiVersion: v1
kind: Pod
metadata:
name: volume-test
spec:
containers:
- name: volume-test
image: nginx
volumeMounts:
- name: expandable-storage
mountPath: /data
volumes:
- name: expandable-storage
persistentVolumeClaim:
claimName: expandable-pvc
Run the following command to deploy the pod
kubectl apply -f pod.yaml
Step 4: Expand the Volume Size
Now, run the following command to edit the PVC to expand the volume size.
k edit pvc expandable-pvc
Then, change the storage request size to 2Gi
as shown below
Save the changes and exit.
Wait for some time, and the PV and PVC capacity will be increased, as shown below.
Now that the volume has been increased, it’s important to check if the filesystem’s size inside the pod has also increased.
Run the following command to check whether the filesystem size is increased.
kubectl exec -it volume-test -- df -h /data
You can see the filesystem size has been increased as well.
Conclusion
In this blog, you have seen what happens if we change the volume size in the EKS cluster using the default storage class, which has allowVolumeExpansion
parameter disabled.
Also, you have learned how to expand a volume size using a custom storage class, which has allowVolumeExpansion
parameter enabled.
Also, resizing volume is one of the important topic in CKA certification.