Full lesson
Explore the full explanation, examples, and visuals at your own pace.
deployment.yaml copied from a blog
Stop writing Kubernetes YAML from scratch. This file was copied from a blog, and someone spent ten minutes fixing its indentation. But the apiVersion line, the label that tells Kubernetes which schema to use, says extensions/v1beta1. That's outdated, so current clusters reject it anyway.
apiVersion: extensions/v1beta1 # outdated, rejected
kind: Deployment
metadata:
name: web
spec:
template:
metadata: # wrong indent
labels:
app: web
spec: # wrong indent
containers:
- name: nginx
image: nginxCopy and fix vs generate
On the left, the copied file carries its age with it: an outdated apiVersion and spacing mistakes. Fixing it by hand means guessing what's wrong. On the right, kubectl already knows the correct structure for your version, so one command produces a clean web.yaml. Let the tool write it.
One command, valid web.yaml
With dry-run equals client, kubectl builds the Deployment locally and creates nothing in the cluster. Dash o yaml prints it as YAML, and the greater-than sign saves it to web.yaml. Notice apps/v1 and the matching web labels. Now just edit what differs, like replicas.
$ kubectl create deployment web --image=nginx \
--dry-run=client -o yaml > web.yaml
# web.yaml (trimmed)
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: web
name: web
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- image: nginx
name: nginxLet kubectl write the boilerplate. You write the changes.
Let kubectl write the boilerplate. You write the changes. Generate the file first, edit it second, and only then run kubectl apply.
