How to run Shell Commands in a Kubernetes Cluster

If you would like to run shell commands inside of your Kubernetes cluster, then you can use the kubectl run command.

What the syntax looks like

kubectl run --image=<imagename> <name> -restart=Never -it --rm -- /bin/sh -c "<command>"
Code language: Bash (bash)

Breaking this down, you pass in an imagename which is the container image you would like to execute the commands in, the name of this temporary container, and then command you would like to run in it.

The -it indicates that it is an interactive command, while --rm tells the container to not persist once it has completed execution.

An example command

kubectl run --image=busybox bbox1 --restart=Never -it --rm -- /bin/sh -c "wget -q0"
Code language: Bash (bash)

Note that the IP above should be swapped out with the IP address you want to run wget against. You can get your Cluster IP by running kuebctl get svc.

Notify of
Inline Feedbacks
View all comments