While YAML files are the declarative standard for managing Kubernetes objects in production, imperative commands are an indispensable tool for quick tasks, debugging, and scripting. The kubectl run
command allows you to create certain cluster resources on the fly without writing a manifest file. Mastering these commands is essential for both day-to-day administration and for success on the CKA exam, where speed and efficiency are critical.
Important: In older Kubernetes versions (before 1.18), the kubectl run
command would create a Deployment object. In modern Kubernetes, its primary function is to create a single Pod, which is ideal for testing and debugging. Be aware of this change when consulting older documentation or exam materials. This evolution makes kubectl run
more focused and predictable for its current primary use cases.
You are a developer who needs to quickly deploy a simple NGINX pod for a test. You don't need a persistent configuration file; you just need to get the pod running immediately to verify a network setting. Additionally, the pod must have a specific label so it can be easily identified and cleaned up later. Your task is to accomplish this using a single kubectl
command.
test-pod
using the nginx:1.25-alpine
container image.env=test
to the newly created Pod.kubectl
command.test-pod
exists in the default
namespace.test-pod
is in a Running
state.test-pod
is using the nginx:1.25-alpine
image.test-pod
has the label env=test
correctly applied.This exercise does not require any initial setup. You will be creating the required resources from scratch using imperative commands.
kubectl run
.kubectl run
command? Check its help (kubectl run --help
) for the relevant flag.--labels
flag.Important Note on Image Versions: In this exercise, we use nginx:1.25-alpine
as an example. The Alpine variant is preferred as it provides a smaller, more secure base image with a reduced attack surface. In production environments, you should always:
latest
for predictable deploymentsWhile declarative YAML is the best practice for production workloads, imperative commands like kubectl run
are essential for efficiency in many situations:
This gives you a shell inside the cluster to run tools likekubectl run net-debug --image=busybox:1.36 -it --rm -- /bin/sh
ping
and nslookup
.kubectl run
allows you to work more efficiently and respond to issues more quickly in your day-to-day Kubernetes operations.