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

时间:2016-05-31 20:44:03

标签: kubernetes kubectl

据我所知,您可以使用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?

1 个答案:

答案 0 :(得分:24)

您的JSON覆盖指定不正确。不幸的是,kubectl只是忽略了它不理解的字段。

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和容器需要是数组)。