全部产品
Search
文档中心

容器服务 Kubernetes 版 ACK:配置云盘自动扩容策略实现自动扩容

更新时间:Feb 24, 2025

ACK支持自动扩容云盘存储卷,您可以通过CRD的方式定义云盘自动扩容策略,在存储卷的使用率高于某个阈值时,实现自动扩容。本文介绍如何配置云盘自动扩容策略,并验证云盘存储卷的自动扩容。

适用范围

本文通过CRD的方式创建自动扩容策略(StorageAutoScalerPolicy),以此实现自动扩容云盘存储卷。该方式的适用范围如下:

  • 云盘对应的PV所绑定的PVC关联了StorageClass,并且StorageClass中已配置allowVolumeExpansion: true

    说明

    ACK默认提供的StorageClass已配置allowVolumeExpansion: true,您自行创建的StorageClass需要自行配置。StorageClass不支持修改属性,只能新建。

  • 云盘挂载的应用Pod需处于Running状态。

  • 如果云盘类型为普通云盘,则不支持使用该方式扩容。

  • 如果集群版本低于1.16,则不支持使用该方式扩容。如需升级集群,请参见升级集群

  • 该方式依赖云盘的Resize能力。更多信息,请参见ResizeDisk

注意事项

触发扩容的最大时间间隔为2 min,云盘扩容时间为1 min,请勿在3 min内将云盘写满。

步骤一:配置storage-operator组件开启自动扩容

  1. 确认集群中已安装storage-operator组件,且组件版本不低于v1.18.8.28-18cca7b-aliyun。

    说明

    storage-operator组件在集群中默认安装。您可以在集群管理页的左侧导航栏选择运维管理 > 组件管理,在存储页签下确认storage-operator组件的安装情况和版本信息。更多信息,请参见管理storage-operator组件

  2. 连接集群,执行以下命令修改storage-operator的ConfigMap文件,开启自动扩容功能。

    storage-operator组件中默认的storage-auto-expander负责自动扩容存储资源。命令示例如下:

    kubectl patch configmap/storage-operator \
      -n kube-system \
      --type merge \
      -p '{"data":{"storage-auto-expander":"{\"imageRep\":\"acs/storage-auto-expander\",\"imageTag\":\"\",\"install\":\"true\",\"template\":\"/acs/templates/storage-auto-expander/install.yaml\",\"type\":\"deployment\"}"}}'

