Writing YAML from memory is slow and easy to get wrong. kubectl writes the first draft for you.
Dry run, print YAML
Add two flags to any command that creates something:
text
learner@shallows:~$ kubectl run web --image=nginx:1.27 --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: web
name: web
spec:
containers:
- image: nginx:1.27
name: web
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}--dry-run=clientdo not send it-o yamlprint it as YAML
Nothing was created. creationTimestamp: null and status: {} are empty server fields; leave them or delete them.
Save, edit, apply
1. Generate into a filekubectl run ... --dry-run=client -o yaml > web.yaml
2. Edit what the flags could not sayvim web.yaml
3. Applykubectl apply -f web.yaml
text
learner@shallows:~$ kubectl run web --image=nginx:1.27 --dry-run=client -o yaml > web.yaml
learner@shallows:~$ vim web.yaml
learner@shallows:~$ kubectl apply -f web.yaml -n shop
pod/web createdThe generator knows the structure. You know the values. Change the label, add a second container, add a field from kubectl explain: everything the flags could not express goes in during step 2.
The same two flags work on every create command you meet later: Deployments, Services, ConfigMaps, Jobs.