Kubernetes is a container orchestration framework that can help you scale hundreds of app containers to meet your user traffic. Kubernetes provides powerful features and tools to manage K8s clusters.
kubectl, an open-source CLI, is one of the popular command-line tools used by engineers to interact with a K8s cluster.
In this guide, we will take a look at
kubectl run, a command that is used to run container images on Kubernetes resources.
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:
You will receive a similar output:
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:
You can control the port that the newly created instance will expose. Here’s how you can use it:
This is possibly one of the most useful options offered with this command. The dry-run option allows you to test the effects of a
kubectl run call without actually running in on your live cluster. Here’s how you can use it:
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
--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:
Or, you could pass them as commands, like this:
While they might appear similar, they are handled quite differently by K8s. If you are looking to run a command, always prefer the
--command option as it will override the
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
Cmd calls, and the final results can vary.
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.
You will often feel the need to create pods and image instances on the fly when working with Kubernetes. The
kubectl run command can help you do just that. In this guide, we showed you how you can achieve the same and also shared a few tips to keep in mind when using the
kubectl run command.
If you're looking for an internal tooling platform to help manage your Kubernetes application, then try out Airplane. With Airplane, you can quickly build tooling from SQL queries, Python scripts, REST API calls, and more. When combined with Airplane Views (React-based platform to build custom UIs), these scripts and queries can function as dashboards, admin panels, and more. Airplane workflows and UIs are also safely shareable across your organization, making it easy for anyone to run workflows or monitor UIs securely.