我们前面已经介绍过静态PV是没办法进行扩容的,而且我们在用NFS做持久化存储的时候了解到要用动态PV并做扩容操作需要Kubernetes底层支持的存储,这次我们就用Glusterfs做扩容测试。
需要准备好Glusterfs,搭建地址为:
(1)、创建namespace,以下测试都在这个namespace下
# kubectl create ns rookieopsnamespace/rookieops created
(1)、创建Secret(glusterfs-secret.yaml)
apiVersion: v1kind: Secretmetadata:name: glusterfs-secretnamespace: rookieopsdata:key: YWRtaW5AUEBzc1cwcmQ=type: kubernetes.io/glusterfs
创建secret对象
# kubectl apply -f glusterfs-secret.yamlsecret/glusterfs-secret created# kubectl get secret -n rookieopsNAME TYPE DATA AGEdefault-token-s7d65 kubernetes.io/service-account-token 3 2m26sglusterfs-secret kubernetes.io/glusterfs 1 5s
(2)、创建StorageClass(glusterfs-storageclass.yaml)
apiVersion: storage.k8s.io/v1kind: StorageClassmetadata:name: glusterfs-storageclassnamespace: rookieopsparameters:resturl: "http://10.1.10.128:48080"clusterid: "cca360f44db482f03297a151886eea19"restauthenabled: "true" #若heketi开启认证此处也必须开启auth认证restuser: "admin"secretName: "glusterfs-secret" #name/namespace与secret资源中定义一致secretNamespace: "rookieops"volumetype: "replicate:3"provisioner: kubernetes.io/glusterfsreclaimPolicy: DeleteallowVolumeExpansion: true
创建storageclass对象
# kubectl apply -f glusterfs-storageclass.yamlstorageclass.storage.k8s.io/glusterfs-storageclass created# kubectl get sc -n rookieopsNAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGEglusterfs-storageclass kubernetes.io/glusterfs Delete Immediate false 9s
(3)、创建PVC(glusterfs-pvc.yaml)
apiVersion: v1kind: PersistentVolumeClaimmetadata:name: glusterfs-pvcnamespace: rookieopsannotations:volume.beta.kubernetes.io/storage-class: glusterfs-storageclassspec:accessModes:- ReadWriteOnceresources:requests:storage: 1Gi
创建PVC对象
# kubectl apply -f glusterfs-pvc.yamlpersistentvolumeclaim/glusterfs-pvc created# kubectl get pvc -n rookieopsNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEglusterfs-pvc Bound pvc-cfae01a0-cb37-4fcc-8640-8ef604ae3874 1Gi RWO glusterfs-storageclass 46s
(4)、然后我们对其进行扩容,直接在PVC的YAML文件中修改其大小
apiVersion: v1kind: PersistentVolumeClaimmetadata:name: glusterfs-pvcnamespace: rookieopsannotations:volume.beta.kubernetes.io/storage-class: glusterfs-storageclassspec:accessModes:- ReadWriteOnceresources:requests:storage: 2Gi
然后更新PVC对象
# kubectl apply -f glusterfs-pvc.yaml# kubectl get pvc -n rookieopsNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEglusterfs-pvc Bound pvc-29c667db-b103-49f8-9b2f-87ca5d29f751 2Gi RWO glusterfs-storageclass 71s
我们describe以下PVC
Finalizers: [kubernetes.io/pvc-protection]Capacity: 2GiAccess Modes: RWOVolumeMode: FilesystemMounted By: <none>Events:Type Reason Age From Message---- ------ ---- ---- -------Normal ProvisioningSucceeded 48s persistentvolume-controller Successfully provisioned volume pvc-29c667db-b103-49f8-9b2f-87ca5d29f751 using kubernetes.io/glusterfsNormal VolumeResizeSuccessful 3s volume_expand ExpandVolume succeeded for volume rookieops/glusterfs-pvc
可以看到event信息中有扩展成功。
既然可以扩容,那么可以缩容吗?我们将上面的YAML文件中的storage改小试试
# kubectl apply -f glusterfs-pvc.yamlThe PersistentVolumeClaim "glusterfs-pvc" is invalid: spec.resources.requests.storage: Forbidden: field can not be less than previous value
然后报错,不允许缩容。
结论:如果动态PV要进行扩容需要满足以下条件:
- 需要是kubernetes支持的存储,开发能力强的也可以自己开发使其支持第三方存储
- 需要在sc中开启allowVolumeExpansion: true
- 存储设备要有足够的存储能力
