我们前面已经介绍过静态PV是没办法进行扩容的,而且我们在用NFS做持久化存储的时候了解到要用动态PV并做扩容操作需要Kubernetes底层支持的存储,这次我们就用Glusterfs做扩容测试。

    需要准备好Glusterfs,搭建地址为:

    (1)、创建namespace,以下测试都在这个namespace下

    1. # kubectl create ns rookieops
    2. namespace/rookieops created

    (1)、创建Secret(glusterfs-secret.yaml)

    1. apiVersion: v1
    2. kind: Secret
    3. metadata:
    4. name: glusterfs-secret
    5. namespace: rookieops
    6. data:
    7. key: YWRtaW5AUEBzc1cwcmQ=
    8. type: kubernetes.io/glusterfs

    创建secret对象

    1. # kubectl apply -f glusterfs-secret.yaml
    2. secret/glusterfs-secret created
    3. # kubectl get secret -n rookieops
    4. NAME TYPE DATA AGE
    5. default-token-s7d65 kubernetes.io/service-account-token 3 2m26s
    6. glusterfs-secret kubernetes.io/glusterfs 1 5s

    (2)、创建StorageClass(glusterfs-storageclass.yaml)

    1. apiVersion: storage.k8s.io/v1
    2. kind: StorageClass
    3. metadata:
    4. name: glusterfs-storageclass
    5. namespace: rookieops
    6. parameters:
    7. resturl: "http://10.1.10.128:48080"
    8. clusterid: "cca360f44db482f03297a151886eea19"
    9. restauthenabled: "true" #若heketi开启认证此处也必须开启auth认证
    10. restuser: "admin"
    11. secretName: "glusterfs-secret" #name/namespace与secret资源中定义一致
    12. secretNamespace: "rookieops"
    13. volumetype: "replicate:3"
    14. provisioner: kubernetes.io/glusterfs
    15. reclaimPolicy: Delete
    16. allowVolumeExpansion: true

    创建storageclass对象

    1. # kubectl apply -f glusterfs-storageclass.yaml
    2. storageclass.storage.k8s.io/glusterfs-storageclass created
    3. # kubectl get sc -n rookieops
    4. NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
    5. glusterfs-storageclass kubernetes.io/glusterfs Delete Immediate false 9s

    (3)、创建PVC(glusterfs-pvc.yaml)

    1. apiVersion: v1
    2. kind: PersistentVolumeClaim
    3. metadata:
    4. name: glusterfs-pvc
    5. namespace: rookieops
    6. annotations:
    7. volume.beta.kubernetes.io/storage-class: glusterfs-storageclass
    8. spec:
    9. accessModes:
    10. - ReadWriteOnce
    11. resources:
    12. requests:
    13. storage: 1Gi

    创建PVC对象

    1. # kubectl apply -f glusterfs-pvc.yaml
    2. persistentvolumeclaim/glusterfs-pvc created
    3. # kubectl get pvc -n rookieops
    4. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
    5. glusterfs-pvc Bound pvc-cfae01a0-cb37-4fcc-8640-8ef604ae3874 1Gi RWO glusterfs-storageclass 46s

    (4)、然后我们对其进行扩容,直接在PVC的YAML文件中修改其大小

    1. apiVersion: v1
    2. kind: PersistentVolumeClaim
    3. metadata:
    4. name: glusterfs-pvc
    5. namespace: rookieops
    6. annotations:
    7. volume.beta.kubernetes.io/storage-class: glusterfs-storageclass
    8. spec:
    9. accessModes:
    10. - ReadWriteOnce
    11. resources:
    12. requests:
    13. storage: 2Gi

    然后更新PVC对象

    1. # kubectl apply -f glusterfs-pvc.yaml
    2. # kubectl get pvc -n rookieops
    3. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
    4. glusterfs-pvc Bound pvc-29c667db-b103-49f8-9b2f-87ca5d29f751 2Gi RWO glusterfs-storageclass 71s

    我们describe以下PVC

    1. Finalizers: [kubernetes.io/pvc-protection]
    2. Capacity: 2Gi
    3. Access Modes: RWO
    4. VolumeMode: Filesystem
    5. Mounted By: <none>
    6. Events:
    7. Type Reason Age From Message
    8. ---- ------ ---- ---- -------
    9. Normal ProvisioningSucceeded 48s persistentvolume-controller Successfully provisioned volume pvc-29c667db-b103-49f8-9b2f-87ca5d29f751 using kubernetes.io/glusterfs
    10. Normal VolumeResizeSuccessful 3s volume_expand ExpandVolume succeeded for volume rookieops/glusterfs-pvc

    可以看到event信息中有扩展成功。

    既然可以扩容,那么可以缩容吗?我们将上面的YAML文件中的storage改小试试

    1. # kubectl apply -f glusterfs-pvc.yaml
    2. The PersistentVolumeClaim "glusterfs-pvc" is invalid: spec.resources.requests.storage: Forbidden: field can not be less than previous value

    然后报错,不允许缩容。

    结论:如果动态PV要进行扩容需要满足以下条件:

    • 需要是kubernetes支持的存储,开发能力强的也可以自己开发使其支持第三方存储
    • 需要在sc中开启allowVolumeExpansion: true
    • 存储设备要有足够的存储能力