To change the storage class of a Persistent Volume Claim (PVC) in Kubernetes from efs
to gp2
, you’ll have to follow a series of steps. Kubernetes doesn’t allow you to change the storage class of an existing PVC directly. Instead, you would typically need to create a new PVC with the desired storage class and then copy the data from the old PVC to the new one.
Here’s a general approach:
- Backup Data: Before making any changes, ensure you have a backup of your data.
- Create a New PVC:Create a new PVC with the
gp2
storage class. Here’s an example manifest (new-pvc.yaml
):
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-new-pvc
spec:
accessModes:
- ReadWriteOnce
storageClassName: gp2
resources:
requests:
storage: <SIZE> # Replace <SIZE> with the desired storage size
- Apply the new PVC:
kubectl apply -f new-pvc.yaml
- Copy Data:One way to copy data between PVCs is to use temporary pods that mount both the old and new PVCs.
- Launch a temporary pod with both PVCs attached:
apiVersion: v1
kind: Pod
metadata:
name: temp-pod
spec:
containers:
- name: busybox
image: busybox
command:
- sleep
- "3600"
volumeMounts:
- name: old-pvc
mountPath: /old
- name: new-pvc
mountPath: /new
volumes:
- name: old-pvc
persistentVolumeClaim:
claimName: <OLD_PVC_NAME> # Replace with the name of your old PVC
- name: new-pvc
persistentVolumeClaim:
claimName: my-new-pvc
- Use
kubectl cp
orkubectl exec
withcp
to copy the data:kubectl exec temp-pod -- cp -r /old/* /new/
Remember to monitor the data transfer process and verify that all data has been correctly transferred to the new PVC before deleting the old one.
- DevSecOps Foundation Certification - June 14, 2025
- SRE Foundation Certification - June 14, 2025
- DevOps Foundation Certification - June 14, 2025