结束解决如下:
首先,我创建了一个 job 定义如下(片段):
job
apiVersion: batch/v1 kind: Job metadata: name: item-001 spec: template: metadata: name: item-xxx spec: containers: - name: worker image: gcr.io/<something>/worker volumeMounts: - mountPath: /var/run/docker.sock name: docker-socket-mount - mountPath: /workspace name: workspace volumes: - name: docker-socket-mount hostPath: path: /var/run/docker.sock - name: workspace hostPath: path: /home/workspace
有2个坐骑,第一个 docker-socket-mount 坐骑 /var/run/docker.sock 进入容器,所以我可以从里面使用Docker,第二个,它安装一个将在主机和来宾容器之间共享的卷 workspace 。
docker-socket-mount
/var/run/docker.sock
workspace
该 worker 运行类似于此的脚本:
worker
#!/usr/bin/env bash IMAGE=gcr.io/some/guest/image # ... gsutil -m cp -r gs://some/files/I/need/* /workspace # ... export DOCKER_API_VERSION=1.23 gcloud docker -- pull ${IMAGE} docker run -v /home/workspace:/workspace ${IMAGE} # ...
让docker socket可用,安装docker客户端并正常调用它就足够了。诀窍是从中挂载客户映像 /home/workspace ,从kubernetes节点看,而不是从主机图像看到( /workspace )。下载的文件 /workspace 现在也可以在来宾容器上使用。
/home/workspace
/workspace
最后, Dockerfile 看起来与此类似:
Dockerfile
FROM ubuntu:14.04 # ... # Install Docker RUN curl -fsSL https://get.docker.com/ | sh # Install Google Cloud SDK ADD xxx.json /home/keys/xxx.json RUN curl https://sdk.cloud.google.com > /tmp/gcloud.sh RUN bash /tmp/gcloud.sh --disable-prompts --install-dir=/home/tools/ RUN /home/tools/google-cloud-sdk/bin/gcloud auth activate-service-account name@my-project.iam.gserviceaccount.com --key-file=/home/keys/xxx.json RUN /home/tools/google-cloud-sdk/bin/gcloud config set project my-project # ...