全部產品
Search
文件中心

Container Compute Service:使用儲存卷

更新時間:Mar 13, 2025

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

使用說明

OSS儲存卷、NAS儲存卷側重的使用情境不同。在不同儲存卷有對應的使用限制和說明,也會產生對應的資源費用,請參見儲存概述

使用樣本

使用OSS儲存卷

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

    更多資訊,請參見靜態掛載OSS儲存卷

    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