无法从 docker 运行 写入 kubernetes pod 中的文件

Unable to write file from docker run inside a kubernetes pod

我有一个 docker 图像,它使用一个卷来写入文件:

docker run --rm -v /home/dir:/out/ image:cli args

当我尝试 运行 在 pod 中执行此操作时,容器正常退出但未写入任何文件。

我不明白。

如果找不到卷,容器会抛出错误,例如,如果我 运行 它没有 -v 选项,它会抛出:

Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path '/out/file.txt'.

但是我没有从容器中收到任何错误。 它像写入文件一样完成,但文件不存在。

我对 Kubernetes 还很陌生,但这让我抓狂。

kubernetes会阻止文件写入吗?还是我错过了一些明显的东西?

整个 Kubernetes 上下文由 GCP composer-airflow 管理,如果有帮助...

docker -v: Docker version 17.03.2-ce, build f5ec1e2

如果你想在 Kubernetes 中有这种行为,你可以使用 hostPath 卷。

本质上,您在 pod 规范中指定它,然后将卷挂载到 pod 运行的节点上,然后文件应该在 pod 退出后位于节点中。

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: image:cli
    name: test-container
    volumeMounts:
    - mountPath: /home/dir
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      path: /out
      type: Directory

when I try to run this inside a pod the container exit normally but no file is written

首先,不需要运行 pod 里面的docker run 命令:)。应该为 pod 编写 spec file (yaml),kubernetes 将为您使用 docker 运行 pod 中的容器。理想情况下,使用 kubernetes 时不需要 运行 docker 命令(除非你正在调试 docker 相关问题)。

This link 为 docker 用户提供了有用的 kubectl 命令。

如果你习惯docker-compose,参考Komposedocker-compose到kubernetes:

在 kubernetes 中将主机上的目录作为容器内的卷挂载的一些选项: