Kubectl: Run Pod With Command & Exec Into

Unless for very specific reasons, you should never run a Pod with a long-running application on its own, but use a Deployment for this.

Nevertheless, if you want to quickly run some container image on Kubernetes resources, you can use a kubectl run command.

For example, i often need to run the Pod on a Kubernetes cluster that starts a container from a wbitt/network-multitool image for container/network testing and troubleshooting.

This note shows how to run the Pod with the kubectl run command and how to log into the running Pod and start an interactive bash or sh session using the kubectl exec command.

Cool Tip: Get Pod’s logs using the kubectl command! Read more →

Run a Pod with a Command

To run a Pod from an image, execute the following command:

$ kubectl run <podName> --image=<imageName>
- examples -
$ kubectl run nginx --image=nginx
$ kubectl run multitool --image=wbitt/network-multitool:latest

If you need to run the Pod in a different Namespace, it can be specified as follows:

$ kubectl run <podName> --image=<imageName> --namespace <namespaceName>

Once the Pod is created, you should see the message about this in the terminal, for example:

pod/nginx created

To verify that the Pod is running, list all the Pods, by using one of these commands:

$ kubectl get pods
$ kubectl get pods --all-namespaces 
$ kubectl get pods --namespace <namespaceName>

Cool Tip: Login to a Pod using kubectl command! Read more →

Exec Into a Pod

To log into a running Pod, start an interactive bash or sh sessions by using the kubectl exec command, as follows:

$ kubectl exec -it <podName> -- /bin/bash
$ kubectl exec -it <podName> -- /bin/sh

If the Pod starts more than one container, you can list them all and log into a particular one, by running:

$ kubectl get pod <podName> -o jsonpath='{.spec.containers[*].name}{"\n"}'
$ kubectl exec -it <podName> -c <containerName> -- /bin/bash

Delete a Pod

To delete a Pod, execute one of these commands:

$ kubectl delete pod <podName>
$ kubectl delete pod <podName> --namespace <namespaceName>
Was it useful? Share this post with the world!

Leave a Reply