kubectl create: Command Reference for CI/CD
Create a brand-new resource imperatively, or scaffold manifest YAML.
kubectl create makes a new resource and errors if it already exists, which makes it the non-idempotent counterpart to apply. Its subcommands are also the fastest way to generate correct YAML to commit. This reference covers the flags and the safe CI patterns.
Common flags and usage
- -f, --filename: create from a manifest (fails on AlreadyExists)
- create deployment|job|namespace: build a resource from flags
- --dry-run=client -o yaml: print the manifest instead of creating it
- --from=cronjob/<name>: create a one-off Job from a CronJob
- --save-config: store last-applied so future apply merges cleanly
Example
shell
# Scaffold YAML to commit
kubectl create deployment web --image=web:${GIT_SHA} \
--dry-run=client -o yaml > k8s/web.yaml
# Idempotent create-then-apply in a re-runnable job
kubectl create namespace ci-${BUILD_ID} \
--dry-run=client -o yaml | kubectl apply -f -In CI
create is not idempotent, so a retried pipeline that already created an object fails with AlreadyExists. Pipe create through apply (create ... --dry-run=client -o yaml | kubectl apply -f -) to get the generator ergonomics with declarative idempotency.
Key takeaways
- create fails if the object exists; apply patches it instead.
- --dry-run=client -o yaml turns create into a manifest generator.
- For re-runnable CI, pipe create output into apply for idempotency.
Related guides
kubectl Cheat Sheet: Commands for Everyday KubernetesA kubectl cheat sheet - get, describe, logs, apply, rollout, debug, and context commands for daily Kubernetes…
kubectl apply: Command Reference for CI/CDReference for kubectl apply: declarative create-or-update from manifests, server-side apply, dry-run validati…
kubectl create secret: Command Reference for CI/CDReference for kubectl create secret: build generic, docker-registry, and tls Secrets from literals or files,…
kubectl create configmap: Command Reference for CI/CDReference for kubectl create configmap: build a ConfigMap from literals, files, or a directory, the create-pi…