kubectl run vs kubectl create: Pods, Controllers and the Traps
kubectl run and kubectl create both make objects from one command line, and the split between them is simple: kubectl run makes a bare Pod, kubectl create <kind> makes everything else (Deployments, Jobs, CronJobs, Services, ConfigMaps, RBAC…). The details are where the time goes: the --rm -it one-liner that hangs for a minute, the nslookup test that fails on a healthy cluster, the --overrides that wipes your image, the flag after -- that creates the object instead of printing it. This article shows each command and the traps found while testing them.
1. The Short Answer
kubectl run |
kubectl create <kind> |
|
|---|---|---|
| Creates | One Pod, nothing else | The named kind: deployment, job, cronjob, service, configmap, secret, role… |
| Owner of the pods | None (ownerReferences empty) |
Deployment → ReplicaSet, Job, CronJob → Job; most kinds make no pods |
| Pod deleted, or its node lost | Gone for good | The controller makes a new one |
| Container crashes | The kubelet restarts it in place (restartPolicy: Always by default) |
Same, inside the controller’s pods |
| Scale, rolling update, rollback | No | Deployments: kubectl scale, kubectl rollout undo |
| Typical use | Debug shells, one-off tests, generating Pod YAML | Generating or creating everything else |
There is no kubectl create pod: it fails with Unexpected args: [pod]. But kubectl create -f pod.yaml creates a Pod without complaint: the -f form takes any kind. Only the generator subcommands skip Pods.
2. kubectl run
kubectl run nginx --image=nginx # Pod "nginx", label run=nginx
kubectl run test --image=busybox --command -- sleep 3600 # override the entrypoint
kubectl run nginx3 --image=nginx --labels='app=web,env=dev' # replaces run=nginx3
kubectl run nginx4 --image=nginx --port=80 # containerPort only
kubectl run nginx5 --image=nginx --env="DB_HOST=postgres" --env="DB_PORT=5432"
kubectl run job-pod --image=busybox --restart=Never --command -- echo "done"
Things to know about the defaults:
- The label is
run=<name>.--labelsreplaces it rather than adding to it, songinx3has norunlabel. restartPolicyisAlways.--restart=Neverand--restart=OnFailureonly change the policy; the result is still a Pod. (Before kubectl 1.18,--restartpicked a generator andkubectl runcould make Deployments and Jobs, which is why old tutorials disagree with this one. The--generatorflag is gone:error: unknown flag: --generator.)--portdoesn’t expose anything. It only setscontainerPort: 80; no Service is created.--exposedoes create one, and it needs--port:
kubectl run exposed --image=nginx --port=80 --expose # Pod and a ClusterIP Service, selector run=exposed
kubectl run exposed2 --image=nginx --expose # fails: --port must be set when exposing a service
-- and --command
Everything after -- belongs to the container. Without --command it becomes args (the image’s entrypoint still runs, with these arguments); with --command it becomes command (it replaces the entrypoint):
kubectl run a --image=busybox --dry-run=client -o yaml -- sleep 1 # args: [sleep, "1"]
kubectl run b --image=busybox --dry-run=client -o yaml --command -- sleep 1 # command: [sleep, "1"]
For busybox the two behave the same because the image has no entrypoint, only CMD ["sh"]. For an image with an ENTRYPOINT, plain -- passes your words to it as arguments. kubectl create job, cronjob and deployment have no --command flag: their -- always sets command.
Removed flags
Many older guides use flags that no longer exist; each fails with unknown flag:
kubectl run nginx6 --image=nginx --requests=cpu=100m
kubectl run x --image=nginx --serviceaccount=default
--limits, --replicas and --schedule fail the same way: all of them belonged to the old generators. Use --overrides, or generate YAML and edit it.
3. –overrides: Repeat the Name and the Image, or Use strategic
--overrides takes a JSON fragment and merges it into the generated Pod. The default --override-type=merge is a JSON merge patch, and a merge patch replaces arrays instead of merging them. containers is an array, so your fragment replaces the generated container, and the API rejects a container with no name and no image:
kubectl run noname --image=nginx --overrides='{"spec":{"containers":[{"resources":{"requests":{"cpu":"100m"}}}]}}'
That’s why the usual example spells out name and image again:
kubectl run nginx2 --image=nginx --overrides='{"spec":{"containers":[{"name":"nginx2","image":"nginx","resources":{"requests":{"cpu":"100m","memory":"128Mi"}}}]}}'
--override-type=strategic merges containers by name, as kubectl apply does, so the name (which is the Pod name) is enough and the image from --image is kept:
kubectl run strat --image=nginx --override-type=strategic \
--overrides='{"spec":{"containers":[{"name":"strat","resources":{"requests":{"cpu":"100m"}}}]}}'
Fields outside an array merge fine with the default type, e.g. {"spec":{"serviceAccountName":"my-sa"}} or {"spec":{"nodeSelector":{"disktype":"ssd"}}}.
4. Throwaway Pods: –rm -it Needs –restart=Never
The classic debug shell works:
kubectl run debug --rm -it --restart=Never --image=busybox -- sh
The trouble starts with a one-shot command such as curl or nslookup, which exits on its own. With the default restartPolicy: Always, the kubelet restarts the finished container over and over, and kubectl waits for a minute and gives up with timed out waiting for the condition, without showing the output (the pod had restarted 3 times by then):
time kubectl run curl --rm -it --image=curlimages/curl -- curl -s -o /dev/null -w '%{http_code}\n' http://web
With --restart=Never, the same command printed the answer in 3 seconds:
kubectl run curl2 --rm -it --restart=Never --image=curlimages/curl -- curl -s -o /dev/null -w '%{http_code}\n' http://web
Make --rm -it --restart=Never one habit. When the command fails, kubectl also tells you so and exits non-zero (pod <namespace>/<name> terminated (Error)), which scripts can check.
5. Debugging One-Liners That Actually Work
DNS: Use the Full Name with BusyBox
The popular nslookup kubernetes.default fails with NXDOMAIN on a healthy cluster with current busybox:
kubectl run dnstest2 --rm -it --restart=Never --image=busybox -- nslookup kubernetes.default
BusyBox’s nslookup doesn’t apply the search domains from the pod’s /etc/resolv.conf, so it asks for the literal name kubernetes.default. and gets NXDOMAIN. The fully qualified name works:
kubectl run dnstest3 --rm -it --restart=Never --image=busybox -- nslookup kubernetes.default.svc.cluster.local
Short names still work for real applications (glibc and musl resolvers apply the search list); only the test tool is misleading.
HTTP from Inside the Cluster
kubectl run curl --rm -it --restart=Never --image=curlimages/curl -- curl -sS http://my-service:8080/health
Pin to a Node: the Name Must Exist
spec.nodeName skips the scheduler and binds the pod to that node directly:
kubectl run nodetest --image=busybox --overrides='{"spec":{"nodeName":"colima"}}' --command -- sleep 3600
A typo in the node name fails silently. With no scheduler involved there is no FailedScheduling event; the pod stays Pending, and the pod garbage collector deletes it a few seconds later (about 15 in this test) because it’s bound to a node that doesn’t exist. kubectl get events shows nothing about it:
kubectl run ghost --image=busybox --overrides='{"spec":{"nodeName":"worker-1"}}' --command -- sleep 3600
Check with kubectl get nodes first, or use {"spec":{"nodeSelector":{"kubernetes.io/hostname":"worker-1"}}}, which goes through the scheduler and leaves a FailedScheduling event if it can’t place the pod.
Run as a Service Account
kubectl run satest --image=busybox --overrides='{"spec":{"serviceAccountName":"my-sa"}}' --command -- sleep 3600
The service account has to exist in the pod’s namespace; the pod above ran as my-sa.
6. kubectl create
kubectl create deployment nginx --image=nginx --replicas=3
kubectl create job my-job --image=busybox -- echo hello
kubectl create cronjob my-cron --image=busybox --schedule="*/5 * * * *" -- echo hi
kubectl create service clusterip my-svc --tcp=80:8080
kubectl create configmap my-config --from-literal=key=value
kubectl create secret generic my-secret --from-literal=password=s3cr3t
kubectl create namespace staging
kubectl create serviceaccount my-sa
kubectl create role pod-reader --verb=get,list --resource=pods
kubectl create rolebinding read-pods --role=pod-reader --serviceaccount=runcreate:my-sa
The pods that come out of the Deployment and the Job have an owner (ReplicaSet, Job); the ones from kubectl run show <none>:
kubectl get pods -o custom-columns=NAME:.metadata.name,OWNER:.metadata.ownerReferences[0].kind
The full list of generators in kubectl 1.37: clusterrole, clusterrolebinding, configmap, cronjob, deployment, ingress, job, namespace, poddisruptionbudget, priorityclass, quota, role, rolebinding, secret, service, serviceaccount and token (which prints a token rather than creating an object).
Run a CronJob Now
--from copies the job template of an existing CronJob, which is the easiest way to test one without waiting for the schedule:
kubectl create job from-cron --from=cronjob/my-cron
Traps in kubectl create
--serviceaccount is <namespace>:<name>, and the namespace is not filled in for you. The SA was created in runcreate. The binding below, copied from a guide written for the default namespace, grants the permission to a default/my-sa that doesn’t exist, and the real account gets nothing. kubectl auth can-i answers yes for the first account and no for the real one:
kubectl create rolebinding read-pods --role=pod-reader --serviceaccount=default:my-sa
kubectl auth can-i list pods -n runcreate --as=system:serviceaccount:default:my-sa
kubectl auth can-i list pods -n runcreate --as=system:serviceaccount:runcreate:my-sa
create service selects app=<service name>. kubectl create service clusterip my-svc gets the selector {"app":"my-svc"}, which matches no pods unless you labelled them that way. To point a Service at existing pods, kubectl expose deployment nginx --port=80 copies the right selector.
Flags after -- go to the container. kubectl stops parsing at --, so a --dry-run placed after it is just more arguments for the container, and the object is created for real:
kubectl create deployment cmd --image=busybox -- sleep 3600 --dry-run=client -o yaml
Nothing is printed, a Deployment is created, and its pod runs sleep 3600 --dry-run=client -o yaml. sleep rejects it (invalid number '--dry-run=client') and the pod goes into CrashLoopBackOff. The generator commands above, including kubectl create job ... --dry-run=client -o yaml -- echo hi, keep every kubectl flag before -- for this reason.
Names collide. Each command fails with already exists if the object exists:
kubectl run nginx --image=nginx
kubectl create deployment nginx --image=nginx
7. Bare Pod vs Controller: Who Brings It Back
Delete the bare pod and one Deployment pod, then list the Deployment’s pods:
kubectl delete pod nginx nginx-56c45fd5ff-7lwss
kubectl get pods -l app=nginx
The ReplicaSet replaced its pod within seconds; the bare nginx pod stayed gone. A bare pod isn’t completely unprotected, though: the kubelet still restarts its containers according to restartPolicy. What it lacks is something to recreate the pod after a delete, an eviction or a node failure:
kubectl run crash --image=busybox -- sh -c 'exit 1' # RESTARTS keeps going up
8. YAML: Generate with run/create, Keep with apply
Both commands print instead of create with --dry-run=client -o yaml:
kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yaml
kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > deployment.yaml
kubectl create job my-job --image=busybox --dry-run=client -o yaml -- echo hi > job.yaml
kubectl create -f creates once and fails after that; kubectl apply -f creates or updates. Mixing them works, with a warning the first time, because create doesn’t store the last-applied-configuration annotation that apply diffs against:
kubectl create -f pod.yaml # created
kubectl create -f pod.yaml # fails: AlreadyExists
kubectl apply -f pod.yaml # works, warns about the missing annotation and adds it
kubectl apply -f pod.yaml # unchanged, no warning
Use kubectl create --save-config if you know the object will be managed with apply later. More generators, kubectl set, patch --local and validation are in How to Create Kubernetes YAML Manifests Quickly.
When to Use What
| Scenario | Command |
|---|---|
| Debug shell | kubectl run debug --rm -it --restart=Never --image=busybox -- sh |
| One-shot test (curl, nslookup) | kubectl run t --rm -it --restart=Never --image=... -- <cmd> |
| Quick test of an image | kubectl run test --image=myapp:1.2.3 |
| Pod YAML to edit | kubectl run ... --dry-run=client -o yaml |
| Long-running app | kubectl create deployment ... --dry-run=client -o yaml, then kubectl apply -f |
| One-off task | kubectl create job ... |
| Scheduled task | kubectl create cronjob ... |
| Run a CronJob now | kubectl create job <name> --from=cronjob/<cron> |
| Anything you’ll change later | A YAML file and kubectl apply -f |
Quick Reference
| Gotcha | Fix |
|---|---|
--rm -it one-liner hangs a minute, timed out waiting for the condition |
Add --restart=Never |
nslookup kubernetes.default → NXDOMAIN with busybox |
nslookup kubernetes.default.svc.cluster.local |
--overrides → containers[0].image: Required value |
Repeat name and image, or --override-type=strategic |
--port=80 but no Service |
--expose (needs --port), or kubectl expose |
unknown flag: --requests / --serviceaccount / --generator |
--overrides, or edit generated YAML |
Pod with nodeName vanishes, no events |
The node name doesn’t exist (kubectl get nodes) |
--dry-run ignored, object created |
Put kubectl flags before -- |
| RoleBinding grants nothing | --serviceaccount=<namespace>:<name> |
create service matches no pods |
Selector is app=<service name>; use kubectl expose |
kubectl apply warns about last-applied-configuration |
Object came from create; use create --save-config or apply |