DEV Community

Hugo Martins
Hugo Martins

Posted on • Originally published at hugomartins.io on

2 2

What Can Be Done With 'kubectl run' Command?

kubectl run is a command that can “create and run a particular image in a pod”, which means that it will start a pod with a particular image. It can also create the pod with some particular details by using combinations of flags. We can see the code for it in pkg/cmd/run/run.go.

Interesting Flags

  • --image specifies the image to be executed.
  • --command allows to overwrite the command field in the container with whatever arguments are sent after --.
  • --env to set environment variables in the pods, in the form of key:value and one variable per --env flag.
  • --expose and --port to create a ClusterIP associated with the pod.
  • --labels which allows us to configure labels for the specific pod. Labels are accepted with only one flag, separated by a comma.
  • --dry-run for testing purposes.

Examples

From documentation:

kubectl run nginx --image=nginx
kubectl run hazelcast --image=hazelcast/hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default"
kubectl run hazelcast --image=hazelcast/hazelcast --labels="app=hazelcast,env=prod"
kubectl run nginx --image=nginx --command -- <cmd> <arg1> ... <argN>

Enter fullscreen mode Exit fullscreen mode

Use Case

While I don’t see much use in kubectl run for dealing with production grade systems, it can be a great resource for testing out different configurations of pods and creating a bunch of pods if there’s a need for it. It can also be a great introductory command for when first starting to deal with Kubernetes because it eases the interaction with Kubernetes without having to deal with YAML right away.

Due to its --output flag, it can also be a great way to use kubectl to create pods, in a stable way, from scripts because we can have stable and reproducible outputs from it. The fact that we can use a --dry-run flag is also a big plus.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay