使用kubectl run创建带有卷的kubernetes pod

51

我了解您可以使用kubectl run创建一个带有Deployment / Job的Pod。但是是否可能创建一个附加了卷的Pod? 我尝试运行此命令:

kubectl run -i --rm --tty ubuntu --overrides='{ "apiVersion":"batch/v1", "spec": {"containers": {"image": "ubuntu:14.04", "volumeMounts": {"mountPath": "/home/store", "name":"store"}}, "volumes":{"name":"store", "emptyDir":{}}}}' --image=ubuntu:14.04 --restart=Never -- bash

但是这个卷在交互式bash中看不到。

是否有更好的方法创建一个可以附加到Pod的卷?


我也尝试过使用kubectl create,然后执行/附加,但对我没有起作用。但这可能是因为我遇到了这个错误:https://github.com/kubernetes/kubernetes/issues/16670 - Kenny Ho
1个回答

59

您的JSON覆盖设置不正确。不幸的是,kubectl run命令会忽略它不理解的字段。

kubectl run -i --rm --tty ubuntu --overrides='
{
  "apiVersion": "batch/v1",
  "spec": {
    "template": {
      "spec": {
        "containers": [
          {
            "name": "ubuntu",
            "image": "ubuntu:14.04",
            "args": [
              "bash"
            ],
            "stdin": true,
            "stdinOnce": true,
            "tty": true,
            "volumeMounts": [{
              "mountPath": "/home/store",
              "name": "store"
            }]
          }
        ],
        "volumes": [{
          "name":"store",
          "emptyDir":{}
        }]
      }
    }
  }
}
'  --image=ubuntu:14.04 --restart=Never -- bash
为了调试这个问题,我运行了你指定的命令,然后在另一个终端中运行了:

To debug this issue I ran the command you specified, and then in another terminal ran:


kubectl get job ubuntu -o json

从那里你可以看到,实际的工作结构与您的json覆盖不同(您缺少嵌套的template/spec,volumes、volumeMounts和容器需要是数组)。


谢谢,特别是关于调试的提示。看起来创建然后附加可能是更好的方法...只要我能让它工作。 (由于某种原因,我很难让甚至是skydns验证步骤都能正常工作。附加/执行步骤就会挂起。)您有关于调试这些情况的任何提示吗? - Kenny Ho
6
很不幸,kubectl run --overrides 忽略不存在的字段,详情可参考 https://github.com/kubernetes/kubernetes/issues/26804 。为了使其能够交互,需要在容器覆盖中使用 ttystdin。参数也来自于覆盖。以下是已更新的代码片段 https://gist.github.com/ctaggart/c372783291162d9c0b8e40441ab14845 - Cameron Taggart
1
在Kubernetes 1.13上,您可能需要使用--generator=job/v1。即使如此,您仍将收到弃用警告。 - remram
@CameronTaggart 请将您的评论和代码片段转化为答案,这样我就可以点赞了。 - user1202136

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接