39

I would like to run a one-off container from the command line in my Kubernetes cluster. The equivalent of:

docker run --rm -it centos /bin/bash

Is there a kubectl equivalent?

Dmitry Minkovsky
  • 36,185
  • 26
  • 116
  • 160

2 Answers2

71

The kubectl equivalent of

docker run --rm -it centos /bin/bash

is

kubectl run tmp-shell --restart=Never --rm -i --tty --image centos -- /bin/bash

Notes:

  • This will create a Pod named tmp-shell. If you don't specify --restart=Never, a Deploment will be created instead (credit: Urosh T's answer).

  • --rm ensures the Pod is deleted when the shell exits.

  • If you want to detach from the shell and leave it running with the ability to re-attach, omit the --rm. You will then be able to reattach with: kubectl attach $pod-name -c $pod-container -i -t after you exit the shell.

  • If your shell does not start, check whether your cluster is out of resources (kubectl describe nodes). You can specify resource requests with --requests:

    --requests='': The resource requirement requests for this container.  For example, 'cpu=100m,memory=256Mi'.  Note that server side components may assign requests depending on the server configuration, such as limit ranges.
    

(Credit: https://gc-taylor.com/blog/2016/10/31/fire-up-an-interactive-bash-pod-within-a-kubernetes-cluster)

Dmitry Minkovsky
  • 36,185
  • 26
  • 116
  • 160
  • kubectl run test-pod --generator=run-pod/v1 --image=busybox --image-pull-policy=IfNotPresent --restart=Never --rm --attach -- ping -c 5 google.com – Time Killer Aug 23 '19 at 09:02
  • @Dmitry where can I find this information: `$pod-container` ? – Felipe Pereira Jan 28 '21 at 15:17
  • @FelipePereira you can do something like `kubectl describe po $pod` which will list all the containers running in pod, where $pod is the name of the the pod that was created by the deployment. – Dmitry Minkovsky Jan 28 '21 at 15:20
  • If you have windows nodes that are not tainted you may want to try `kubectl run tmp-shell --restart=Never --rm -i --tty --image ubuntu --overrides='{"apiVersion": "v1", "spec": {"nodeSelector": { "beta.kubernetes.io/os": "linux" }}}' -- /bin/bash` – Jonas Kello Aug 10 '21 at 16:56
  • how do we provide security context as part of the command? I am gettting the error "Error: container has runAsNonRoot and image will run as root" when i run the command `kubectl run pod1 --restart=Never --rm -i --tty --image test1 --requests='cpu=1000m,memory=1000Mi' -n testnamespace -- /bin/bash` – user3679686 Sep 01 '21 at 18:01
  • how do you do this in a yml file? – Marci-man Oct 14 '21 at 10:45
  • From kubernetes version 1.18 `kubectl run` does not create deployment anymore because all the generators were deprecated. – Jan Groth Mar 19 '23 at 02:19
17

In order to have a Pod created instead of a Deployment and to have it removed by itself when you exit it, try this:

kubectl run curl-debug --rm -i --tty --restart=Never --image=radial/busyboxplus:curl -- /bin/sh

The --restart=Never flag is what it says to create a Pod instead of a Deployment object

Also - This image is lightweight, downloads fast and is good for network debugging.

Hope that helps

Urosh T.
  • 3,336
  • 5
  • 34
  • 42