通過定義CNFS中託管的NAS儲存卷的自動擴容策略,當NAS儲存卷的使用率高於閾值時觸發自動擴容。本文介紹如何使用CNFS實現NAS儲存卷的自動擴容。
前提條件
叢集使用容器網路檔案系統CNFS來管理NAS檔案系統。更多資訊,請參見通過CNFS管理NAS檔案系統(推薦)。
叢集的儲存外掛程式為CSI,且儲存相關組件滿足以下要求:
csi-plugin和csi-provisioner組件版本不低於v1.20.5-ff6490f-aliyun。如需升級,請參見升級csi-plugin和csi-provisioner。
csi-plugin預設的
dnsPolicy是ClusterFirst。若要使用CNFS自動擴容NAS儲存卷,需要手動將ClusterFirst改為ClusterFirstWithHostNet。storage-operator組件版本不低於v1.18.8.56-2aa33ba-aliyun。如需升級,請參見管理storage-operator組件。
步驟一:配置storage-operator組件開啟自動擴容
storage-operator組件中預設的storage-auto-expander負責自動擴容儲存資源。串連叢集,執行以下命令修改storage-operator的ConfigMap檔案,開啟自動擴容功能。
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\"}"}}'步驟二:配置NAS自動擴容策略
查看CNFS對象的狀態,需要確保CNFS對象的狀態為Available。
查看CNFS對象。
kubectl get cnfs預期輸出:
NAME AGE default-cnfs-nas-837d6ea-20210819155623 14d查看CNFS對象的狀態。
kubectl get cnfs <上一步查詢到的CNFS對象> -o yaml | grep Available預期輸出:
status: Available
使用以下YAML模板建立NAS擴容策略。
cat << EOF | kubectl apply -f - apiVersion: storage.alibabacloud.com/v1alpha1 kind: StorageAutoScalerPolicy metadata: name: hybrid-expand-policy spec: pvcSelector: matchLabels: app: nginx # 需要與PVC及Deployment下labels的配置相同,本樣本均為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: 100% limits: 500Gi EOF參數
說明
pvcSelector
選擇目標PVC,通過Label-Selector方式實現擴容策略與PVC之間的匹配。本樣本為nginx。
namespaces
表示目標PVC所在的命名空間,多個命名空間為或邏輯。若不配置,預設為default。本樣本為default及nginx。
conditions
表示觸發規則的條件,多個condition為與邏輯。每個condition包含以下參數:
key:定義一個Metric的類型。
volume-capacity-used-percentage:表示容量使用百分比。
operator:定義規則,包含Gt(大於)、Lt(小於)、Eq(等於)或Ne(不等於),不限制英文字母大小寫。
values:規則的具體數值。
本樣本表示當PVC容量的使用率高於80%時會觸發actions。
actions
表示滿足上述conditions時執行的操作,可以是多個操作。包含以下參數:
type:表示行為方式,目前只支援擴容。
scale:表示擴容的大小,單位為GiB,或可使用百分比。
limits:表示PVC在此action中的最大限制。
若actions中有多個action時,則從首個action開始匹配,執行首個滿足條件的action,其餘跳過。
本樣本的action1表示,當滿足擴容條件時NAS儲存卷容量以當前容量的100%為單位擴容,最大擴容到500 GiB。
使用以下YAML模板建立PVC及Deployment。
重要通過匹配擴容策略與對應PVC及Deployment之間的標籤,將擴容策略應用到PVC及Deployment中。例如,本樣本擴容策略中pvcSelector.matchLabels的值為
app: nginx,對應PVC及Deployment中labels的值為app: nginx。cat << EOF | kubectl apply -f - kind: PersistentVolumeClaim apiVersion: v1 metadata: name: cnfs-nas-pvc labels: app: nginx # 需要與擴容策略YAML模板下pvcSelector.matchLabels的配置相同,本樣本均為app: nginx。 spec: accessModes: - ReadWriteMany storageClassName: alibabacloud-cnfs-nas resources: requests: storage: 50Gi --- apiVersion: apps/v1 kind: Deployment metadata: name: cnfs-nas-deployment labels: app: nginx spec: selector: matchLabels: app: nginx # 需要與擴容策略YAML模板下pvcSelector.matchLabels的配置相同,本樣本均為app: nginx。 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.2 volumeMounts: - mountPath: "/data" name: cnfs-nas-pvc volumes: - name: cnfs-nas-pvc persistentVolumeClaim: claimName: cnfs-nas-pvc EOF
步驟三:驗證NAS儲存卷的自動擴容
查看命名空間default下的所有Pod。
kubectl get pod預期輸出:
NAME READY STATUS RESTARTS AGE cnfs-nas-deployment-56dbcc7fb7-wh79z 1/1 Running 0 20m進入應用Pod,在NAS儲存卷掛載目錄寫入50 GB的測試資料。
cd /data dd if=<資料路徑> of=<掛載路徑> kubectl exec -it cnfs-nas-deployment-56dbcc7fb7-wh79z -ti -- dd if=/dev/zero of=/data/test1 bs=1G count=50查看觸發擴容的事件。
kubectl get events預期輸出如下,可以看到當前NAS儲存卷的容量使用量已超過80%,觸發擴容,NAS儲存卷容量將從50 GiB擴容到100 GiB。
12s Warning StartExpand persistentvolumeclaim/cnfs-nas-pvc Start to expand of pvc cnfs-nas-pvc from 50Gi to 100Gi, usedCapacityPercentage:96%, freeSize:2048MB. 12s Normal ExternalExpanding persistentvolumeclaim/cnfs-nas-pvc waiting for an external controller to expand this PVC 12s Normal Resizing persistentvolumeclaim/cnfs-nas-pvc External resizer is resizing volume nas-462db2b2-717d-44fe-b0b6-fb4db03a**** 12s Normal VolumeResizeSuccessful persistentvolumeclaim/cnfs-nas-pvc Resize volume succeeded在CSI NAS監控大盤查看儲存卷使用資訊。
登入Container Service管理主控台,在左側導覽列選擇叢集列表。
在叢集列表頁面,單擊目的地組群名稱,然後在左側導覽列,選擇。
在Prometheus監控頁面,單擊儲存監控頁簽,然後單擊CSI NAS頁簽。
在CSI NAS監控大盤中,選擇NAS儲存卷所在的Namespace及PVC名稱後,在總容量地區可以查看NAS儲存卷當前的容量。
本樣本Namespace選擇default、PVC選擇cnfs-nas-pvc。

從上圖可見,在10:23:30觸發擴容,且擴容後容量為100 GiB。