50

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?

Kenny Ho
  • 3,247
  • 5
  • 19
  • 24
  • I have also tried using kubectl create and then do an exec/attach but it didn't work for me. But that's probably because I was running into this bug: https://github.com/kubernetes/kubernetes/issues/16670 – Kenny Ho Jun 01 '16 at 18:01

1 Answers1

58

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).

Tim Allclair
  • 7,347
  • 2
  • 27
  • 25
  • 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 03 '16 at 20:42
  • 6
    `kubectl run --overrides` ignoring non existent fields is really unfortunate https://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 https://gist.github.com/ctaggart/c372783291162d9c0b8e40441ab14845 – Cameron Taggart Jul 27 '18 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 '19 at 21:46
  • @CameronTaggart Please make your comment and gist an answer so I can upvote it. – user1202136 Dec 11 '19 at 12:40