Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create kubernetes pod with volume using kubectl run

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?

like image 691
Kenny Ho Avatar asked May 31 '16 20:05

Kenny Ho


People also ask

How do you create a pod object using kubectl Run command?

Use kubectl run --generator=run-pod/v1 or kubectl create instead. We get a warning message because the --generator=deployment/apps. v1 flag (implicitly used as we do not provide any other flags) is deprecated. It is advised to use the --generator=run-pod/v1 flag if we want to create a Pod instead of a deployment.

Does kubectl run create a deployment or pod?

kubectl run was earlier used to create deployments as well. However, with Kubernetes 1.18, kubectl run was updated to only create pods and it lost its deployment-specific options as well. If you are looking to create a deployment, you should instead use the kubectl create deployment command.

What is kubectl Run command?

Kubectl controls the Kubernetes Cluster. It is one of the key components of Kubernetes which runs on the workstation on any machine when the setup is done. It has the capability to manage the nodes in the cluster. Kubectl commands are used to interact and manage Kubernetes objects and the cluster.


1 Answers

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

like image 54
Tim Allclair Avatar answered Sep 21 '22 20:09

Tim Allclair