51

I understand that you can create a pod with Deployment/Job using kubectl run. But is it possible to create one with a volume attached to it? I tried running this command:

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

But the volume does not appear in the interactive bash.

Is there a better way to create a pod with volume that you can attach to?

1

1 Answer 1

59

Your JSON override is specified incorrectly. Unfortunately kubectl run just ignores fields it doesn't understand.

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

From there you can see that the actual job structure differs from your json override (you were missing the nested template/spec, and volumes, volumeMounts, and containers need to be arrays).

4
  • Thanks, especially the tip on debugging it. Looks like doing a create and then attach may be a better way to go... if only I can get it working. (For some reason, I have the hardest time getting even the skydns validation step to work. The attach/exec step just hangs.) Do you have any tips on debugging those situation?
    – Kenny Ho
    Jun 3, 2016 at 20:42
  • 6
    kubectl run --overrides ignoring non existent fields is really unfortunate github.com/kubernetes/kubernetes/issues/26804 . In order to get it interactive, tty and stdin are required in the container override. The args come from the override as well. Here is an updated gist gist.github.com/ctaggart/c372783291162d9c0b8e40441ab14845 Jul 27, 2018 at 17:01
  • 1
    You might need --generator=job/v1 on Kubernetes 1.13. You will get a deprecation warning even then
    – remram
    Nov 13, 2019 at 21:46
  • @CameronTaggart Please make your comment and gist an answer so I can upvote it. Dec 11, 2019 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.