如何构建Argo Workflow以运行本地镜像中的Python脚本?
解决Argo Workflow使用本地镜像并运行Python脚本的问题
1. 让Argo能够使用本地myImage镜像
本地镜像无法被Argo调用的核心原因是Kubernetes集群节点无法访问你的本地Docker镜像,针对不同集群场景,解决方案如下:
本地单节点K8s集群(Minikube/Kind)
直接把本地镜像加载到集群环境中:
# Minikube环境执行 minikube load image myImage # Kind环境执行 kind load docker-image myImage
之后在Workflow的容器配置中指定image: myImage,并添加imagePullPolicy: Never(避免K8s尝试拉取远程镜像)。
多节点K8s集群
方法一:使用本地私有镜像仓库
- 启动本地Registry服务:
docker run -d -p 5000:5000 --restart=always --name registry registry:2
- 给本地镜像打私有仓库标签:
docker tag myImage localhost:5000/myImage
- 推送镜像到本地仓库:
docker push localhost:5000/myImage
- 在Workflow中使用
image: localhost:5000/myImage,确保所有K8s节点能访问该Registry地址。
方法二:手动同步镜像到所有节点
- 导出本地镜像为tar包:
docker save -o myImage.tar myImage
- 将tar包复制到每个K8s节点:
scp myImage.tar 用户名@节点IP:/tmp/
- 在每个节点导入镜像:
docker load -i /tmp/myImage.tar
完成后在Workflow中使用image: myImage,并添加imagePullPolicy: Never。
2. 编写Argo Workflow实现脚本调用与输出保存
2.1 调用不带参数的Python脚本
创建workflow-no-args.yaml:
apiVersion: argoproj.io/v1alpha1 kind: Workflow metadata: generateName: run-python-script- spec: entrypoint: run-script templates: - name: run-script container: image: myImage imagePullPolicy: Never command: ["python3"] args: ["/script/python01.py"]
运行Workflow:
argo submit workflow-no-args.yaml --watch
2.2 调用带参数的Python脚本并保存输出
假设python02.py接收--input和--output参数,创建workflow-with-args.yaml:
apiVersion: argoproj.io/v1alpha1 kind: Workflow metadata: generateName: run-python-with-args- spec: entrypoint: run-script-with-args arguments: parameters: - name: input-value value: "sample-input" - name: output-file value: "/output/result.txt" templates: - name: run-script-with-args inputs: parameters: - name: input-value - name: output-file container: image: myImage imagePullPolicy: Never command: ["python3"] args: [ "/script/python02.py", "--input", "{{inputs.parameters.input-value}}", "--output", "{{inputs.parameters.output-file}}" ] volumeMounts: - name: output-volume mountPath: /output volumes: - name: output-volume hostPath: path: /home/你的用户名/argo-outputs # 替换为Ubuntu本地要保存输出的目录 type: DirectoryOrCreate
运行Workflow:
argo submit workflow-with-args.yaml --watch
执行完成后,直接查看Ubuntu本地指定目录(如/home/你的用户名/argo-outputs)即可获取输出文件。
内容的提问来源于stack exchange,提问作者GKV
相关产品推荐
相关产品推荐

