Exercises
Five small kubectl exercises that build muscle memory for the workflows you will use weekly.
Five exercises
EXAMPLE
# 1. Deploy nginx and reach it from your laptop
kubectl create deployment web --image=nginx:1.27
kubectl expose deployment web --port=80 --target-port=80 --type=NodePort
kubectl get svc web -o jsonpath='{.spec.ports[0].nodePort}'
# curl http://<node-ip>:<nodeport>
# 2. Scale up and down
kubectl scale deployment web --replicas=5
kubectl get pods -l app=web -w # watch them come up
kubectl scale deployment web --replicas=0
# 3. Roll out a new image and observe
kubectl set image deployment/web nginx=nginx:1.27-alpine
kubectl rollout status deployment/web
kubectl rollout history deployment/web
# 4. Bad rollout - roll back
kubectl set image deployment/web nginx=nginx:does-not-exist
kubectl rollout status deployment/web --timeout=30s # will fail
kubectl rollout undo deployment/web
kubectl rollout status deployment/web
# 5. ConfigMap + env
kubectl create configmap web-cfg \
--from-literal=GREETING='hello, k8s'
kubectl set env deployment/web --from=configmap/web-cfg
kubectl exec -it deploy/web -- printenv GREETING
# Stretch: write the equivalent YAML and apply with kubectl apply -f .
# Compare 'kubectl get deployment web -o yaml' before and after each step.
Why it matters
kubectl is a learnable surface. The verbs (get, describe, logs, exec, apply, rollout) cover 90 percent of day-to-day work. Get fluent with those before reaching for Helm, Kustomize, or operators.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Discussion
Loading…