步骤二:配置云盘自动扩容策略

  1. 查看StorageClass,确认已开启扩容。

    kubectl get sc
    说明

    推荐您使用ACK集群中默认创建的名为alicloud-disk-topology-alltype的StorageClass,该StorageClass能自适应为您选择云盘类型,避免因实例规格限制,或所在可用区云盘库存不足导致云盘无法创建。关于如何创建StorageClass,请参见使用云盘动态存储卷

    预期返回如下,ALLOWVOLUMEEXPANSION的配置为true表示已开启扩容。

    NAME                                    PROVISIONER                       RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
    alicloud-disk-efficiency                diskplugin.csi.alibabacloud.com   Delete          Immediate              true                   26h
    alicloud-disk-essd                      diskplugin.csi.alibabacloud.com   Delete          Immediate              true                   26h
    alicloud-disk-ssd                       diskplugin.csi.alibabacloud.com   Delete          Immediate              true                   26h
    alicloud-disk-topology-alltype          diskplugin.csi.alibabacloud.com   Delete          WaitForFirstConsumer   true                   26h
  2. 通过CRD创建自动扩容策略。

    1. 使用以下YAML,创建StorageAutoScalerPolicy.yaml文件。

      apiVersion: storage.alibabacloud.com/v1alpha1
      kind: StorageAutoScalerPolicy
      metadata:
        name: hybrid-expand-policy
      spec:
        pvcSelector:
          matchLabels:
            app: nginx
        namespaces:
          - default
          - nginx
        conditions:
          - name: condition1
            key: volume-capacity-used-percentage
            operator: Gt
            values:
              - "80"
        actions:
          - name: action1
            type: volume-expand
            params:
              scale: 50Gi
              limits: 100Gi
          - name: action2
            type: volume-expand
            params:
              scale: 50%
              limits: 300Gi

      请根据您的应用和PVC情况进行配置。相关参数说明如下:

      参数

      说明

      pvcSelector

      通过Label筛选目标PVC。本示例为app: nginx

      namespaces

      目标PVC所在的命名空间,多个命名空间时为或逻辑。若不配置,默认为default。

      conditions

      触发规则的条件,多个condition时为与逻辑。每个condition包含以下参数:

      • name:condition的名称,自定义。

      • key:定义一个Metric的类型。volume-capacity-used-percentage表示容量使用百分比。

      • operator:定义规则,包含Gt(大于)、Lt(小于)、Eq(等于)或Ne(不等于),不限制英文字母大小写。

      • values:规则的具体数值。

      本示例表示当PVC容量的使用率高于80%时会触发action。

      actions

      满足condition时执行的操作,可以是多个操作。包含以下参数:

      • name:action的名称,自定义。

      • type:表示操作类型,目前只支持volume-expand,表示扩容。

      • params:表示操作参数。其中scale表示扩容的大小,单位为GiB,也可使用百分比;limits表示PVC在此action中的最大限制。

      存在多个action时,则从首个action开始匹配,执行首个满足条件的action,其余action跳过不执行。

      例如,本示例中的action1如果满足条件,则执行action1,不会执行action2。当action1不满足条件时,则执行action2。

      • action1表示云盘容量<100 GiB时,触发扩容时每次增加50 GiB,最大扩容到100 GiB。

      • action2表示当云盘容量≥100 GiB且<300 GiB时,触发扩容时每次增加当前容量的50%,即每次扩容后的总容量为扩容前容量的150%,最大扩容到300 GiB。

    2. 创建自动扩容策略。

      kubectl create -f StorageAutoScalerPolicy.yaml

