Generate manifests fast with --dry-run
Writing YAML manifests from scratch is error-prone: a misplaced indent or a forgotten field and the resource fails to create. A very useful practice is to let kubectl generate the manifest skeleton for you and then tweak it.
The fastest way: --dry-run=client -o yaml
The general pattern is:
kubectl create <resource> [options] --dry-run=client -o yaml > resource.yml
--dry-run=client: simulates the operation locally, it does not contact the API server nor create the object.-o yaml: prints the manifest in YAML format.>redirects the output to a file you can edit and then apply withkubectl apply -f.
Practical examples
Deployment
kubectl create deployment nginx \
--image=nginx:1.27 \
--replicas=3 \
--dry-run=client -o yaml > nginx-deploy.yml
Service
kubectl create service clusterip my-app \
--tcp=80:8080 \
--dry-run=client -o yaml > my-app-svc.yml
ConfigMap from a file
kubectl create configmap app-config \
--from-file=config.properties \
--dry-run=client -o yaml > app-config.yml
Job and CronJob
kubectl create job backup --image=alpine \
--dry-run=client -o yaml > backup-job.yml
kubectl create cronjob cleanup --image=alpine --schedule="0 2 * * *" \
-- /bin/sh -c "echo cleaning" \
--dry-run=client -o yaml > cleanup-cron.yml
Role and RoleBinding
kubectl create role reader \
--verb=get,list,watch --resource=pods \
--dry-run=client -o yaml > role.yml
kubectl create rolebinding reader-binding \
--role=reader --serviceaccount=default:myapp \
--dry-run=client -o yaml > rolebinding.yml
Variant: --dry-run=server
If you replace client with server, kubectl sends the request to the API server but does not persist the object. This validates that the manifest would be accepted by admission controllers and webhooks. Useful when you need to confirm a resource would pass cluster validations before actually applying it.
kubectl apply -f resource.yml --dry-run=server
Tip: combine with kubectl explain
Once you have the skeleton, use kubectl explain to discover optional fields you may want to add:
kubectl explain deployment.spec.strategy
kubectl explain pod.spec.containers.resources
This way you avoid memorizing the YAML structure and you speed up the creation of clean, valid manifests.