Deploying your First Application
The local cluster is running. Now the notes app goes in — and you can reach it from your browser. There are two ways to get it there, and knowing why teams prefer one of them is the real lesson.
The rule
Commands are for trying; files are for keeping.
The imperative way tells the cluster what to do, one command at a time:
kubectl create namespace notes
kubectl create deployment notes-web --image=ghcr.io/example/notes-web:1.2 -n notes
kubectl expose deployment notes-web --port=80 --target-port=8080 -n notesQuick, but the only record of what you did is your shell history.
The declarative way writes the desired state in a file and applies it:
kubectl apply -f notes-web.yamlThe file can be reviewed, stored in Git and applied again. apply is safe to repeat: if nothing in the file changed, nothing in the cluster changes.
To reach the app from your laptop without any public address:
kubectl port-forward service/notes-web 8080:80 -n notesNotice the -n notes on every command. Objects live in a namespace, and a command without -n looks in default, where notes-web does not exist. "Not found" errors right after a deploy are usually a missing -n.
Cleaning up mirrors creating: kubectl delete -f notes-web.yaml removes exactly what the file created.
Now http://localhost:8080 reaches the app. Port-forward is a debugging tunnel, not a way to serve real users.
The shortcut
Use the "get, describe, logs" ladder whenever you deploy anything: each step looks one level deeper.
get
kubectl get pods -n notesshowsnotes-web-6f7b… 1/1 Running. The overview looks healthy.
A useful habit: generate a starting file from a command, then keep the file.
kubectl create deployment notes-web --image=ghcr.io/example/notes-web:1.2 --dry-run=client -o yaml > notes-web.yamlDrills
Answer fast — trust the rule.
Why do teams prefer kubectl apply -f files over one-off commands?
You run kubectl apply -f notes-web.yaml twice without editing the file. What happens the second time?
A pod is Running but the app returns errors. Which command shows the app's own output?
What does kubectl port-forward service/notes-web 8080:80 -n notes do?
Remember
- Commands are for trying; files are for keeping —
kubectl apply -f. - Check a deploy with the ladder: get, describe, logs.
- Trap: using port-forward as if it were real public access.
