Delete the Pod you created in Chapter 4:
Output:
Now check if it came back:
Output:
It didn't come back. That's the problem with bare Pods—they're mortal. Delete one, it's gone forever. Your application is down until you manually recreate it.
Deployments fix this. A Deployment is a manager that continuously monitors your Pods and ensures the desired count always exists. Delete a Pod? The Deployment creates a replacement. Node crashes? Pods get rescheduled elsewhere. This chapter teaches you to declare WHAT you want (3 replicas of your agent), and let Kubernetes handle HOW to maintain it.
Direct Pod deployment is like hiring workers without a manager. You manage each worker individually:
This manual approach doesn't scale.
Recall from Chapter 4 that Pods are the smallest deployable unit in Kubernetes. But Pods have a critical limitation: they are designed to be ephemeral (temporary).
A Pod can be deleted, evicted, or crash at any time:
If you deploy a Pod directly with kubectl run hello --image=nginx, and the Pod crashes, Kubernetes does NOT automatically create a new Pod. That container is gone. Your service is down.
A Deployment is a manager that guarantees a desired state:
The manager runs continuously, observing reality and fixing mismatches. This is Kubernetes' declarative model in action.
Deployments don't directly manage Pods. They use an intermediate abstraction called a ReplicaSet.
The hierarchy allows different responsibilities:
Let's create a Deployment manifest for a simple nginx service.
Output: (This is just the manifest structure; we'll apply it next)
Important: The labels in template.metadata.labels MUST match the selector in selector.matchLabels. If they don't match, Kubernetes can't find the Pods, and the Deployment creates an infinite number of new Pods trying to satisfy the replicas requirement.
Save the manifest above as deployment.yaml and deploy it:
Output:
Check the Deployment status:
Output:
What each column means:
Check the Pods created by this Deployment:
Output:
Notice the Pod names: hello-deployment-[ReplicaSet-hash]-[random-id]. The ReplicaSet is embedded in the name.
Check the ReplicaSet:
Output:
The ReplicaSet hello-deployment-7d4b8c9f5 is responsible for ensuring 3 Pods exist.
Now demonstrate Kubernetes' self-healing. Intentionally delete one Pod:
Output:
Wait a few seconds, then check Pods again:
Output:
What happened:
This is self-healing: Kubernetes automatically recovers from Pod failures without human intervention.
Increase the replica count from 3 to 5:
Output:
Check the result:
Output:
Check Pods:
Output:
Two new Pods (timestamps: 10 seconds ago) were created to reach 5 replicas.
Scale back down:
Output:
Kubernetes will terminate 2 Pods gracefully.
Your application needs to upgrade from nginx 1.24 to nginx 1.25. Update the image:
Output:
Watch the rollout:
Output:
What Kubernetes did:
Check the ReplicaSets:
Output:
The old ReplicaSet has 0 desired replicas (no Pods). The new ReplicaSet has 3 Pods running.
If the update introduced a bug and the new version doesn't work, rollback immediately:
Output:
Kubernetes:
Check status:
Output:
View the history of updates:
Output:
Each revision is a ReplicaSet. You can even rollback to a specific revision:
Reflect on what happened:
You never said HOW. You declared WHAT you wanted, and Kubernetes' controllers continuously worked to achieve that state.
This is the power of Kubernetes: declare your desired state, and Kubernetes keeps you there.
Open a terminal and work through these scenarios with an AI assistant's help:
Your task: Create a Deployment manifest for a Python Flask application that:
Ask AI: "Create a Deployment manifest for a Flask app with these requirements: [list your requirements]"
Review AI's response:
Tell AI your constraints: "The image needs to be pulled with a secret called my-registry-secret because it's in a private registry."
Ask AI: "Update the manifest to handle the private registry secret."
Reflection:
Your task: You deployed a Deployment, but kubectl get deployments shows 0/3 READY. The manifest looks correct, but Pods aren't running.
Ask AI: "I deployed a Deployment for my app, but the READY status shows 0/3. What could be wrong?"
AI might suggest checking:
Ask: "Show me the exact commands I should run to diagnose this."
Reflection:
Your task: You need to update your Deployment from version 1.0 to 2.0, but the service cannot have downtime. You're unsure about the rolling update strategy.
Ask AI: "Explain the rolling update process. I have 3 replicas—walk me through exactly what happens when I update the image."
AI should explain:
Ask: "What happens if the new version has a bug and Pods crash? How do I recover?"
Reflection:
You built a kubernetes-deployment skill in Chapter 0. Test and improve it based on what you learned.
Ask yourself:
If you found gaps: