【问题标题】:Create kubernetes pod with volume using kubectl run使用 kubectl run 创建具有卷的 kubernetes pod
【发布时间】:2016-09-30 00:45:41
【问题描述】:

我知道您可以使用 kubectl run 创建一个带有 Deployment/Job 的 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?

【问题讨论】:

标签: kubernetes kubectl


【解决方案1】:

您的 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

为了调试这个问题,我运行了你指定的命令,然后在另一个终端运行:

kubectl get job ubuntu -o json

从那里您可以看到实际的作业结构与您的 json 覆盖不同(您缺少嵌套的模板/规范,并且卷、volumeMounts 和容器需要是数组)。

【讨论】:

  • 谢谢,尤其是调试它的技巧。看起来做一个创建然后附加可能是一个更好的方法......如果我能让它工作的话。 (出于某种原因,我什至很难让 skydns 验证步骤正常工作。附加/执行步骤只是挂起。)您对调试这些情况有什么建议吗?
  • kubectl run --overrides 忽略不存在的字段真的很不幸 github.com/kubernetes/kubernetes/issues/26804 。为了使其具有交互性,容器覆盖中需要ttystdin。 args 也来自覆盖。这是更新的要点gist.github.com/ctaggart/c372783291162d9c0b8e40441ab14845
  • 您可能需要在 Kubernetes 1.13 上使用 --generator=job/v1。即使那样,您也会收到弃用警告
  • @CameronTaggart 请发表您的评论并给出答案,以便我投票。
猜你喜欢
  • 1970-01-01
  • 2020-10-23
  • 2021-09-30
  • 2022-10-02
  • 2016-01-10
  • 2020-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多