【问题标题】:Not able to access file shared with init container无法访问与 init 容器共享的文件
【发布时间】:2020-10-19 14:32:04
【问题描述】:

我正在初始化容器中创建一个文件,并希望在主容器中使用该文件。

  containers:
  - name: test1
    imagePullPolicy: Always
    image: newbusybox
    command:
    - "some command  --from-file=tmp/file.txt"
    volumeMounts:
    - name: workdir
      mountPath: /tmp
  initContainers:
  - name: install
    image: busybox
    command: ["/bin/sh", "-c"]
    args: ["echo test  > /pod-data/file.txt]
    volumeMounts:
    - name: workdir
      mountPath: /pod-data/
  volumes:
  - name: workdir
    emptyDir: {}

Pods 进入 crashlookBack off 并出现以下错误

Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"some command  --from-file=tmp/file.txt\": stat some comamnd  --from-file=tmp/file.txt: no such file or directory": unknown

我已验证文件已安装在 tmp 位置。

【问题讨论】:

  • 您可以尝试更改command 字段以使其像这样工作吗? kubernetes.io/docs/tasks/inject-data-application/…报错信息表明错误不在文件共享中,而是在命令规范中
  • 我认为您只是在开头缺少一个正斜杠(--from-file=/tmp/file.txt 而不是--from-file=tmp/file.txt
  • 好的,我也试过了。同样的问题--------------------------------------------- -------------------------------------------------- -------------------------------------- 图片:“ubuntu:14.04” 命令:- touch tmp 。文本 - - - - - - - - - - - - - - - - - - - - - - - - --------------- 错误:无法启动容器“my-container”:来自守护进程的错误响应:OCI 运行时创建失败:container_linux.go:349:启动容器进程导致“exec: \"touch tmp.txt\": $PATH 中找不到可执行文件":
  • 请尝试在 initContainer -> command: ["bin/sh"] args: ["-c", "echo hello > /pod-data/hello.txt"] 中使用。在主容器中:command: [/bin/sh] args: ["-c","cat /tmp/hello.txt; sleep infinity"]

标签: kubernetes google-kubernetes-engine


【解决方案1】:

我不确定您要完成什么,但我需要同意用户 @Lauri Koskela 提供的评论:

您可以尝试更改命令字段以使其像这样工作吗? kubernetes.io/docs/tasks/inject-data-application/… 错误信息表明错误不在文件共享中,而是在命令规范中

@Lauri Koskela 发布的链接:

可以阐明将命令解析为Pods 的方法。

你还有一个错字:

  • args: ["echo test > /pod-data/file.txt]
  • args: ["echo test > /pod-data/file.txt"] #

Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"some command  --from-file=tmp/file.txt\": stat some comamnd  --from-file=tmp/file.txt: no such file or directory": unknown

上面的错误与您尝试在未指定 shell 的情况下运行命令有关(它也与您所做的评论有关)。您可以在此处阅读更多信息:

如果你:

    command: ["/bin/sh", "-c"] 
    args: 
    - "some command  --from-file=/tmp/file.txt"

你应该可以运行some command

按照这条路径,这里有一个额外的例子:

apiVersion: v1
kind: Pod
metadata:
  name: busybox-pod
  labels:
    app: busybox
spec:
  containers:
  - name: busybox
    imagePullPolicy: Always
    image: busybox
    command: ["/bin/sh", "-c"]
    args: 
    - "cat /tmp/file.txt > /working.txt && sleep 3600"
    # - "cat /tmp/file.txt > /working.txt; sleep 3600"
    volumeMounts:
    - name: workdir
      mountPath: /tmp
  initContainers:
  - name: install
    image: busybox
    command: ["/bin/sh", "-c"]
    args: ["echo test > /pod-data/file.txt"]
    volumeMounts:
    - name: workdir
      mountPath: /pod-data/
  volumes:
  - name: workdir
    emptyDir: {}

这个例子将:

  • 使用busybox 图像运行initContainer,并将“测试”文本保存到/pod-data/ 目录中名为file.txt 的文件中
  • 运行 main container 并将 cat 之前创建的文件的内容现在位于 /tmp 中的 working.txt 文件中。它还会休眠 3600 秒。

运行此容器后,您可以通过以下方式检查文件是否被正确读取:

  • $ kubectl exec -t busybox-pod -- cat /working.txt
hello

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 2021-12-27
    相关资源
    最近更新 更多