動態擴容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.
解決方法
:要支援動態擴容需要滿足兩個條件:
可以看到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動態擴容成功。