SecureTech
SecureTech

Reputation: 259

Issue in resizing PVC: Only dynamically provisioned pvc can be resized and the storageclass that provisions the pvc must support resize

I am trying to create pvc which can be resized later on demand.

I am using this code from github for mongodb pvc.

I followed all the steps and pvc is created as below:

PS C:\Users> minikube kubectl -- get pvc
NAME         STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mongo-data   Bound     pvc-4a22efc0-9fd3-42dc-80a7-db86e8f2c189   1Gi        RWO            standard       32m

I edited the file mongodb-pvc.yaml and changed the storage field to 2 GB. storage: 2Gi and then again executed kubectl apply command and got below error:

  PS C:\Users> minikube kubectl -- apply -f mongodb-pvc.yaml
    Error from server (Forbidden): error when applying patch:
    {"metadata":{"annotations":{"kubectl.kubernetes.io/last-applied-configuration":"{\"apiVersion\":\"v1\",\"kind\":\"PersistentVolumeClaim\",\"metadata\":{\"annotations\":{},\"name\":\"mongo-data\",\"namespace\":\"default\"},\"spec\":{\"accessModes\":[\"ReadWriteOnce\"],\"resources\":{\"requests\":{\"storage\":\"2Gi\"}}}}\n"}},"spec":{"resources":{"requests":{"storage":"2Gi"}}}}
    to:
    Resource: "/v1, Resource=persistentvolumeclaims", GroupVersionKind: "/v1, Kind=PersistentVolumeClaim"
    Name: "mongo-data", Namespace: "default"
    for: "mongodb-pvc.yaml": persistentvolumeclaims "mongo-data" is forbidden: only dynamically provisioned pvc can be resized and the storageclass that provisions the PVC must support resize

I tried using the storageclass also, but unable to map it correctly.

Please help, I am a beginner in Kubernetes and especially in PVC.

Upvotes: 4

Views: 12619

Answers (3)

Artem Pavlovskii
Artem Pavlovskii

Reputation: 29

  1. Learn which persistent volume is linked to your pvc: kubectl get pv. Then see the CLAIM column.
  2. Learn which storage class your pv uses. kubectl get pv <your_pv> -o yaml | grep storageClassName.
  3. Edit storage class kubectl edit sc <your_sc>. Add allowVolumeExpansion: true to the end of the file as proposed above.

Now you can resize your pvc.

Upvotes: 0

thibault ketterer
thibault ketterer

Reputation: 1024

I wrote a script for it

This should work on minikube and k3s

  • get the storage class name

  • enable volume exapansion on it

    query='{.items[?(@.metadata.annotations.storageclass\.kubernetes\.io/is-default-class=="true")].metadata.name}'
    default_sc=$(kubectl get sc -o=jsonpath="$query")
    
    echo patching storage class "[$default_sc]"
    
    kubectl patch storageclass $default_sc -p '{"allowVolumeExpansion": true}'
    

Upvotes: 0

Fariya Rahmat
Fariya Rahmat

Reputation: 3270

In order to resize a PVC, it is required to have the attribute allowVolumeExpansion set to “true” in your storage class definition. Otherwise, you won't be able to perform the update.

You can follow these steps and add this attribute to its YAML:

  1. In Kubernetes Engine -> Storage -> Storage Classes, select the storage class that you are using for your PVC.

  2. Go to its YAML file and add the instruction. As is explained in the following documentation:

     allowVolumeExpansion: true
    

Once the YAML file is modified, try to perform the resize of the PVC once again

Upvotes: 14

Related Questions