步骤三:验证云盘的自动扩容

  1. 创建用于测试自动扩容的StatefulSet。

    1. 使用以下内容,创建StatefulSet.yaml文件。

      以下YAML将创建一个StatefulSet,该StatefulSet包含一个Pod,Pod挂载了一个25 GiB的云盘,挂载路径为/data

      apiVersion: apps/v1
      kind: StatefulSet
      metadata:
        name: nginx
      spec:
        selector:
          matchLabels:
            app: nginx
        serviceName: nginx
        replicas: 1
        template:
          metadata:
            labels:
              app: nginx
          spec:
            containers:
            - name: nginx
              image: anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6
              ports:
              - containerPort: 80
              volumeMounts:
              - name: pvc-disk
                mountPath: /data
            volumes:
              - name: pvc-disk
                persistentVolumeClaim:
                  claimName: disk-pvc
        volumeClaimTemplates:
          - metadata:
              name: pvc-disk
              labels:
                app: nginx
            spec:
              accessModes: [ "ReadWriteOnce" ]
              storageClassName: "alicloud-disk-topology-alltype"
              resources:
                requests:
                  storage: 25Gi    
    2. 创建StatefulSet。

      kubectl create -f StatefulSet.yaml
    3. 查看Pod部署情况。

      kubectl get pod -l app=nginx

      预期返回:

      NAME      READY   STATUS    RESTARTS   AGE
      nginx-0   1/1     Running   0          99s
    4. 查看云盘容量。

      kubectl exec -it nginx-0 -- df -h /data

      预期返回:

      Filesystem      Size  Used Avail Use% Mounted on
      /dev/vdb         25G   24K   25G   1% /data
  2. 向挂载目录写入数据,使云盘容量使用率高于80%,进行第一次扩容。

    1. 向挂载目录写入数据。

      命令示例如下,表示向/data/test1写入22 GB的数据,从而使云盘容量使用率高于80%。

      kubectl exec -it nginx-0 -- dd if=/dev/zero of=/data/test1 bs=1G count=22
    2. 查看扩容事件。

      kubectl get events

      由于云盘容量使用率高于80%时,云盘容量为25 GiB,满足action1的条件,因此会自动执行action1进行扩容(增加50 GiB)。在事件列表中可看到以下相关事件。

      2m1s   Warning   StartExpand                  persistentvolumeclaim/pvc-disk-nginx-0    Start to expand of pvc pvc-disk-nginx-0 from 25Gi to 75Gi, usedCapacityPercentage:90%, freeSize:2498MB.
      2m1s   Normal    ExternalExpanding            persistentvolumeclaim/pvc-disk-nginx-0    waiting for an external controller to expand this PVC
      2m1s   Normal    Resizing                     persistentvolumeclaim/pvc-disk-nginx-0    External resizer is resizing volume d-uf66kkzltnq6xgi9****
      118s   Normal    FileSystemResizeRequired     persistentvolumeclaim/pvc-disk-nginx-0    Require file system resize of volume on node
      116s   Warning   SkipExpand                   persistentvolumeclaim/pvc-disk-nginx-0    Pvc pvc-disk-nginx-0 is expanding status from 25Gi to 75Gi, this action action2 will skip.
    3. 查看PVC的容量。

      kubectl get pvc

      预期输出如下,可以看到执行action1后,云盘已从25 GiB扩容到75 GiB。

      NAME               STATUS   VOLUME                   CAPACITY   ACCESS MODES   STORAGECLASS                     VOLUMEATTRIBUTESCLASS   AGE
      pvc-disk-nginx-0   Bound    d-uf66kkzltnq6xgi9****   75Gi       RWO            alicloud-disk-topology-alltype   <unset>                 26m
  3. 继续向挂载目录写入数据,再次使云盘容量使用率高于80%,进行第二次扩容。

    1. 向挂载目录写入数据。

      命令示例如下,表示向/data/test2写入40 GB的数据,从而使云盘容量使用率高于80%。

      kubectl exec -it nginx-0 -- dd if=/dev/zero of=/data/test2 bs=1G count=40
    2. 查看扩容事件。

      kubectl get events

      由于云盘容量使用率高于80%时,云盘容量为75 GiB,满足action1的条件,因此会自动执行action1进行扩容(增加50 GiB)。由于action1的limit为100 GiB,因此执行action1后只能将云盘从75 GiB扩容到100 GiB。在事件列表中可看到以下相关事件。

      7m4s   Warning   StartExpand                  persistentvolumeclaim/pvc-disk-nginx-0      Start to expand of pvc pvc-disk-nginx-0 from 75Gi to 100Gi, usedCapacityPercentage:84%, freeSize:11927MB.
      7m4s   Normal    ExternalExpanding            persistentvolumeclaim/pvc-disk-nginx-0      waiting for an external controller to expand this PVC
      7m4s   Normal    Resizing                     persistentvolumeclaim/pvc-disk-nginx-0      External resizer is resizing volume d-uf66kkzltnq6xgi9****
      7m1s   Normal    FileSystemResizeRequired     persistentvolumeclaim/pvc-disk-nginx-0      Require file system resize of volume on node
      5m59s  Warning   SkipExpand                   persistentvolumeclaim/pvc-disk-nginx-0      Pvc pvc-disk-nginx-0 is expanding status from 75Gi to 100Gi, this action action2 will skip.
  4. 继续向挂载目录写入数据,再次使云盘容量使用率高于80%,进行第三次扩容。

    1. 向挂载目录写入数据。

      命令示例如下,表示向/data/test3写入20 GB的数据,从而使云盘容量使用率高于80%。

      kubectl exec -it nginx-0 -- dd if=/dev/zero of=/data/test3 bs=1G count=20
    2. 查看扩容事件。

      kubectl get events

      由于云盘容量使用率高于80%时,云盘容量为100 GiB,满足action2的条件,因此会自动执行action2进行扩容,将云盘从100 GiB扩容到150 GiB。在事件列表中可看到以下相关事件。

      2m40s   Warning   StartExpand                  persistentvolumeclaim/pvc-disk-nginx-0    Start to expand of pvc pvc-disk-nginx-0 from 100Gi to 150Gi, usedCapacityPercentage:83%, freeSize:16637MB.
      2m40s   Normal    ExternalExpanding            persistentvolumeclaim/pvc-disk-nginx-0    waiting for an external controller to expand this PVC
      2m40s   Normal    Resizing                     persistentvolumeclaim/pvc-disk-nginx-0    External resizer is resizing volume d-uf66kkzltnq6xgi9****
      2m37s   Normal    FileSystemResizeRequired     persistentvolumeclaim/pvc-disk-nginx-0    Require file system resize of volume on node
      109s    Warning   SkipExpand                   persistentvolumeclaim/pvc-disk-nginx-0    Pvc pvc-disk-nginx-0 is expanding status from 100Gi to 150Gi, this action action2 will skip.
  5. 继续向挂载目录写入数据,再次使云盘容量使用率高于80%,进行第四次扩容。

    1. 向挂载目录写入数据。

      命令示例如下,表示向/data/test4写入50 GB的数据,从而使云盘容量使用率高于80%。

      kubectl exec -it nginx-0 -- dd if=/dev/zero of=/data/test4 bs=1G count=50
    2. 查看扩容事件。

      kubectl get events

      由于云盘容量使用率高于80%时,云盘容量为150 GiB,满足action2的条件,因此会自动执行action2进行扩容(增加当前容量的50%)。将云盘从150 GiB扩容到225 GiB。在事件列表中可看到以下相关事件。

      2m42s   Warning   StartExpand                  persistentvolumeclaim/pvc-disk-nginx-0    Start to expand of pvc pvc-disk-nginx-0 from 150Gi to 225Gi, usedCapacityPercentage:87%, freeSize:19621MB.
      2m42s   Normal    ExternalExpanding            persistentvolumeclaim/pvc-disk-nginx-0    waiting for an external controller to expand this PVC
      2m42s   Normal    Resizing                     persistentvolumeclaim/pvc-disk-nginx-0    External resizer is resizing volume d-uf66kkzltnq6xgi9****
      2m38s   Normal    FileSystemResizeRequired     persistentvolumeclaim/pvc-disk-nginx-0    Require file system resize of volume on node
      114s    Warning   SkipExpand                   persistentvolumeclaim/pvc-disk-nginx-0    Pvc pvc-disk-nginx-0 is expanding status from 150Gi to 225Gi, this action action2 will skip.
  6. 继续向挂载目录写入数据,再次使云盘容量使用率高于80%,进行第五次扩容。

    1. 向挂载目录写入数据。

      命令示例如下,表示向/data/test5写入50 GB的数据,从而使云盘容量使用率高于80%。

      kubectl exec -it nginx-0 -- dd if=/dev/zero of=/data/test5 bs=1G count=50
    2. 查看扩容事件。

      kubectl get events

      由于云盘容量使用率高于80%时,云盘容量为225 GiB,满足action2的条件,因此会自动执行action2进行扩容(增加当前容量的50%)。由于action2的limit为300 GiB,因此执行action2后只能将云盘从225 GiB扩容到300 GiB。在事件列表中可看到以下相关事件。

      17m     Warning   StartExpand                  persistentvolumeclaim/pvc-disk-nginx-0    Start to expand of pvc pvc-disk-nginx-0 from 225Gi to 300Gi, usedCapacityPercentage:82%, freeSize:40351MB.
      17m     Normal    ExternalExpanding            persistentvolumeclaim/pvc-disk-nginx-0    waiting for an external controller to expand this PVC
      17m     Normal    Resizing                     persistentvolumeclaim/pvc-disk-nginx-0    External resizer is resizing volume d-uf66kkzltnq6xgi9****
      17m     Normal    FileSystemResizeRequired     persistentvolumeclaim/pvc-disk-nginx-0    Require file system resize of volume on node

      此时action1和action2均已执行完毕,当云盘容量再次高于80%时,无法满足两者的条件,不再触发扩容。

相关文档

如在使用云盘存储卷的过程中遇到相关问题,请参见云盘存储卷FAQ