使用 kubectl 运行 创建带卷的 kubernetes pod

Create kubernetes pod with volume using kubectl run

我知道您可以使用 kubectl 运行 创建一个带有 Deployment/Job 的 pod。但是有没有可能创建一个附有卷的呢?我尝试了 运行ning 这个命令:

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?

您的 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 覆盖不同(您缺少嵌套的 template/spec,并且卷、volumeMounts 和容器需要是数组)。