全部產品
Search
文件中心

Container Service for Kubernetes:使用儲存卷

更新時間:Dec 18, 2025

如果工作流程的多個步驟之間需要共用資料或狀態,您可以在叢集中掛載儲存卷。目前支援OSS儲存卷、NAS儲存卷。本文結合樣本介紹如何建立靜態儲存卷,並指定樣本工作流程使用該儲存卷。

使用說明

OSS儲存卷、NAS儲存卷的側重的使用情境不同。下方建議供您參考。

儲存卷

使用情境

OSS儲存卷

  • 共用資料情境

    OSS為共用儲存,支援多個Pod同時訪問一份資料。OSS中的資料不會隨Pod刪除而清空,可用於Pod之間共用資料。

  • 設定檔、圖片、各類視頻等媒體檔案等多讀少寫情境

    OSS適合儲存非結構化資料,如設定檔、圖片、音視頻等。如業務需要變更檔案內容,請選擇ossfs1.0儲存卷,對POSIX操作的相容性更佳。

  • AI推理、資料分析等大並發批處理情境

    OSS的高服務端頻寬適合大並發批處理,比如AI訓練、資料分析、自動駕駛等新型計算密集型負載及工作流程。這類工作負載主要涉及順序和隨機讀取、順序(僅支援追加)寫入操作,請選擇ossfs2.0儲存卷獲得更高吞吐。

  • 具有較高鑒權、容災需求的資料安全情境

    OSS支援3AZ同城冗餘容災及KMS等服務端加密能力,並支援MD5校正,保證資料上傳下載的完整性,同時支援RAM及OSS Bucket Policy兩種對象層級的鑒權方式。

NAS儲存卷

  • 共用資料情境

    NAS支援多個Pod同時訪問一份資料,如果您有共用資料需求,推薦使用NAS。

  • 巨量資料分析情境

    NAS提供了較高的資料吞吐能力,能滿足大批量作業對共用儲存訪問的需求。

  • Web應用情境

    為Web應用、內容管理系統提供儲存支撐。

  • 儲存日誌情境

    如果您希望將日誌儲存到儲存卷,推薦使用NAS儲存卷。

更多資訊,請參見NAS儲存卷

CPFS儲存卷

  • 基因計算、巨量資料分析情境

    CPFS提供的高吞吐能力,滿足大規模叢集對儲存超高效能的需求。

  • 資料緩衝訪問情境

    您可以將低速儲存資料先下載到CPFS卷,然後供應用高速訪問。

更多資訊,請參見使用CPFS通用版靜態儲存卷管理CPFS通用版動態儲存裝置卷

在不同儲存卷有對應的使用限制和說明,也會產生對應的資源費用,請參見以下文檔瞭解詳情。

使用OSS儲存卷

  1. 使用以下範例程式碼,建立OSS儲存卷。

    更多資訊,請參見使用ossfs 1.0靜態儲存卷

    apiVersion: v1
    kind: Secret
    metadata:
      name: oss-secret
      namespace: argo
    stringData:
      akId: <yourAccessKey ID> # 替換為實際的AccessKeyID。
      akSecret: <yourAccessKey Secret> # 替換為實際的AccessKeySecret。
    ---
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv-oss
      namespace: argo
      labels:
        alicloud-pvname: pv-oss
    spec:
      capacity:
        storage: 5Gi
      accessModes:
        - ReadWriteMany
      persistentVolumeReclaimPolicy: Retain
      csi:
        driver: ossplugin.csi.alibabacloud.com
        volumeHandle: pv-oss   # 與PV名字保持一致。
        nodePublishSecretRef:
          name: oss-secret
          namespace: argo
        volumeAttributes:
          bucket: <your bucket name> # 替換為實際的Bucket名稱。
          url: "oss-<your region id>-internal.aliyuncs.com" # 替換<your region id>為OSS的地區ID,例如華北2(北京)地區為oss-cn-beijing-internal.aliyuncs.com。
          otherOpts: "-o max_stat_cache_size=0 -o allow_other -o multipart_size=30 -o parallel_count=20" # -o max_stat_cache_size=0
          path: "/"  #掛載Bucket根目錄,也可以設定此參數掛載Bucket下子目錄,例如path: "testdir/testdir1"。
    ---
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc-oss
      namespace: argo
    spec:
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 5Gi
      selector:
        matchLabels:
          alicloud-pvname: pv-oss

    選擇性參數

    您可以為OSS儲存卷輸入定製化參數,格式為-o *** -o ***,例如-o umask=022 -o max_stat_cache_size=0 -o allow_other

    參數

    說明

    umask

    用於更改ossfs讀檔案的許可權。例如,設定umask=022後,ossfs檔案的許可權都會變更為755。通過SDK、OSS控制台等其他方式上傳的檔案在ossfs中預設許可權均為640。因此,建議您在讀寫分離情境中配置umask許可權。

    max_stat_cache_size

    用於指定檔案中繼資料的緩衝空間,可緩衝多少個檔案的中繼資料。中繼資料快取可加快ls操作速度。但若通過其他例如OSS、SDK、控制台、ossutil等方式修改檔案,可能會導致中繼資料未被及時更新。

    allow_other

    賦予電腦上其他使用者訪問掛載目錄的許可權,但不包含目錄內的檔案。

    更多選擇性參數,請參見選項列表

  2. 使用以下範例程式碼,建立執行個體工作流程,使用儲存卷。

    apiVersion: argoproj.io/v1alpha1
    kind: Workflow
    metadata:
      generateName: volumes-existing-
      namespace: argo
    spec:
      entrypoint: volumes-existing-example
      volumes:
      # Pass my-existing-volume as an argument to the volumes-existing-example template.
      # Same syntax as k8s Pod spec.
      - name: workdir
        persistentVolumeClaim:
          claimName: pvc-oss
    
      templates:
      - name: volumes-existing-example
        steps:
        - - name: generate
            template: whalesay
        - - name: print
            template: print-message
    
      - name: whalesay
        container:
          image: mirrors-ssl.aliyuncs.com/busybox:latest
          command: [sh, -c]
          args: ["echo generating message in volume; echo hello world | tee /mnt/vol/hello_world.txt"]
          volumeMounts:
          - name: workdir
            mountPath: /mnt/vol
    
      - name: print-message
        container:
          image: mirrors-ssl.aliyuncs.com/alpine:latest
          command: [sh, -c]
          args: ["echo getting message from volume; find /mnt/vol; cat /mnt/vol/hello_world.txt"]
          volumeMounts:
          - name: workdir
            mountPath: /mnt/vol

