全部產品
Search
文件中心

Container Service for Kubernetes:配置雲端硬碟自動擴容策略實現自動擴容

更新時間:Feb 25, 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