troubleshoot:PVC动态扩容报错

时间:2022-12-11 17:07:30

动态扩容PVC的时候报错(kubectl edit pvc pvcname):“error: persistentvolumeclaims "pvvolume" could not be patched: persistentvolumeclaims "pvvolume" is forbidden: only dynamically provisioned pvc can be resized and the storageclass that provisions the pvc must support resize”

二.解决方法

配置一个10M大小的pvc,关于PVC的详细内容,请查看博客《Kubernetes(k8s)存储管理之数据卷volumes(四):持久卷Persistent Volume》https://www.cnblogs.com/renshengdezheli/p/16972289.html

[student@vms20 ~]$ vim 13.yaml

[student@vms20 ~]$ cat 13.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvvolume
spec:
  accessModes:
    - ReadWriteOnce
  volumeMode: Filesystem
  resources:
    requests:
      storage: 10Mi
  storageClassName: csi-hostpath-sc

创建PVC

[student@vms20 ~]$ kubectl apply -f 13.yaml 
persistentvolumeclaim/pvvolume created

[student@vms20 ~]$ kubectl get pvc
NAME       STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS      AGE
pvvolume   Bound    pvc-09b90084-aa96-4e10-a124-79c9af98ccc0   10Mi       RWO            csi-hostpath-sc   10s

[student@vms20 ~]$ kubectl get pvc -o wide
NAME       STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS      AGE   VOLUMEMODE
pvvolume   Bound    pvc-09b90084-aa96-4e10-a124-79c9af98ccc0   10Mi       RWO            csi-hostpath-sc   14s   Filesystem

把PVC挂载到pod上,并创建pod

[student@vms20 ~]$ vim 13pod.yaml

[student@vms20 ~]$ cat 13pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: web-server
spec:
  containers:
    - name: myfrontend
      image: nginx
      imagePullPolicy: IfNotPresent
      volumeMounts:
      - mountPath: "/usr/share/nginx/html"
        name: mypvc
  volumes:
    - name: mypvc
      persistentVolumeClaim:
        claimName: pvvolume

[student@vms20 ~]$ kubectl apply -f 13pod.yaml 
pod/web-server created

[student@vms20 ~]$ kubectl get pod | grep web-server
web-server                   1/1     Running   0             20s

修改pvc的容量为70Mi,PVC动态扩容报错如下:

[student@vms20 ~]$ kubectl get pvc 
NAME       STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS      AGE
pvvolume   Bound    pvc-09b90084-aa96-4e10-a124-79c9af98ccc0   10Mi       RWO            csi-hostpath-sc   14m

#动态扩容PVC报错
[student@vms20 ~]$ kubectl edit pvc pvvolume --record
Flag --record has been deprecated, --record will be removed in the future
error: persistentvolumeclaims "pvvolume" could not be patched: persistentvolumeclaims "pvvolume" 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 /tmp/kubectl-edit-857302533.yaml` to try this update again.

解决方法要支持动态扩容需要满足两个条件

  1. 后端底层存储支持卷扩展(后端存储保证足够资源) ;
  2. 需要在StorageClass对象中设置allowVolumeExpansion为true。

可以看到storageclass此时ALLOWVOLUMEEXPANSION参数为false。

[student@vms20 ~]$ kubectl get storageclass
NAME              PROVISIONER           RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
csi-hostpath-sc   hostpath.csi.k8s.io   Delete          Immediate           false                  277d

编辑csi-hostpath-sc,把ALLOWVOLUMEEXPANSION修改为true

#添加参数allowVolumeExpansion: true
#######
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
allowVolumeExpansion: true
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"storage.k8s.io/v1","kind":"StorageClass","metadata":{"annotations":{},"name":"csi-hostpath-sc"},"parameters":{"archiveOnDelete":"false"},"provisioner":"hostpath.csi.k8s.io"}
  creationTimestamp: "2021-11-04T10:54:48Z"
  name: csi-hostpath-sc
  resourceVersion: "177035"
  uid: a594f8fd-9c3d-49d3-85a4-085c89a7bb1c
parameters:
  archiveOnDelete: "false"
provisioner: hostpath.csi.k8s.io
reclaimPolicy: Delete
volumeBindingMode: Immediate

#######

[student@vms20 ~]$ kubectl edit storageclass csi-hostpath-sc
storageclass.storage.k8s.io/csi-hostpath-sc edited

#现在ALLOWVOLUMEEXPANSION变为true了
[student@vms20 ~]$ kubectl get storageclass
NAME              PROVISIONER           RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
csi-hostpath-sc   hostpath.csi.k8s.io   Delete          Immediate           true                   277d

再次PVC动态扩容

[student@vms20 ~]$ kubectl get pvc
NAME       STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS      AGE
pvvolume   Bound    pvc-09b90084-aa96-4e10-a124-79c9af98ccc0   10Mi       RWO            csi-hostpath-sc   25m

#现在pvc动态扩容:将所有位置的capacity:storage:10Mi修改为70Mi
[student@vms20 ~]$ kubectl edit pvc pvvolume --record
Flag --record has been deprecated, --record will be removed in the future
persistentvolumeclaim/pvvolume edited

#PVC动态扩容成功
[student@vms20 ~]$ kubectl get pvc
NAME       STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS      AGE
pvvolume   Bound    pvc-09b90084-aa96-4e10-a124-79c9af98ccc0   70Mi       RWO            csi-hostpath-sc   27m

自此,PVC动态扩容成功。