使用NAS儲存卷

  1. 使用以下範例程式碼,建立NAS共用儲存卷。

    更多資訊,請參見使用NAS靜態儲存卷

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv-nas
      namespace: argo
      labels:
        alicloud-pvname: pv-nas
    spec:
      capacity:
        storage: 100Gi
      accessModes:
        - ReadWriteMany
      csi:
        driver: nasplugin.csi.alibabacloud.com
        volumeHandle: pv-nas   # 必須與PV Name保持一致。
        volumeAttributes:
          server: "<your nas filesystem id>.cn-beijing.nas.aliyuncs.com"
          path: "/"
      mountOptions:
      - nolock,tcp,noresvport
      - vers=3
    ---
    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: pvc-nas
      namespace: argo
    spec:
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 100Gi
      selector:
        matchLabels:
          alicloud-pvname: pv-nas
  2. 使用以下範例程式碼,在工作流程中掛載和使用NAS。

    apiVersion: argoproj.io/v1alpha1
    kind: Workflow
    metadata:
      generateName: volumes-existing-
      namespace: argo
    spec:
      entrypoint: volumes-existing-example
      volumes:
      # Pass my-existing-volume as an argument to the volumes-existing-example template.
      # Same syntax as k8s Pod spec.
      - name: workdir
        persistentVolumeClaim:
          claimName: pvc-nas
    
      templates:
      - name: volumes-existing-example
        steps:
        - - name: generate
            template: whalesay
        - - name: print
            template: print-message
    
      - name: whalesay
        container:
          image: mirrors-ssl.aliyuncs.com/busybox:latest
          command: [sh, -c]
          args: ["echo generating message in volume; echo hello world | tee /mnt/vol/hello_world.txt"]
          volumeMounts:
          - name: workdir
            mountPath: /mnt/vol
    
      - name: print-message
        container:
          image: mirrors-ssl.aliyuncs.com/alpine:latest
          command: [sh, -c]
          args: ["echo getting message from volume; find /mnt/vol; cat /mnt/vol/hello_world.txt"]
          volumeMounts:
          - name: workdir
            mountPath: /mnt/vol

使用CPFS2.0儲存卷

  1. 執行以下命令,建立CPFS2.0共用卷。

    更多資訊,請參見使用CPFS通用版靜態儲存卷

    cat << EOF | kubectl apply -f -
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv-cpfs
      namespace: argo
      labels:
        alicloud-pvname: pv-cpfs
    spec:
      accessModes:
      - ReadWriteOnce
      capacity:
        storage: 1000Gi
      csi:
        driver: nasplugin.csi.alibabacloud.com
        volumeAttributes:
          mountProtocol: cpfs-nfs             # 掛載時,使用NFS協議進行掛載。
          path: "/share"                      # 掛載目錄必須以/share為首碼。
          volumeAs: subpath
          server: "<your cpfs id, e.g cpfs-****>.<regionID>.cpfs.aliyuncs.com"      # 為掛載點前面的網域名稱。
        volumeHandle: pv-cpfs # 必須與PV Name保持一致。
      mountOptions:
      - rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport
      - vers=3
    ---
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc-cpfs
      namespace: argo
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 1000Gi
      selector:
        matchLabels:
          alicloud-pvname: pv-cpfs
    EOF
  2. 使用以下範例程式碼,在工作流程中掛載和使用CPFS2.0。

    apiVersion: argoproj.io/v1alpha1
    kind: Workflow
    metadata:
      generateName: volumes-existing-
      namespace: argo
    spec:
      entrypoint: volumes-existing-example
      volumes:
      # Pass my-existing-volume as an argument to the volumes-existing-example template.
      # Same syntax as k8s Pod spec.
      - name: workdir
        persistentVolumeClaim:
          claimName: pvc-cpfs
    
      templates:
      - name: volumes-existing-example
        steps:
        - - name: generate
            template: whalesay
        - - name: print
            template: print-message
    
      - name: whalesay
        container:
          image: mirrors-ssl.aliyuncs.com/busybox:latest
          command: [sh, -c]
          args: ["echo generating message in volume; echo hello world | tee /mnt/vol/hello_world.txt"]
          volumeMounts:
          - name: workdir
            mountPath: /mnt/vol
    
      - name: print-message
        container:
          image: mirrors-ssl.aliyuncs.com/alpine:latest
          command: [sh, -c]
          args: ["echo getting message from volume; find /mnt/vol; cat /mnt/vol/hello_world.txt"]
          volumeMounts:
          - name: workdir
            mountPath: /mnt/vol