Distributed systems / Kubernetes pods, deployments and services

Kubernetes pods, deployments and services

Three building blocks of Kubernetes, in the order you need them: the pod that runs your container, the deployment that keeps pods alive, the service that gives them one address.

Intermediate7 steps~7 min
Pod — one running copy of the app
Label — a key=value tag on a pod
Deployment — says how many pods, of which version
Service — one stable address in front of the pods
// THE SHORT VERSION

In 30 seconds

Kubernetes runs containers for you across a group of machines. Its smallest unit is the pod: one running copy of your app, with its own address. Pods are disposable.

A deployment says how many copies you want and of which version. It replaces pods that are lost, and swaps old for new gradually when you release.

A service gives the changing set of pods one stable name and address, and spreads requests across them. Labels on pods, matched by selectors, tie the three together.

Kubernetes pods, deployments and services, step by step

STEPS01 / 07

Simplified: a deployment manages its pods through an intermediate object, the ReplicaSet, left out here. The service’s split of traffic is roughly even, not exact. Addresses are examples.

STEP 01 / 07 A pod wraps a container
THE MATH
pod = 1 or more containers + one IP address
  1. Step 1: A pod wraps a container. A container is an application packaged with everything it needs to run. Kubernetes never runs a bare container: it wraps it in a pod, the smallest unit it can start, stop or move. A pod holds one container, sometimes with a helper beside it, and everything inside shares one network address.
  2. Step 2: Pods are disposable. Three copies of the app run as three pods. When the machine under one of them fails, that pod is gone. Kubernetes does not repair or revive a pod, and a replacement would get a new name and a new address. That leaves two problems: who starts the replacement, and how do callers find it.
  3. Step 3: A deployment keeps count. A deployment is a standing order: keep three replicas, that is three identical pods, of this image running. A controller compares that wish with reality all the time. When it counts two, it starts one more pod from the template in the deployment, with a fresh name and address. Nobody has to be awake for it.
  4. Step 4: Labels and selectors. How does the deployment know which pods are its own? Every pod carries labels, small key=value tags. A selector is a query over those tags: app=web matches every pod labelled app=web and nothing else. Names and addresses may change; membership is decided by the label alone.
  5. Step 5: A service: one stable address. A service answers the second question. It gets a name and a virtual IP address that stay the same for as long as the service exists, and it uses a selector to find the pods behind it. Callers ask for web on port 80 and never need to learn a pod address.
  6. Step 6: The service spreads the load. Behind the service Kubernetes keeps a list of the matching pods that are ready to take traffic, and updates it as pods appear and vanish. Each new connection to the service is handed to one pod from that list, so six requests land roughly two on each. The service is a small built-in load balancer.
  7. Step 7: A rolling update. To ship a new version you change one line of the deployment: the image. It then replaces pods gradually. It starts one pod of the new version, waits until that pod reports ready, removes one old pod, and repeats. The service only sends traffic to ready pods, so three are serving at every moment.
VALUES SO FAR
pod IP10.1.0.4changes
labelapp=webtag
replicas····desired
image····desired
selector····rule
service····stable
ready····actual
image····desired

Spotted a mistake on this page?