Deployments, Services and desired state
Pods run your containers, but they are fragile and their IPs change. Two more objects fix that: a Deployment keeps the right number of Pods alive, and a Service gives them one stable address. Together with Pods, these three are the foundation of Kubernetes.
Deployment: manager of Pods
Instead of creating Pods yourself, you create a Deployment and tell it: "I want 3 Pods." The Deployment creates them and keeps that number running.
Compare this with the last chapter, where nobody replaced the crashed Pod. Now crash one, and try + and − to change how many you want:
Desired state vs current state
What you just watched is the heart of Kubernetes. It keeps comparing two things:
You describe the result you want; Kubernetes works out the steps. This is called declarative configuration, and it comes back below.
Service: a stable address
Now you have posts-pod-1, posts-pod-2 and posts-pod-3. Which one should your frontend call? Not http://10.0.0.15: Pods are temporary, and you just saw a replacement Pod get a new IP.
A Service is a stable network endpoint in front of the Pods. Your frontend calls http://posts-service, and the Service passes each request to one of the Pods that is running right now.
The three to remember
Pod
Runs your container.
Pod
└── ContainerDeployment
Manages Pods and keeps the desired number running.
Deployment
├── Pod
├── Pod
└── PodService
Gives Pods a stable network endpoint and routes traffic to them.
Client
↓
Service
↓
PodsOne complete picture
Here is everything together. The Deployment manages the Pods from above; traffic flows from the client, through the Service, into a Pod and its container. Send a few requests, crash a Pod, change the count, and keep sending:
The client never notices Pods coming and going. It only ever knows posts-service. Two separate jobs:
Traffic flows
Client
↓
Service
↓
Pod
↓
Container
↓
Your applicationManagement
Kubernetes
└── Deployment
└── Pods
└── ContainersImperative vs declarative
There are two ways to tell Kubernetes what to do.
You say what action to perform.
$ kubectl create deployment posts --image=micro-posts
"Create this deployment." Quick for experiments, but the command is gone once you have run it. Nobody can see later what you intended, and running it twice fails because it already exists.
You describe the final state you want in a file:
# deployment.yaml (shortened)
kind: Deployment
metadata:
name: posts
spec:
replicas: 3
...
image: micro-posts
$ kubectl apply -f deployment.yaml
"This is the state I want. Kubernetes, make reality match it." Run it once or ten times; the result is the same. Change replicas: 3 to 5, apply again, and Kubernetes adds 2 Pods. The file lives in Git, so everyone can see what is supposed to be running.
What to learn, in order
Kubernetes has a lot of topics. Do not try to learn them all at once. This order builds each idea on the last one:
- What is Kubernetes?
- Pod
- Deployment
- Replicas
- Service
- Labels & selectors
- YAML / declarative config
- kubectl
- Updates & rollbacks
- Liveness / readiness probes
Green ones you have met in this course so far, at least at picture level. After those come ConfigMaps, Secrets, Volumes, Namespaces, Ingress, StatefulSets, DaemonSets, Jobs, autoscaling and Kubernetes networking.
The five words
Do not memorise commands yet. Just understand these:
| Word | Simple meaning |
|---|---|
| Container | Your running application |
| Pod | Kubernetes' wrapper around your container(s) |
| Deployment | Keeps the right number of Pods running |
| Service | Stable network address for a group of Pods |
| Kubernetes | Manages all of this |
Quick check
Show answer
Nothing. The Deployment replaced the Pod, and the Service started sending traffic to the new one. The frontend only ever calls posts-service.
replicas: 3 to replicas: 2 and run kubectl apply. What happens?Show answer
Desired is now 2 and current is 3, so Kubernetes removes one Pod. You never said "delete a Pod"; you described the new state.
Next: Pods in depth, over four short chapters.
Deployments and Services: common questions
What is the difference between a Deployment and a Service?
A Deployment manages Pods and keeps the right number running. A Service gives those Pods one stable network address and routes traffic to them.
What is declarative configuration in Kubernetes?
You describe the state you want in a file, such as replicas: 3, and run kubectl apply. Kubernetes keeps comparing that desired state with what is running and fixes the difference.