如何在Kubeflow Katib的YAML模板中从GCP存储桶获取文件?
在Katib中从GCP存储桶获取文件的实现方法
Katib的试验模板基于Kubernetes Pod定义,无法直接使用Argo Workflows的artifacts语法,你可以通过以下两种常用方式实现从GCP存储桶拉取文件:
方法1:使用GCS CSI Volume挂载(推荐)
如果你的Kubernetes集群(比如GKE)已经安装了GCS CSI驱动,这是最原生、持久化的挂载方式,无需额外命令即可让容器直接访问GCS文件。
在Katib Experiment的YAML中,按如下方式配置:
apiVersion: kubeflow.org/v1beta1 kind: Experiment metadata: name: gcs-file-example spec: objective: type: maximize goal: 0.99 objectiveMetricName: accuracy algorithm: algorithmName: random parallelTrialCount: 1 maxTrialCount: 3 maxFailedTrialCount: 1 trialTemplate: primaryContainerName: training-container trialSpec: apiVersion: batch/v1 kind: Job spec: template: spec: volumes: - name: gcs-artifact csi: driver: gcs.csi.storage.gke.io volumeAttributes: bucket: "INPUT_BUCKET_NAME" mode: "RO" # 只读模式 path: "FILE_NAME" # GCS存储桶中的文件路径 containers: - name: training-container image: your-training-image:latest command: ["python", "/opt/scripts/your-optimization-script.py"] volumeMounts: - name: gcs-artifact mountPath: /my-artifact # 文件将挂载到这个路径下 readOnly: true restartPolicy: Never
关键说明:
volumes部分通过GCS CSI驱动关联指定的存储桶和文件volumeMounts将GCS文件挂载到容器内的指定路径,和你之前Argo配置的path保持一致即可让脚本无缝访问- 确保集群已安装GCS CSI驱动(GKE默认已安装,自建集群需手动部署)
方法2:在容器中用gsutil拉取文件
如果无法使用CSI驱动,可以在主容器启动时通过gsutil命令直接拉取GCS文件,或者用init容器预先下载。
方式A:主容器启动命令中拉取
apiVersion: kubeflow.org/v1beta1 kind: Experiment metadata: name: gcs-gsutil-example spec: # 省略objective、algorithm等通用配置 trialTemplate: primaryContainerName: training-container trialSpec: apiVersion: batch/v1 kind: Job spec: template: spec: containers: - name: training-container image: your-training-image:latest # 先拉取文件,再执行优化脚本 command: ["/bin/sh", "-c"] args: - gsutil cp gs://INPUT_BUCKET_NAME/FILE_NAME /my-artifact && python /opt/scripts/your-optimization-script.py # 确保镜像中已安装gsutil,或基于google/cloud-sdk镜像构建 restartPolicy: Never
方式B:用init容器预先下载
apiVersion: kubeflow.org/v1beta1 kind: Experiment metadata: name: gcs-init-container-example spec: # 省略通用配置 trialTemplate: primaryContainerName: training-container trialSpec: apiVersion: batch/v1 kind: Job spec: template: spec: volumes: - name: shared-volume emptyDir: {} initContainers: - name: download-artifact image: google/cloud-sdk:latest command: ["gsutil", "cp", "gs://INPUT_BUCKET_NAME/FILE_NAME", "/shared/my-artifact"] volumeMounts: - name: shared-volume mountPath: /shared containers: - name: training-container image: your-training-image:latest command: ["python", "/opt/scripts/your-optimization-script.py"] volumeMounts: - name: shared-volume mountPath: /my-artifact restartPolicy: Never
关键说明:
- 主容器方式需要镜像包含
gsutil(可基于google/cloud-sdk镜像扩展) - init容器方式通过临时共享卷传递文件,主容器无需安装
gsutil,更轻量化
内容的提问来源于stack exchange,提问作者Pedro Zaterka
相关产品推荐
相关产品推荐

