我们前面已经介绍过静态PV是没办法进行扩容的,而且我们在用NFS做持久化存储的时候了解到要用动态PV并做扩容操作需要Kubernetes底层支持的存储,这次我们就用Glusterfs做扩容测试。
需要准备好Glusterfs,搭建地址为:
(1)、创建namespace,以下测试都在这个namespace下
# kubectl create ns rookieops
namespace/rookieops created
(1)、创建Secret(glusterfs-secret.yaml)
apiVersion: v1
kind: Secret
metadata:
name: glusterfs-secret
namespace: rookieops
data:
key: YWRtaW5AUEBzc1cwcmQ=
type: kubernetes.io/glusterfs
创建secret对象
# kubectl apply -f glusterfs-secret.yaml
secret/glusterfs-secret created
# kubectl get secret -n rookieops
NAME TYPE DATA AGE
default-token-s7d65 kubernetes.io/service-account-token 3 2m26s
glusterfs-secret kubernetes.io/glusterfs 1 5s
(2)、创建StorageClass(glusterfs-storageclass.yaml)
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: glusterfs-storageclass
namespace: rookieops
parameters:
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/glusterfs
reclaimPolicy: Delete
allowVolumeExpansion: true
创建storageclass对象
# kubectl apply -f glusterfs-storageclass.yaml
storageclass.storage.k8s.io/glusterfs-storageclass created
# kubectl get sc -n rookieops
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
glusterfs-storageclass kubernetes.io/glusterfs Delete Immediate false 9s
(3)、创建PVC(glusterfs-pvc.yaml)
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: glusterfs-pvc
namespace: rookieops
annotations:
volume.beta.kubernetes.io/storage-class: glusterfs-storageclass
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
创建PVC对象
# kubectl apply -f glusterfs-pvc.yaml
persistentvolumeclaim/glusterfs-pvc created
# kubectl get pvc -n rookieops
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
glusterfs-pvc Bound pvc-cfae01a0-cb37-4fcc-8640-8ef604ae3874 1Gi RWO glusterfs-storageclass 46s
(4)、然后我们对其进行扩容,直接在PVC的YAML文件中修改其大小
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: glusterfs-pvc
namespace: rookieops
annotations:
volume.beta.kubernetes.io/storage-class: glusterfs-storageclass
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
然后更新PVC对象
# kubectl apply -f glusterfs-pvc.yaml
# kubectl get pvc -n rookieops
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
glusterfs-pvc Bound pvc-29c667db-b103-49f8-9b2f-87ca5d29f751 2Gi RWO glusterfs-storageclass 71s
我们describe以下PVC
Finalizers: [kubernetes.io/pvc-protection]
Capacity: 2Gi
Access Modes: RWO
VolumeMode: Filesystem
Mounted 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/glusterfs
Normal VolumeResizeSuccessful 3s volume_expand ExpandVolume succeeded for volume rookieops/glusterfs-pvc
可以看到event信息中有扩展成功。
既然可以扩容,那么可以缩容吗?我们将上面的YAML文件中的storage改小试试
# kubectl apply -f glusterfs-pvc.yaml
The PersistentVolumeClaim "glusterfs-pvc" is invalid: spec.resources.requests.storage: Forbidden: field can not be less than previous value
然后报错,不允许缩容。
结论:如果动态PV要进行扩容需要满足以下条件:
- 需要是kubernetes支持的存储,开发能力强的也可以自己开发使其支持第三方存储
- 需要在sc中开启allowVolumeExpansion: true
- 存储设备要有足够的存储能力