Sub topic 9
Running your first Deployment
This chapter assumes you have a running kind or minikube cluster, a kubectl context that points at it, and basic comfort reading kubectl get output. You will create a Deployment, see the objects it creates, and watch it recover from failures.
Pods and Deployments
A Pod is the smallest unit Kubernetes runs. It wraps one or more containers that share a network address. A bare Pod is not replaced if it is deleted or its node fails.
A Deployment is a controller that keeps a desired number of identical Pods running. It does this through a ReplicaSet, an object that counts Pods and creates or removes them to match the target. You normally manage the Deployment and let it manage the rest.
Create a Deployment
The quickest way is an imperative command. The next chapter replaces it with a manifest file.
kubectl create deployment web --image=nginx:1.27 --replicas=2
kubectl rollout status deployment/web
kubectl get deployment,replicaset,podsrollout status blocks until the new Pods are ready. The last command lists all three layers. The Pod names combine the Deployment name, a ReplicaSet hash, and a random suffix.
Watch it self-heal
Delete one Pod and list Pods again:
kubectl delete pod -l app=web --wait=false --field-selector=status.phase=Running | head -n 1
kubectl get podsThe simpler way is to copy a Pod name from kubectl get pods and run kubectl delete pod <pod-name>. Either way, the ReplicaSet notices only one Pod remains and starts a replacement within seconds. The Deployment is a promise about the desired state, and Pods are disposable.
Scale and update
kubectl scale deployment/web --replicas=3
kubectl set image deployment/web nginx=nginx:1.27-alpine
kubectl rollout status deployment/web
kubectl rollout history deployment/webChanging the image creates a second ReplicaSet. Kubernetes starts new Pods there and shrinks the old ReplicaSet step by step, so the app stays available. The container is named nginx because kubectl create deployment derives the name from the image. The nginx= prefix in set image refers to that container name.
If an update goes wrong, return to the previous ReplicaSet:
kubectl rollout undo deployment/webCommon errors
| Symptom in `kubectl get pods` | Likely cause |
|---|---|
| `ImagePullBackOff` or `ErrImagePull` | Wrong image name or tag, or a private or local-only image the cluster cannot pull |
| `CrashLoopBackOff` | The container starts and exits repeatedly. Check `kubectl logs`. |
| `Pending` | No node can fit the Pod, for example because of resource requests |
The later chapters on local images and debugging cover the first two errors in detail.
Exercise
You run kubectl delete pod <pod-name> on one Pod of a Deployment with 2 replicas. A few seconds later kubectl get pods shows 2 Pods again, one with a new name. Which object created the replacement, and why?
Show answer
The ReplicaSet created it. The ReplicaSet compares the number of running Pods with the desired count of 2, sees only 1, and starts a new Pod. The Deployment manages the ReplicaSet, so the desired count survives Pod deletion.