Mounting a volume with kubectl

9/29/2018

I want to mount a volume with kubectl and get a shell in the environment.

I've tried this:

kubectl run -i --rm --tty alpine --overrides='
{
  "apiVersion": "v1",
  "spec": {
    "template": {
      "spec": {
        "containers": [
          {
            "name": "alpine",
            "image": "alpine:latest",
            "args": [
              "sh"
            ],
            "stdin": true,
            "stdinOnce": true,
            "tty": true,
            "volumeMounts": [{
              "mountPath": "/home/store",
              "name": "store"
            }]
          }
        ],
        "volumes": [{
          "name":"store",
          "emptyDir":{}
        }]
      }
    }
  }
}
'  --image=alpine:latest --restart=Never -- sh

I'm not getting any errors but the volume is not present at the mount path /home/store:

~ # ls -lah /home/
total 8
drwxr-xr-x    2 root     root        4.0K Sep 11 20:23 .
drwxr-xr-x    1 root     root        4.0K Sep 29 09:47 ..

I'm looking for the most direct way to use a volume with kubectl run for debugging purposes.

-- Juicy
kubectl
kubernetes
minikube
volumes

1 Answer

9/29/2018

TL;DR I don't know what the issue was but I ended up solving this by making the build request very verbose.

I ended up solving this by setting debug to very verbose (v=0) and noticing that my volume mount was completely ignored by kubectl and not present in the request to the API:

I0929 13:31:22.429307   14616 request.go:897] Request Body: {"kind":"Pod","apiVersion":"v1","metadata":{"name":"alpine","creationTimestamp":null,"labels":{"run":"alpine"}},"spec":{"volumes":[{"name":"store","emptyDir":{}}],"containers":[{"name":"alpine","image":"alpine:latest","args":["sh"],"resources":{},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File","imagePullPolicy":"IfNotPresent","stdin":true,"stdinOnce":true,"tty":true}],"restartPolicy":"Never","terminationGracePeriodSeconds":30,"dnsPolicy":"ClusterFirst","securityContext":{},"schedulerName":"default-scheduler"},"status":{}}

I copy pasted that request, and edited it to add the same volume mount as above, and it worked:

kubectl run -i --rm --tty alpine --overrides='
{
    "kind": "Pod",
    "apiVersion": "v1",
    "metadata": {
        "name": "alpine",
        "creationTimestamp": null,
        "labels": {
            "run": "alpine"
        }
    },
    "spec": {
        "containers": [{
            "name": "alpine",
            "image": "alpine:latest",
            "args": ["sh"],
            "resources": {},
            "terminationMessagePath": "/dev/termination-log",
            "terminationMessagePolicy": "File",
            "imagePullPolicy": "IfNotPresent",
            "stdin": true,
            "stdinOnce": true,
            "tty": true,
            "volumeMounts": [{
              "mountPath": "/home/store",
              "name": "store"
            }]
        }],
        "volumes": [{
          "name":"store",
          "emptyDir":{}
        }],
        "restartPolicy": "Never",
        "terminationGracePeriodSeconds": 30,
        "dnsPolicy": "ClusterFirst",
        "securityContext": {},
        "schedulerName": "default-scheduler"
    },
    "status": {}
}
'  --image=alpine:latest -v=9 --restart=Never -- sh
-- Juicy
Source: StackOverflow