DEV Community

Cover image for Kubectl Demystified: Mastering the `kubectl run` Command
Naveen.S
Naveen.S

Posted on

Kubectl Demystified: Mastering the `kubectl run` Command

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

  1. Quick Prototyping: Test a container image or command instantly.
  2. Temporary Workloads: Run one-off tasks (e.g., a script in a BusyBox pod).
  3. Learning Kubernetes: Experiment with pods and their configurations.
  4. 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
Enter fullscreen mode Exit fullscreen mode
  • Creates a pod named nginx using the nginx image.
  • Verify: kubectl get pods

2. Override the Default Command

kubectl run busybox --image=busybox --command -- sleep 3600
Enter fullscreen mode Exit fullscreen mode
  • Runs a busybox pod and executes sleep 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"
Enter fullscreen mode Exit fullscreen mode
  • Injects environment variables ENV=prod and LOG_LEVEL=debug into the pod.

4. Expose a Port

kubectl run web --image=nginx --port=80
Enter fullscreen mode Exit fullscreen mode
  • 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"
Enter fullscreen mode Exit fullscreen mode
  • Assigns labels tier=backend and app=store to the pod for easy filtering.

6. Run an Interactive Pod

kubectl run -it --rm debug-pod --image=busybox -- sh
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • Generates a YAML manifest without creating the pod. Great for learning or templating.

Common Use Cases for the CKA Exam

  1. Quick Pod Creation: Use kubectl run to save time during the exam.
   kubectl run test-pod --image=nginx
Enter fullscreen mode Exit fullscreen mode
  1. Troubleshooting: Start a debugging pod with tools like busybox.
   kubectl run debugger --image=busybox --command -- curl http://service-name
Enter fullscreen mode Exit fullscreen mode
  1. Generate YAML: Combine with --dry-run to create YAML files.
   kubectl run mypod --image=nginx --port=80 --dry-run=client -o yaml > pod.yaml
Enter fullscreen mode Exit fullscreen mode

Best Practices

  1. Avoid in Production: Use declarative YAML for critical workloads.
  2. Clean Up: Delete temporary pods after use.
   kubectl delete pod nginx
Enter fullscreen mode Exit fullscreen mode
  1. Combine with kubectl expose: Expose pods as Services.
   kubectl run web --image=nginx --port=80
   kubectl expose pod web --type=NodePort --port=80
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Image description

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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

If this article connected with you, consider tapping ❤️ or leaving a brief comment to share your thoughts!

Okay