Polyaxon v3 is coming →

How to use kubectl run

Use kubectl run to create quick pods for testing images, commands, ports, and dry-run manifests in a Kubernetes cluster.

March 19, 2024by Polyaxon
Mar 19, 2024

How to use kubectl run

Use kubectl run to create quick pods for testing images, commands, ports, and dry-run manifests in a Kubernetes cluster.

Picture

kubectl run is a scratchpad command. It creates a pod quickly, which makes it useful for testing images, commands, ports, and small debugging scenarios.

It should not become your deployment workflow. Real workloads need manifests, version control, repeatability, and review. For ML teams, that line matters because one-off pods are easy to create and hard to reproduce.

What is Kubectl Run used for?

As stated before, the kubectl run command helps you to run container images on your Kubernetes pods. The syntax for the command is simple:

You can provide a name for the running instance of the image using the name field. Here's how you can create a pod with a basic nginx server:

kubectl run nginx --image=nginx

# Output

pod/nginx created

You can now view the newly created pod by running kubectl get pods.

Using '-port' and '-dry-run' with Kubectl Run

Some useful options offered with the run command include:

--port

You can specify the port on which the container will listen. Here's an example:

kubectl run nginx --image-nginx --port=5701

# Output

pod/nginx created

--dry-run

The --dry-run option allows you to test the command without actually creating the pod. This is useful for checking if the command is correct. Here's an example:

kubectl run nginx --image=nginx --dry-run

If you couple it with the -o yaml option, you can see the manifest that was generated for your new pod. The dry-run option is also offered for some other useful commands like create, apply, patch, etc.

Kubectl Run best practices

Here are a few best practices you should know when using kubectl run.

--command vs --arguments

You might want to run commands on your newly created pods. You can either append them to your kubectl run call like this:

kubectl run nginx --image=nginx -- 'echo hello;sleep 3600'

Or, you could pass them as commands, like this:

kubectl run nginx --image=nginx --command -- 'echo hello;sleep 3600'

While they might appear similar, they are handled quite differently by Kubernetes. If you are looking to run a command, always prefer the --command option as it will override the EntryPoint and Cmd defined in your container and run your command directly. While in the other case, your command will be passed as an argument to the EntryPoint and Cmd calls, and the final results can vary.

Creating deployment

kubectl run was earlier used to create deployments as well. However, with Kubernetes 1.18, kubectl run was updated to only create pods and it lost its deployment-specific options as well. If you are looking to create a deployment, you should instead use the kubectl create deployment command.

Final thoughts

kubectl run is good for quick experiments and throwaway pods. It is bad as a long-term deployment habit. If the workload matters, put the configuration in a manifest or a higher-level workload spec.

Polyaxon gives ML teams that repeatable layer for jobs and services. You can still debug with kubectl, but production training and serving should not depend on terminal history.