Chapter 4 of 47 · Foundations

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.

posts-deployment manages three posts Podsposts-deploymentreplicas: 3managesposts-podposts-podposts-pod

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:

The reconcile loopDesired state, 3 Pods, and current state, 2 Pods, are compared. They differ, so Kubernetes creates 1 Pod, then compares again until desired equals current. DESIRED STATE3 Pods (what you asked) CURRENT STATE2 Pods (what is running) Compare3 ≠ 2 Actcreate 1 Pod repeat until DESIRED = CURRENT (3 = 3), then keep watching

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.

posts-service in front of three PodsFrontendposts-servicesame name foreverPod 1Pod 2Pod 3
The Pods behind it can change at any time. The Service name does not.

The three to remember

Pod

Runs your container.

Pod
└── Container

Deployment

Manages Pods and keeps the desired number running.

Deployment
├── Pod
├── Pod
└── Pod

Service

Gives Pods a stable network endpoint and routes traffic to them.

Client
  ↓
Service
  ↓
Pods

One 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 application

Management

Kubernetes
  └── Deployment
        └── Pods
              └── Containers

Imperative 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.

The declarative way is how Kubernetes is used in practice. It is the same desired-state loop you watched above, written down in a file.

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:

  1. What is Kubernetes?
  2. Pod
  3. Deployment
  4. Replicas
  5. Service
  6. Labels & selectors
  7. YAML / declarative config
  8. kubectl
  9. Updates & rollbacks
  10. 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:

WordSimple meaning
ContainerYour running application
PodKubernetes' wrapper around your container(s)
DeploymentKeeps the right number of Pods running
ServiceStable network address for a group of Pods
KubernetesManages all of this

Quick check

A Pod behind posts-service crashes and is replaced with a new IP. What does the frontend need to change?
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.

You edit 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.