全部产品
Search
文档中心

容器服务 Kubernetes 版 ACK:自动扩容云盘数据卷(公测)

更新时间:Sep 06, 2023

随着业务发展和应用数据增长,当云盘使用空间不足时,您可以通过定义一种或多种扩容策略,在数据卷的使用率高于某个阈值时自动扩容卷。本文介绍如何实现云盘数据卷的自动扩容。

前提条件

注意事项

  • 只支持云盘扩容,不支持缩容。

  • 只有处于挂载状态的云盘才支持扩容。

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

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

  • 云盘容量最大为2000 GiB。

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

  1. 推荐您使用ACK集群中默认创建的alicloud-disk-topology-alltype StorageClass,该StorageClass能自适应为您选择云盘类型,避免因实例规格限制、或所在可用区云盘余额不足导致云盘无法创建,增强可用性。若您的集群版本较低,可以通过以下方式手动创建。

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

      allowVolumeExpansion: true
      apiVersion: storage.k8s.io/v1
      kind: StorageClass
      metadata:
        name: alicloud-disk-topology-alltype
      parameters:
        type: cloud_essd,cloud_ssd,cloud_efficiency
      provisioner: diskplugin.csi.alibabacloud.com
      reclaimPolicy: Delete
      volumeBindingMode: WaitForFirstConsumer
    2. 执行以下命令,创建StorageClass。

      kubectl create -f storageclass.yaml
  2. 通过CRD(Custom Resource Definitions)创建自动扩容策略。

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

      apiVersion: storage.alibabacloud.com/v1alpha1
      kind: StorageAutoScalerPolicy
      metadata:
        name: hybrid-expand-policy
      spec:
        pvcSelector:
          matchLabels:
            app: mysql
        namespaces:
          - default
          - mysql
        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

      参数

      说明

      pvcSelector

      目标PVC,通过Label-Selector方式。本示例为mysql

      namespaces

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

      conditions

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

      • key:定义一个Metric的类型。

      • volume-capacity-used-percentage:表示容量使用百分比。

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

      • values:规则的具体数值。

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

      actions

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

      • type:表示行为方式,目前只支持扩容。

      • scale:表示扩容的大小,单位为GiB,或可使用百分比。

      • limits:表示PVC在此action中的最大限制。

      actions中存在多个action时,则从首个action开始匹配,执行首个满足条件的action,其余action跳过不执行。例如,本示例中的action1如果满足条件,则执行action1,不会执行action2

      • 本示例的action1表示云盘容量在100 GiB内以50 GiB为单位扩容,最大扩容到100 GiB。

      • 本示例的action2表示当云盘容量大于100 GiB小于300 GiB时,以当前容量的50%扩容,即每次扩容后的总容量为扩容前容量的150%,最大扩容到300 GiB。

    2. 执行以下命令,创建自动扩容策略。

      kubectl create -f StorageAutoScalerPolicy.yaml
  3. 创建工作负载StatefulSet。

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

       apiVersion: apps/v1
      kind: StatefulSet
      metadata:
        name: mysql
      spec:
        selector:
          matchLabels:
            app: mysql
        serviceName: mysql
        replicas: 3
        template:
          metadata:
            labels:
              app: mysql
          spec:
            containers:
            - name: mysql
              image: mysql:5.7
              env:
              - name: MYSQL_ROOT_PASSWORD
                valueFrom:
                  secretKeyRef:
                    name: mysql-pass
                    key: password
              ports:
              - containerPort: 80
                name: mysql
              volumeMounts:
              - name: pvc-disk
                mountPath: /data
        volumeClaimTemplates:
          - metadata:
              name: pvc-disk
              labels:
                app: mysql
            spec:
              accessModes: [ "ReadWriteOnce" ]
              storageClassName: "alicloud-disk-topology-alltype"
              resources:
                requests:
                  storage: 25Gi
      ---
      apiVersion: v1
      kind: Secret
      metadata:
        name: mysql-pass
      type: Opaque
      data:
        username: dGVz****             
        password: dGVzdDEt****     
    2. 执行以下命令,创建工作负载。

      kubectl create -f StatefulSet.yaml

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

  1. 向挂载目录写入数据,使云盘容量使用率高于80%。

    1. 执行以下命令,向挂载目录写入数据。

      dd if=<数据路径> of=<挂载路径>
    2. 执行以下命令,查看云盘容量详情。

      df -h | grep d-****1

      预期输出:

      Filesystem    Size   Used   Avail   Use%    Mounted on
      /dev/vde      25G    24G    1.5G    95%     /var/lib/kubelet/plugins/kubernetes.io/csi/pv/d-****1/globalmount
  2. 执行以下命令,查看扩容事件。

    当云盘容量使用率高于80%时会触发扩容,此时action1满足条件,则使用action1作为扩容策略。

    kubectl get events

    预期输出:

    101s     Warning     StartExpand                persistentvolumeclaim/pvc-disk-mysql-1     Start to expand of pvc pvc-disk-mysql-1 from 25Gi to 75Gi, usedCapacityPercentage:94%, freeSize:1472MB.
    101s     Warning     ExternalExpanding          persistentvolumeclaim/pvc-disk-mysql-1     Ignoring the PVC: did't find a plugin capable of expanding the volume; waiting for an external controller to process this PVC.
    101s     Normal      Resizing                   persistentvolumeclaim/pvc-disk-mysql-1     External resizer is resizing volume d-****1
    97s      Normal      FileSystemResizeRequired   persistentvolumeclaim/pvc-disk-mysql-1     Require file system resize of volume on node
    96s      Warning     SkipExpand                 persistentvolumeclaim/pvc-disk-mysql-1     Pvc pvc-disk-mysql-1 is expanding status from 25Gi to 75Gi, this action action2 will skip.
  3. 执行以下命令,查看PVC的容量。

    kubectl get pvc

    预期输出:

    NAME               STATUS     VOLUME     CAPACITY     ACCESS MODES     STORAGECLASS                      AGE
    pvc-disk-mysql-0   Bound      d-****0    25Gi         RWO              alicloud-disk-topology-alltype    22m
    pvc-disk-mysql-1   Bound      d-****1    75Gi         RWO              alicloud-disk-topology-alltype    21m
    pvc-disk-mysql-2   Bound      d-****2    25Gi         RWO              alicloud-disk-topology-alltype    21m

    从预期输出可得,云盘d-****1已扩容到75 GiB。

  4. 根据步骤1,再次使云盘容量使用率高于80%,进行第二次扩容。

    执行以下命令,查看扩容事件。

    kubectl get events

    预期输出:

    7m22s    Warning     StartExpand                persistentvolumeclaim/pvc-disk-mysql-1     Start to expand of pvc pvc-disk-mysql-1 from 100Gi to 150Gi, usedCapacityPercentage:95%, freeSize:3732MB.
    5m2s     Warning     ExternalExpanding          persistentvolumeclaim/pvc-disk-mysql-1     Ignoring the PVC: did't find a plugin capable of expanding the volume; waiting for an external controller to process this PVC.
    2m4s     Normal      Resizing                   persistentvolumeclaim/pvc-disk-mysql-1     External resizer is resizing volume d-****1
    3m4s     Normal      FileSystemResizeRequired   persistentvolumeclaim/pvc-disk-mysql-1     Require file system resize of volume on node
    5m59s    Warning     SkipExpand                 persistentvolumeclaim/pvc-disk-mysql-1     Pvc pvc-disk-mysql-1 is expanding status from 100Gi to 150Gi, this action action1 will skip.

    由于action1limit为100 GiB,从预期输出可得,此次只能将云盘从75 GiB扩容到100 GiB。

  5. 根据步骤1,再次使云盘容量使用率高于80%,进行第三次扩容。

    执行以下命令,查看扩容事件。

    kubectl get events

    预期输出:

    7m22s    Warning     StartExpand                persistentvolumeclaim/pvc-disk-mysql-1     Start to expand of pvc pvc-disk-mysql-1 from 100Gi to 150Gi, usedCapacityPercentage:95%, freeSize:3732MB.
    5m2s     Warning     ExternalExpanding          persistentvolumeclaim/pvc-disk-mysql-1     Ignoring the PVC: did't find a plugin capable of expanding the volume; waiting for an external controller to process this PVC.
    2m4s     Normal      Resizing                   persistentvolumeclaim/pvc-disk-mysql-1     External resizer is resizing volume d-****1
    3m4s     Normal      FileSystemResizeRequired   persistentvolumeclaim/pvc-disk-mysql-1     Require file system resize of volume on node
    5m59s    Warning     SkipExpand                 persistentvolumeclaim/pvc-disk-mysql-1     Pvc pvc-disk-mysql-1 is expanding status from 100Gi to 150Gi, this action action1 will skip.

    由于action1limit为100 GiB,不满足扩容条件。从预期输出可得,使用action2策略将云盘从100 GiB扩容到150 GiB。

  6. 根据步骤1,再次使云盘容量使用率高于80%,进行第四次扩容。

    执行以下命令,查看扩容事件。

    kubectl get events

    预期输出:

    0s     Warning     StartExpand                persistentvolumeclaim/pvc-disk-mysql-1     Start to expand of pvc pvc-disk-mysql-1 from 150Gi to 225Gi, usedCapacityPercentage:94%, freeSize:7637MB.
    0s     Warning     ExternalExpanding          persistentvolumeclaim/pvc-disk-mysql-1     Ignoring the PVC: did't find a plugin capable of expanding the volume; waiting for an external controller to process this PVC.
    0s     Normal      Resizing                   persistentvolumeclaim/pvc-disk-mysql-1     External resizer is resizing volume d-****1
    0s     Normal      FileSystemResizeRequired   persistentvolumeclaim/pvc-disk-mysql-1     Require file system resize of volume on node
    0s     Warning     SkipExpand                 persistentvolumeclaim/pvc-disk-mysql-1     Pvc pvc-disk-mysql-1 is expanding status from 150Gi to 225Gi, this action action1 will skip.

    从预期输出可得,使用action2策略将云盘从150 GiB扩容到225 GiB。

  7. 根据步骤1,再次使云盘容量使用率高于80%,进行第五次扩容。

    执行以下命令,查看扩容事件。

    kubectl get events

    预期输出:

    0s     Warning     StartExpand                 persistentvolumeclaim/pvc-disk-mysql-1     Start to expand of pvc pvc-disk-mysql-1 from 225Gi to 300Gi, usedCapacityPercentage:94%, freeSize:7637MB.
    0s     Warning     ExternalExpanding           persistentvolumeclaim/pvc-disk-mysql-1     Ignoring the PVC: did't find a plugin capable of expanding the volume; waiting for an external controller to process this PVC.
    0s     Normal      Resizing                    persistentvolumeclaim/pvc-disk-mysql-1     External resizer is resizing volume d-****1
    0s     Normal      FileSystemResizeRequired    persistentvolumeclaim/pvc-disk-mysql-1     Require file system resize of volume on node
    0s     Warning     FileSystemResizeSuccessful  persistentvolumeclaim/pvc-disk-mysql-1     MountVolume.NodeExpandVolume succeeded for volume "d-****1"

    由于action2limit为300 GiB,从预期输出可得,此次只能将云盘从225 GiB扩容到300 GiB。

    当云盘容量再次高于80%时,不再触发扩容。