Dmitry Minkovsky
Dmitry Minkovsky

Reputation: 38203

How do I run a container from the command line in Kubernetes (like docker run)?

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?

Upvotes: 42

Views: 33199

Answers (2)

Dmitry Minkovsky
Dmitry Minkovsky

Reputation: 38203

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)

Upvotes: 77

Urosh T.
Urosh T.

Reputation: 3824

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

Upvotes: 17

Related Questions