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

6/23/2017

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

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

Is there a kubectl equivalent?

-- Dmitry Minkovsky
kubernetes

2 Answers

6/23/2017

It looks like the simplest way is:

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

Notes:

  • This will bring up a whole Deployment named tmp-shell. This happens any time you use kubectl run.
  • --rm ensures this Deployment and all of its components are deleted when you exit the shell. If you omitted --rm, you can delete it manually with kubectl delete deploy/tmp-shell.
  • 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 control the resources this deployment is requesting 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.

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

-- Dmitry Minkovsky
Source: StackOverflow

11/12/2019

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.
Source: StackOverflow