πŸ“šBook Signing at KubeCon EU 2026Meet us at Booking.com HQ (Mon 18:30-21:00) & vCluster booth #521 (Tue 24 Mar, 12:30-1:30pm) β€” free book giveaway!RSVP Booking.com Event
Deployments beginner ⏱ 8 minutes K8s 1.28+

kubectl run: Create Pod from Command Line

Use kubectl run to create pods and deployments from the command line. Dry-run output, resource limits, environment variables, and CKA exam patterns.

By Luca Berton β€’ β€’ πŸ“– 5 min read

πŸ’‘ Quick Answer: kubectl run nginx --image=nginx:1.27 --port=80 creates a pod named nginx. Add --dry-run=client -o yaml to generate YAML without creating. For CKA exams: kubectl run busybox --image=busybox --restart=Never --command -- sleep 3600 creates a non-restarting pod. Use --env, --labels, --requests, --limits for inline configuration.

The Problem

Creating pods via YAML is verbose for quick tasks:

  • Testing a container image
  • Running a one-off debugging pod
  • CKA/CKAD exam time pressure β€” imperative commands are faster
  • Generating YAML templates for further customization

The Solution

Basic Pod Creation

# Create a simple nginx pod
kubectl run nginx --image=nginx:1.27

# Create with port exposed
kubectl run nginx --image=nginx:1.27 --port=80

# Create and immediately attach
kubectl run -it busybox --image=busybox --restart=Never -- sh

# Create with labels
kubectl run nginx --image=nginx:1.27 --labels="app=web,tier=frontend"

Generate YAML (Dry Run)

# Generate YAML without creating the pod
kubectl run nginx --image=nginx:1.27 --port=80 \
  --dry-run=client -o yaml

# Output:
# apiVersion: v1
# kind: Pod
# metadata:
#   labels:
#     run: nginx
#   name: nginx
# spec:
#   containers:
#   - image: nginx:1.27
#     name: nginx
#     ports:
#     - containerPort: 80
#   restartPolicy: Always

# Save to file for editing
kubectl run nginx --image=nginx:1.27 --port=80 \
  --dry-run=client -o yaml > pod.yaml

Resource Limits and Requests

# Set CPU and memory requests/limits
kubectl run nginx --image=nginx:1.27 \
  --requests='cpu=100m,memory=128Mi' \
  --limits='cpu=500m,memory=256Mi'

# With environment variables
kubectl run nginx --image=nginx:1.27 \
  --env="DB_HOST=postgres" \
  --env="DB_PORT=5432"

CKA Exam Patterns

# Pod with command override
kubectl run busybox --image=busybox --restart=Never \
  --command -- sleep 3600

# Pod with args
kubectl run busybox --image=busybox --restart=Never \
  -- /bin/sh -c "echo hello && sleep 3600"

# Pod in specific namespace
kubectl run nginx --image=nginx:1.27 -n production

# Pod with service account
kubectl run nginx --image=nginx:1.27 \
  --overrides='{"spec":{"serviceAccountName":"my-sa"}}'

# Temporary pod for DNS testing
kubectl run dnstest --image=busybox:1.36 --restart=Never --rm -it \
  -- nslookup kubernetes.default

# Temporary pod for network testing
kubectl run curlpod --image=curlimages/curl --restart=Never --rm -it \
  -- curl -s http://my-service:8080/health

kubectl run vs kubectl create

Featurekubectl runkubectl create
Creates Podβœ…kubectl create -f pod.yaml
Creates Deployment❌ (removed in 1.18+)kubectl create deployment
Dry-run YAMLβœ… --dry-run=client -o yamlβœ… same flags
Interactiveβœ… -it❌
One-shot jobsβœ… --restart=Neverkubectl create job
Resource limitsβœ… --requests/--limitsOnly via YAML

Overrides for Advanced Config

# Add tolerations via JSON overrides
kubectl run gpu-test --image=nvidia/cuda:12.4.0-runtime-ubuntu22.04 \
  --overrides='{
    "spec": {
      "tolerations": [{"key": "nvidia.com/gpu", "operator": "Exists", "effect": "NoSchedule"}],
      "containers": [{"name": "gpu-test", "image": "nvidia/cuda:12.4.0-runtime-ubuntu22.04",
        "resources": {"limits": {"nvidia.com/gpu": "1"}}}]
    }
  }'

# Add node selector
kubectl run nginx --image=nginx:1.27 \
  --overrides='{"spec":{"nodeSelector":{"disk":"ssd"}}}'

Common Issues

β€œerror: β€”restart=OnFailure is not valid for pod”

Since K8s 1.18, kubectl run only creates Pods. Use kubectl create job for Jobs or kubectl create cronjob for CronJobs.

Pod stuck in Pending after kubectl run

No resource requests set β€” may hit ResourceQuota or LimitRange. Add --requests flag.

β€œalready exists” error

Delete the existing pod first: kubectl delete pod nginx then re-run.

Best Practices

  • Always use --dry-run=client -o yaml to preview before creating
  • Pin image tags β€” nginx:1.27 not nginx:latest
  • Use --restart=Never --rm -it for temporary debug pods that auto-cleanup
  • Set resource requests even on quick test pods to avoid quota issues
  • Combine imperative + declarative β€” generate YAML with kubectl run, edit, then kubectl apply

Key Takeaways

  • kubectl run creates pods quickly from the command line
  • --dry-run=client -o yaml generates YAML templates without creating resources
  • Use --restart=Never for one-shot pods, --rm -it for temporary debug pods
  • --overrides handles advanced config (tolerations, node selectors, service accounts)
  • Essential for CKA/CKAD exams where speed matters
#kubectl #pods #cka #imperative #deployments
Luca Berton
Written by Luca Berton

Principal Solutions Architect specializing in Kubernetes, AI/GPU infrastructure, and cloud-native platforms. Author of Kubernetes Recipes and creator of CopyPasteLearn courses.

Kubernetes Recipes book cover

Want More Kubernetes Recipes?

This recipe is from Kubernetes Recipes, our 750-page practical guide with hundreds of production-ready patterns.

Luca Berton Ansible Pilot Ansible by Example Open Empower K8s Recipes Terraform Pilot CopyPasteLearn ProteinLens