Skip to content

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 http://12.34.56.78/"
Code language: Bash (bash)

Note that the IP above 12.34.56.78 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.

See also  Solve the Number of Recent Calls Challenge in Java
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x