ArgoWorkflow教程(七)---高效的步骤间文件共享策略

news/2024/10/22 13:41:58

argoworkflow-7-file-share-between-steps.png

之前我们分析了使用 artifact 实现步骤间文件共享,今天分享一下如何使用 PVC 实现高效的步骤间文件共享。

1. 概述

之前在 artifact 篇我们演示了如何使用 artifact 实现步骤间文件传递,今天介绍一种更为简单的文件传递方式:PVC 共享

artifact 毕竟是借助 S3 实现中转,效率上肯定是低于直接共享 PVC 的,而且 artifact 一般用于结果输出,将最终结果保存到 S3,而不是单纯的用来共享文件。

2. 使用 artifact 共享文件

之前已经分享过了怎么通过 artifact 在不同步骤之间传递文件,这里在回顾一下。

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:generateName: artifact-passing-
spec:entrypoint: artifact-exampletemplates:- name: artifact-examplesteps:- - name: generate-artifacttemplate: whalesay- - name: consume-artifacttemplate: print-messagearguments:artifacts:# bind message to the hello-art artifact# generated by the generate-artifact step- name: messagefrom: "{{steps.generate-artifact.outputs.artifacts.hello-art}}"- name: whalesaycontainer:image: docker/whalesay:latestcommand: [sh, -c]args: ["cowsay hello world | tee /tmp/hello_world.txt"]outputs:artifacts:# generate hello-art artifact from /tmp/hello_world.txt# artifacts can be directories as well as files- name: hello-artpath: /tmp/hello_world.txt- name: print-messageinputs:artifacts:# unpack the message input artifact# and put it at /tmp/message- name: messagepath: /tmp/messagecontainer:image: alpine:latestcommand: [sh, -c]args: ["cat /tmp/message"]

可以看到,artifact 方式共享文件步骤间传递参数是比较类似。

导出 artifact

outputs:artifacts:# generate hello-art artifact from /tmp/hello_world.txt# artifacts can be directories as well as files- name: hello-artpath: /tmp/hello_world.txt

后续步骤引用导出的 artifact

arguments:artifacts:# bind message to the hello-art artifact# generated by the generate-artifact step- name: messagefrom: "{{steps.generate-artifact.outputs.artifacts.hello-art}}"

以及步骤中怎么将 artifact 引入,比如下面 demo 则是将 artifact 做为 /tmp/message 挂载到 Pod 中。

inputs:artifacts:# unpack the message input artifact# and put it at /tmp/message- name: messagepath: /tmp/message

3. 使用 PVC 高效共享文件

顾名思义,就是不同步骤都挂载同一个 PVC,这样自然就实现了文件共享。

ArgoWorkflow 中的每一步都会单独启动一个 Pod 来运行

也有两种方式:

  • 1)动态创建 PVC:Workflow 运行时创建 PVC,运行结束后删除 PVC
  • 2)使用已有 PVC:不会创建也不会删除

动态创建 PVC

完整 Demo 如下:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:generateName: volumes-pvc-
spec:entrypoint: volumes-pvc-examplevolumeClaimTemplates:                 # define volume, same syntax as k8s Pod spec- metadata:name: workdir                     # name of volume claimspec:accessModes: [ "ReadWriteOnce" ]resources:requests:storage: 1Gi                  # Gi => 1024 * 1024 * 1024templates:- name: volumes-pvc-examplesteps:- - name: generatetemplate: whalesay- - name: printtemplate: print-message- name: whalesaycontainer:image: docker/whalesay:latestcommand: [sh, -c]args: ["echo generating message in volume; cowsay hello world | tee /mnt/vol/hello_world.txt"]# Mount workdir volume at /mnt/vol before invoking docker/whalesayvolumeMounts:                     # same syntax as k8s Pod spec- name: workdirmountPath: /mnt/vol- name: print-messagecontainer:image: alpine:latestcommand: [sh, -c]args: ["echo getting message from volume; find /mnt/vol; cat /mnt/vol/hello_world.txt"]# Mount workdir volume at /mnt/vol before invoking docker/whalesayvolumeMounts:                     # same syntax as k8s Pod spec- name: workdirmountPath: /mnt/vol

首先定义了一个 PVC 模版,Workflow 运行时就会使用该模版创建一个 PVC

spec:entrypoint: volumes-pvc-examplevolumeClaimTemplates:                 # define volume, same syntax as k8s Pod spec- metadata:name: workdir                     # name of volume claimspec:accessModes: [ "ReadWriteOnce" ]resources:requests:storage: 1Gi                  # Gi => 1024 * 1024 * 1024

然后其他步骤需要将该 PVC 挂载到对应目录

  - name: whalesaycontainer:image: docker/whalesay:latestcommand: [sh, -c]args: ["echo generating message in volume; cowsay hello world | tee /mnt/vol/hello_world.txt"]# Mount workdir volume at /mnt/vol before invoking docker/whalesayvolumeMounts:                     # same syntax as k8s Pod spec- name: workdirmountPath: /mnt/vol- name: print-messagecontainer:image: alpine:latestcommand: [sh, -c]args: ["echo getting message from volume; find /mnt/vol; cat /mnt/vol/hello_world.txt"]# Mount workdir volume at /mnt/vol before invoking docker/whalesayvolumeMounts:                     # same syntax as k8s Pod spec- name: workdirmountPath: /mnt/vol

这样就实现了文件共享,非常简单。

等 Workflow 运行结束后,Argo 会自动将创建出的 PVC 删除。

