Kubernetes: How to change pvc storage class from one to another?

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:

  1. Backup Data: Before making any changes, ensure you have a backup of your data.
  2. 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
  1. Apply the new PVC: kubectl apply -f new-pvc.yaml
  2. 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 or kubectl exec with cp 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.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x