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