<p>I would like to run a one-off container from the command line in my Kubernetes cluster. The equivalent of:</p>
<pre><code>docker run --rm -it centos /bin/bash
</code></pre>
<p>Is there a <code>kubectl</code> equivalent?</p>

<p>The <code>kubectl</code> equivalent of</p>
<pre><code>docker run --rm -it centos /bin/bash
</code></pre>
<p>is</p>
<pre><code>kubectl run tmp-shell --restart=Never --rm -i --tty --image centos -- /bin/bash
</code></pre>
<p>Notes:</p>
<ul>
<li><p>This will create a Pod named <code>tmp-shell</code>. If you don't specify <code>--restart=Never</code>, a Deploment will be created instead (credit: Urosh T's answer).</p>
</li>
<li><p><code>--rm</code> ensures the Pod is deleted when the shell exits.</p>
</li>
<li><p>If you want to detach from the shell and leave it running with the ability to re-attach, omit the <code>--rm</code>. You will then be able to reattach with: <code>kubectl attach $pod-name -c $pod-container -i -t</code> after you exit the shell.</p>
</li>
<li><p>If your shell does not start, check whether your cluster is out of resources (<code>kubectl describe nodes</code>). You can specify resource requests with <code>--requests</code>:</p>
<pre><code>--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.
</code></pre>
</li>
</ul>
<p>(Credit: <a href="https://gc-taylor.com/blog/2016/10/31/fire-up-an-interactive-bash-pod-within-a-kubernetes-cluster" rel="noreferrer">https://gc-taylor.com/blog/2016/10/31/fire-up-an-interactive-bash-pod-within-a-kubernetes-cluster</a>)</p>
