Skip to content
Latchkey

kubectl create: Usage, Options & Common CI Errors

Create resources imperatively - or scaffold YAML to commit.

kubectl create makes a brand-new resource and errors if it already exists. Its subcommands (secret, configmap, deployment, job) are also the fastest way to generate correct manifest YAML.

What it does

kubectl create -f reads a manifest and creates the objects, failing if any already exist (unlike apply, which patches). The subcommand forms - create secret generic, create configmap, create job - build resources from flags. Add --dry-run=client -o yaml to print the manifest instead of creating it, which is the canonical way to scaffold YAML.

Common usage

Terminal
kubectl create deployment web --image=nginx:1.27
kubectl create secret generic db --from-literal=pass=s3cr3t
kubectl create configmap app-cfg --from-file=./config/
kubectl create job --from=cronjob/report run-now
kubectl create deployment web --image=nginx --dry-run=client -o yaml > deploy.yaml

Common errors in CI

"Error from server (AlreadyExists): ... already exists" is the classic re-run failure: create is not idempotent, so a retried pipeline that already created the object fails the second time. Use kubectl apply for declarative idempotency, or for the secret/configmap pattern pipe through apply: kubectl create secret generic db --from-literal=pass=x --dry-run=client -o yaml | kubectl apply -f -. That generates the object and applies it idempotently.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →