使用存储卷

本文介绍如何在工作流集群中挂载使用存储卷。

使用说明

在工作流集群中支持使用OSS存储卷、NAS存储卷和CPFS2.0存储卷

使用OSS存储卷

  1. 使用以下示例代码,创建OSS存储卷。

    更多信息,请参见使用OSS静态存储卷。如果使用其他类型存储卷,请替换对应的参数。

    展开查看使用OSS存储卷的YAML示例代码

    apiVersion: v1
    kind: Secret
    metadata:
      name: oss-secret
      namespace: default
    stringData:
      akId: <yourAccessKey ID> # akId需要替换为您的AccessKey ID。
      akSecret: <yourAccessKey Secret> # akSecret需要替换为您的AccessKey Secret。
    ---
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv-oss
      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: default
        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: default
    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. 使用以下示例代码,创建工作流使用存储卷。

    展开查看在工作流中挂载使用OSS存储卷的YAML示例代码

    apiVersion: argoproj.io/v1alpha1
    kind: Workflow
    metadata:
      generateName: volumes-existing-
      namespace: default
    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: docker/whalesay:latest
          command: [sh, -c]
          args: ["echo generating message in volume; cowsay hello world | tee /mnt/vol/hello_world.txt"]
          volumeMounts:
          - name: workdir
            mountPath: /mnt/vol
    
      - name: print-message
        container:
          image: 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共享卷的YAML示例代码

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv-nas
      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
    spec:
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 100Gi
      selector:
        matchLabels:
          alicloud-pvname: pv-nas
  2. 使用以下示例代码,在工作流中挂载和使用NAS。

    展开查看在工作流中挂载使用NAS共享卷的YAML示例代码

    apiVersion: argoproj.io/v1alpha1
    kind: Workflow
    metadata:
      generateName: volumes-existing-
      namespace: default
    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: docker/whalesay:latest
          command: [sh, -c]
          args: ["echo generating message in volume; cowsay hello world | tee /mnt/vol/hello_world.txt"]
          volumeMounts:
          - name: workdir
            mountPath: /mnt/vol
    
      - name: print-message
        container:
          image: 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共享卷。

    展开查看使用CPFS2.0共享卷的YAML示例代码

    cat << EOF | kubectl apply -f -
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv-cpfs
      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
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 1000Gi
      selector:
        matchLabels:
          alicloud-pvname: cpfs-pv
    EOF
  2. 使用以下示例代码,在工作流中挂载和使用CPFS2.0。

    展开查看在工作流中挂载使用CPFS2.0共享卷的YAML示例代码

    apiVersion: argoproj.io/v1alpha1
    kind: Workflow
    metadata:
      generateName: volumes-existing-
      namespace: default
    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: docker/whalesay:latest
          command: [sh, -c]
          args: ["echo generating message in volume; cowsay hello world | tee /mnt/vol/hello_world.txt"]
          volumeMounts:
          - name: workdir
            mountPath: /mnt/vol
    
      - name: print-message
        container:
          image: 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