Kubernetes容器间通信教程定义了以下管道 yaml:
apiVersion: v1
kind: Pod
metadata:
name: two-containers
spec:
restartPolicy: Never
volumes: <--- This is what I need
- name: shared-data
emptyDir: {}
containers:
- name: nginx-container
image: nginx
volumeMounts:
- name: shared-data
mountPath: /usr/share/nginx/html
- name: debian-container
image: debian
volumeMounts:
- name: shared-data
mountPath: /pod-data
command: ["/bin/sh"]
args: ["-c", "echo Hello from the debian container > /pod-data/index.html"]
请注意,volumes
密钥在 下定义spec
,因此该卷可用于所有已定义的容器。我想使用kfp来实现相同的行为,它是 kubeflow 管道的 API。
但是,我只能将卷添加到单个容器,而不是使用kfp.dsl.ContainerOp.container.add_volume_mount
指向先前创建的卷 ( kfp.dsl.PipelineVolume ) 的整个工作流规范,因为卷似乎只在容器中定义。
这是我尝试过的,但卷总是在第一个容器中定义,而不是“全局”级别。如何获取它以便可以op2
访问该卷?我原以为它会在kfp.dsl.PipelineConf内,但无法将卷添加到其中。只是没有实施吗?
import kubernetes as k8s
from kfp import compiler, dsl
from kubernetes.client import V1VolumeMount
import pprint
@dsl.pipeline(name="debug", description="Debug only pipeline")
def pipeline_func():
op = dsl.ContainerOp(
name='echo',
image='library/bash:4.4.23',
command=['sh', '-c'],
arguments=['echo "[1,2,3]"> /tmp/output1.txt'],
file_outputs={'output': '/tmp/output1.txt'})
op2 = dsl.ContainerOp(
name='echo2',
image='library/bash:4.4.23',
command=['sh', '-c'],
arguments=['echo "[4,5,6]">> /tmp/output1.txt'],
file_outputs={'output': '/tmp/output1.txt'})
mount_folder = "/tmp"
volume = dsl.PipelineVolume(volume=k8s.client.V1Volume(
name=f"test-storage",
empty_dir=k8s.client.V1EmptyDirVolumeSource()))
op.add_pvolumes({mount_folder: volume})
op2.container.add_volume_mount(volume_mount=V1VolumeMount(mount_path=mount_folder,
name=volume.name))
op2.after(op)
workflow = compiler.Compiler().create_workflow(pipeline_func=pipeline_func)
pprint.pprint(workflow["spec"])