使用已有 PVC

在某些情况下,我们可以希望访问已经存在的卷,而不是动态创建/销毁卷。

完整 Demo 如下:

# Define Kubernetes PVC
kind: PersistentVolumeClaim
apiVersion: v1
metadata:name: my-existing-volume
spec:accessModes: [ "ReadWriteOnce" ]resources:requests:storage: 1Gi---
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:generateName: volumes-existing-
spec:entrypoint: volumes-existing-examplevolumes:# Pass my-existing-volume as an argument to the volumes-existing-example template# Same syntax as k8s Pod spec- name: workdirpersistentVolumeClaim:claimName: my-existing-volumetemplates:- name: volumes-existing-examplesteps:- - name: generatetemplate: whalesay- - name: printtemplate: print-message- name: whalesaycontainer:image: docker/whalesay:latestcommand: [sh, -c]args: ["echo generating message in volume; cowsay hello world | tee /mnt/vol/hello_world.txt"]volumeMounts:- name: workdirmountPath: /mnt/vol- name: print-messagecontainer:image: alpine:latestcommand: [sh, -c]args: ["echo getting message from volume; find /mnt/vol; cat /mnt/vol/hello_world.txt"]volumeMounts:- name: workdirmountPath: /mnt/vol

首先就是手动创建一个 PVC

# Define Kubernetes PVC
kind: PersistentVolumeClaim
apiVersion: v1
metadata:name: my-existing-volume
spec:accessModes: [ "ReadWriteOnce" ]resources:requests:storage: 1Gi

然后在 Workflow 中定义要使用这个 PVC

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:generateName: volumes-existing-
spec:entrypoint: volumes-existing-examplevolumes:# Pass my-existing-volume as an argument to the volumes-existing-example template# Same syntax as k8s Pod spec- name: workdirpersistentVolumeClaim:claimName: my-existing-volume

可以看做是使用 persistentVolumeClaim 来替换了之前的 volumeClaimTemplates

然后就是步骤将这个 PVC 挂载到对应目录

  - name: whalesaycontainer:image: docker/whalesay:latestcommand: [sh, -c]args: ["echo generating message in volume; cowsay hello world | tee /mnt/vol/hello_world.txt"]volumeMounts:- name: workdirmountPath: /mnt/vol- name: print-messagecontainer:image: alpine:latestcommand: [sh, -c]args: ["echo getting message from volume; find /mnt/vol; cat /mnt/vol/hello_world.txt"]volumeMounts:- name: workdirmountPath: /mnt/vol

这一步和使用动态创建 PVC 时没有任何变化。


【ArgoWorkflow 系列】持续更新中,搜索公众号【探索云原生】订阅,阅读更多文章。


4. 小结

本文主要分析了 Argo 中的 Workflow 中怎么使用 PVC 共享文件。

  • 1)定义 PVC 模版或者指定使用已有的 PVC
  • 2)步骤中将 PVC 挂载到对应目录使用

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/74615.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

Linux下安装Nginx,CentOS7安装Nginx

首先,需要安装一些编译 Nginx 所需的依赖包,使用以下命令: yum -y install gcc pcre - devel zlib - devel openssl - devel 下载 https://nginx.org/en/download.html上传到opt目录下 或者直接在linux系统里下载 wget http://nginx.org/download/nginx-1.26.2.tar.gz 如果c…

习题2.7

习题2.7代码 import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as plt plt.rcParams[font.sans-serif]=[Times New Roman + SimSun + WFM Sans SC] plt.rcParams[mathtext.fontset]=cm Times New Roma…

玩转博客园

收集一些优化博客园的帖子博客园美化主题推荐之Bili https://www.cnblogs.com/AhuntSun-blog/p/12342443.html 博客园 复制他人的博客模板(皮肤) https://www.cnblogs.com/abadcat97/p/14146475.html

C语言中的初始化是什么意思

在C语言中,初始化是指在定义变量时为其赋予初值的过程。通过初始化,可以确保变量在使用之前具有已知的初始值,避免了未初始化变量的不确定行为。初始化可以在变量定义时直接赋值,也可以通过赋予默认值或调用特定的初始化函数来完成。C语言中的初始化 在C语言中,初始化是指…

从事项目管理的朋友们,是如何有效管理项目进度

从事项目管理的專业人士,优化和管理项目进度的有效方法和技巧主要包括以下几点:•创建详细的项目时间表、•分配和优化资源、•设定和跟踪进度基准、•实施有效的团队沟通及•积极应对和管理风险。 首先让我们详细讨论创建详细的项目时间表。针对任何新的项目或任务,首要步骤…

习题2.13

习题2.13代码 import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as plt plt.rcParams[font.sans-serif]=[Times New Roman + SimSun + WFM Sans SC] plt.rcParams[mathtext.fontset]=cm Times New Rom…

习题2.12

习题2.12代码 import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as plt plt.rcParams[font.sans-serif]=[Times New Roman + SimSun + WFM Sans SC] plt.rcParams[mathtext.fontset]=cm Times New Rom…

React和Vue哪个更适合前端开发

在前端开发领域,React和Vue一直是两大热门框架。本文深入对比两者在不同维度的表现,包括:1. 设计理念和学习曲线;2. 数据绑定;3. 组件化;4. 生态系统和工具;5. 性能;6. 社区支持;7. 企业采用和工作机会。通过全面的比较分析,我们可以发现React和Vue各有优势,选择哪一…