There are two styles of working with Kubernetes, and the exam rewards knowing both.
Imperative: tell it what to do, one command at a time. Fast to type, perfect under exam pressure.
kubectl create deployment shop \
--image=shop:1.0 --replicas=3
Create a Deployment.
kubectl scale deployment shop --replicas=5
Change how many copies you want.
kubectl expose deployment shop \
--port=80 --target-port=8080
Create a Service for it, with the selector filled in for you.
kubectl run tmp --image=nginx
Create a single bare Pod (handy for quick tests).
kubectl run tmp --image=busybox:1.36 \
--rm -it --restart=Never -- \
wget -qO- http://shop
Test a Service from inside the cluster: a throwaway Pod fetches it, prints the answer, then disappears.
kubectl run job1 --image=busybox:1.36 \
--restart=Never -- echo hi
A one-shot Pod: restartPolicy: Never, so it runs once and stays Completed.
kubectl run web --image=nginx \
--dry-run=client -o yaml
Draft a Pod's YAML without creating anything.
kubectl port-forward svc/shop 8080:80
Reach the Service from your own machine at localhost:8080 (stop with Ctrl+C).
kubectl annotate pod shop-xk2pq \
note="canary"
Attach a note to something that exists. Unlike labels, annotations are not used to select objects.
kubectl label pod shop-xk2pq tier=web
Add a name tag to something that exists.
kubectl delete pod shop-xk2pq
Delete a Pod. Its Deployment replaces it.
Declarative: describe what you want in a YAML file and hand the whole file over. Repeatable, reviewable, and what teams use day to day.
kubectl apply -f shop.yaml
Create or update everything in the file to match it.
kubectl diff -f shop.yaml
Preview what apply would change.
kubectl delete -f shop.yaml
Remove everything the file describes.
Typing YAML from memory is slow and error-prone. Have kubectl draft it, then edit only what you need:
kubectl create deployment shop --image=shop:1.0 --replicas=3 \ --dry-run=client -o yaml > shop.yaml
--dry-run=client means "don't create anything, just show me", and -o yaml prints it as YAML. This is the single most useful trick on the exam.
-o wide
Extra columns, like IP and Node.
-o yaml
The full object, every field.
--show-labels
Show each object’s name tags.
-l app=shop
Only objects with this label.
-w
Keep watching for changes.
kubectl explain deployment.spec
Built-in docs for any field.
kubectl edit deployment shop or by re-applying its file. Edit the Deployment, not its Pods: they are made from the Deployment's recipe, so a hand-made change is lost the next time a Pod is replaced.