The kubectl run
command is a powerful yet often misunderstood tool in Kubernetes. This guide will explain what it does, when to use it, and how to leverage it effectively—especially for those preparing for the Certified Kubernetes Administrator (CKA) exam.
What is kubectl run
?
kubectl run
is an imperative command used to create Kubernetes resources directly from the command line. Its primary purpose is to quickly spin up pods or other workloads without writing YAML manifests. However, its behavior has evolved across Kubernetes versions:
-
Pre-1.18: Created a Deployment by default (e.g.,
kubectl run nginx --image=nginx
created a Deployment, ReplicaSet, and Pod). - Post-1.18: Creates a Pod directly unless specific flags are used. This change simplified the command but caused confusion for users accustomed to older versions.
What Does kubectl run
Do?
In modern Kubernetes (v1.18+), kubectl run
creates a Pod by default. It allows you to specify:
- Container images
- Commands to run
- Environment variables
- Ports
- Labels
- Resource limits
It’s ideal for quick testing, debugging, or troubleshooting without the overhead of YAML files.
When to Use kubectl run
- Quick Prototyping: Test a container image or command instantly.
- Temporary Workloads: Run one-off tasks (e.g., a script in a BusyBox pod).
- Learning Kubernetes: Experiment with pods and their configurations.
- CKA Exam Scenarios: Speed up tasks by generating resources imperatively.
Avoid using it for production workloads. Declarative YAML manifests are better for reproducibility and version control.
Key Examples with Explanations
1. Run a Basic Pod
kubectl run nginx --image=nginx
- Creates a pod named
nginx
using thenginx
image. -
Verify:
kubectl get pods
2. Override the Default Command
kubectl run busybox --image=busybox --command -- sleep 3600
- Runs a
busybox
pod and executessleep 3600
instead of the default shell. - The
--command
flag ensures the arguments after it are treated as the command.
3. Set Environment Variables
kubectl run myapp --image=myapp --env="ENV=prod" --env="LOG_LEVEL=debug"
- Injects environment variables
ENV=prod
andLOG_LEVEL=debug
into the pod.
4. Expose a Port
kubectl run web --image=nginx --port=80
- Exposes port 80 on the pod. Useful for later creating a Service with
kubectl expose
.
5. Add Labels
kubectl run redis --image=redis --labels="tier=backend,app=store"
- Assigns labels
tier=backend
andapp=store
to the pod for easy filtering.
6. Run an Interactive Pod
kubectl run -it --rm debug-pod --image=busybox -- sh
- Starts an interactive pod (
-it
), attaches a terminal, and deletes it after exit (--rm
). - Use Case: Debugging or running ad-hoc commands.
7. Generate YAML for a Pod
kubectl run temp --image=nginx --dry-run=client -o yaml > pod.yaml
- Generates a YAML manifest without creating the pod. Great for learning or templating.
Common Use Cases for the CKA Exam
-
Quick Pod Creation: Use
kubectl run
to save time during the exam.
kubectl run test-pod --image=nginx
-
Troubleshooting: Start a debugging pod with tools like
busybox
.
kubectl run debugger --image=busybox --command -- curl http://service-name
-
Generate YAML: Combine with
--dry-run
to create YAML files.
kubectl run mypod --image=nginx --port=80 --dry-run=client -o yaml > pod.yaml
Best Practices
- Avoid in Production: Use declarative YAML for critical workloads.
- Clean Up: Delete temporary pods after use.
kubectl delete pod nginx
-
Combine with
kubectl expose
: Expose pods as Services.
kubectl run web --image=nginx --port=80
kubectl expose pod web --type=NodePort --port=80
FAQ
Q: How do I create a Deployment with kubectl run
?
A: In modern Kubernetes, use kubectl create deployment
instead:
kubectl create deployment my-dep --image=nginx
Q: Why doesn’t my pod restart after failure?
A: Check the restartPolicy
(default: Always
for pods). Use kubectl describe pod
to debug.
Q: How to set resource limits?
A: Use --limits
:
kubectl run stress --image=stress --limits="cpu=500m,memory=256Mi"
Summary
- Use Case: Quick pod creation for testing, debugging, or CKA exam tasks.
-
Key Flags:
--image
,--command
,--env
,--port
,--labels
. -
Pro Tip: Combine with
--dry-run=client -o yaml
to generate YAML templates.
Mastering kubectl run
streamlines your workflow and boosts efficiency in both learning and exam environments. Practice these examples to gain confidence!
Top comments (0)