Deployment patterns and Kubernetes
A monolith is deployed a few times a year by hand. Dozens of services, each released every week, cannot be. This chapter shows how FTGO packages, runs and releases its services automatically, without downtime and without betting every release on all its customers.
key idea / coordinator service / good path in progress / watch out problem / failure neutral
In chapter 18 we made FTGO's services survive slow, failed and buggy neighbors with time-outs, retries, circuit breakers, bulkheads and redundancy. That left one problem open: surviving failure is not enough. The team must ship dozens of services, each with its own fixes and settings, to production safely and often.
How does FTGO get a new version of a service into production, many times a week, without hand work, downtime or a risky big bang? By the end you can choose a deployment pattern, explain what Kubernetes does for you, create infrastructure from code, and release a new version to a few users before everyone.
Release night does not scale
FTGO's monolith was released four times a year. On release night the operations team logged into each server, copied the new WAR file, edited config files by hand and restarted. Now there are a dozen services, each with several instances, and every team wants to release weekly. Release night lasts until dawn, the servers have drifted apart because each was fixed by hand, and nobody dares touch the one that "just works".
Servers that are named, hand-configured and nursed back to health when they get sick are often called pets. The industry has moved toward cattle: identical, throwaway machines created and replaced by automation. Each step in that history hid more of the machine.
Automate every step of a deployment, and run services on a production environment that does the routine work for you. Developers deploy through an API, not by logging into servers.
Whatever platform FTGO picks, the production environment must provide four things:
A management API to deploy and update services. Runtime management that keeps the right number of instances running. Monitoring that gathers the logs and metrics from chapter 17. Routing that sends each user request to a healthy instance.
Automation is what makes many services workable at all. In a side example, the online retailer Gilt had about 10 services a year after starting with microservices, and over 450 by 2014. Tooling that let developers create and deploy services themselves drove that growth.
Automation needs a unit to automate. "Copy a JAR to a server" is easy to script, but the scripts still put several services on shared machines, and that creates its own trouble.
Noisy neighbors: package each service in isolation
The first decision is what exactly gets deployed: the shape of the package.
To save machines, Kitchen Service and Order Service are deployed as JAR files on the same host. Kitchen's nightly report job has a memory leak. At 7 p.m. it eats the host's memory, and Order Service, which did nothing wrong, slows to a crawl in the dinner rush. The ops team cannot even tell which service is using the CPU.
Run each service instance in its own isolated environment, a virtual machine or a container, with its own CPU and memory limits and its own filesystem. A leak in Kitchen Service then hurts only Kitchen Service.
There are four deployment patterns. Each step down the table hides more of the machine from you:
| Pattern | Benefits | Drawbacks |
|---|---|---|
| Language package (JAR/WAR) | Fast deploy, shares resources | No isolation, no resource limits, ops must know the runtime |
| Virtual machine (AMI) | Stack encapsulated, isolated, mature cloud | Heavy, minutes to build and boot, patch the OS |
| Container (Docker) | Encapsulated, isolated, limited, starts fast | You patch images and run the platform |
| Serverless (Lambda) | No servers, elastic, pay per request | Cold starts, limited to short functions |
A virtual machine image, such as an Amazon Machine Image (AMI), packages the service with its whole operating system. Isolation is strong, but each instance carries a full OS and takes minutes to build and boot. A container packages the service and its libraries but shares the host's kernel, so it builds in seconds and starts fast, while still getting its own IP address, filesystem and CPU and memory limits.
FTGO chooses containers with Docker. A Dockerfile names a base image, copies in the service's JAR and sets the command to start it. docker build turns it into an image, docker push stores the image in a registry, and docker run starts a container from it on any machine.
The fourth pattern, serverless, hides the machine completely. It gets its own chapter next.
FTGO must now keep its base images patched and run a container platform. And containers on their own are just processes on machines. With forty of them spread over eight hosts, someone still has to decide where each one runs and restart it when it dies.
Who restarts the containers at 2 a.m.? Kubernetes
Docker runs one container on one machine. FTGO needs something that runs many containers across many machines.
At 2 a.m. one of the eight hosts fails. The three Order Service containers and two Kitchen containers on it are gone. The on-call engineer has to find hosts with free memory, start replacement containers by hand, and update the load balancer, all while orders fail.
Declare what you want, such as "three instances of Order Service", and let an orchestrator decide where they run and keep that state true. When reality drifts, because an instance dies or a host fails, the platform notices and fixes it.
Kubernetes is the orchestration framework that won this space. It turns many machines into one pool. The master holds the control plane: the API server that kubectl talks to, etcd for the cluster state, the scheduler that picks a node for each pod, and the controller manager that keeps the desired state equal to the actual state. Each node runs the kubelet, which starts pods, and kube-proxy, which handles networking.
You describe the desired state as objects in YAML files and send them with kubectl apply. A pod is one or more containers sharing an IP address; usually one container per pod, sometimes with a helper container beside it (the sidecar pattern). A bare pod is not enough for production, because nothing replaces it when it dies. A deployment keeps the chosen number of pod replicas running, and a service gives them one stable DNS name and address. ConfigMaps and Secrets carry the externalized configuration from chapter 16.
Most of FTGO's services should be reachable only inside the cluster. The API Gateway is the exception: customers must reach it from outside. The service's type decides this:
Kubernetes is a large system with many concepts to learn. And the cluster itself, with its nodes, network and container registry, has to come from somewhere. If someone builds it by clicking in a cloud portal, FTGO is back to a hand-made pet, just a bigger one.
A cluster nobody can rebuild: infrastructure as code
Kubernetes keeps the services in their desired state. The infrastructure under it needs the same treatment.
An engineer created FTGO's production cluster and registry by clicking through the cloud provider's portal. The staging cluster was made a month later, by someone else, with slightly different settings. A bug appears only in production. Nobody can say how the two differ, and nobody could rebuild production if the account were lost.
Describe the infrastructure in machine-readable files kept in source control, and let a tool create it. Every environment is built from the same code, every change is reviewed and recorded, and the code is documentation that cannot go out of date.
Version control gives an audit trail and lets you recreate an environment as it was on a given date. That matters more than it sounds: one company had to spend months rebuilding an old system for a court case because nobody had recorded how it was set up.
The road from a developer's laptop to a running FTGO service has the same four steps every time:
Different environments need different tools along that road:
| Tool | Used for |
|---|---|
| Docker Compose | Run the whole app on your computer while developing and testing. It simulates the cluster without the weight. |
| Local Kubernetes (Docker Desktop) | Learn and rehearse a deployment: switch it on, apply, test, delete. |
| Managed Kubernetes (for example on Azure) | Production. A few clicks, or better Terraform code, give you a cluster with a container registry attached. |
Terraform is a language and a tool for describing cloud infrastructure. terraform init downloads the providers, terraform apply creates or updates resources to match the code, Terraform's state file tracks what exists, and terraform destroy tears it all down.
Grow the code in small steps: a resource group first, then the container registry, then the Kubernetes cluster, sharing settings through variables. Outputs expose values such as the registry address. Do not output passwords: they land in plain text in the state file.
The same idea reaches the cluster's contents through GitOps: the desired state of the cluster lives in a Git repository, and a tool in the cluster watches the repository and applies every change. Deploying becomes a reviewed commit.
Whether a CD workflow (for example in GitHub Actions) runs kubectl apply or a GitOps tool pulls the change, no one logs into a server anymore. Chapter 21 builds that pipeline.
The infrastructure code must be written, reviewed and kept in step with reality, and the Terraform state file must be stored safely because it can hold secrets. With deployment now easy, the team deploys Order Service v2 in the middle of lunch, and finds out what a deployment does to the requests in flight.
Deploy without an outage: rolling updates
Automated deployment makes releases frequent. Each release must therefore be invisible to customers.
The first automated deploy of Order Service v2 stops all v1 instances, then starts the v2 ones. For about ninety seconds no instance is ready, and every order placed in that window fails. The team goes back to deploying at 3 a.m., which defeats the point of deploying often.
Replace instances a few at a time. Start a new v2 pod, wait until its readiness probe says it can serve traffic, then remove one v1 pod, and repeat until all pods run v2. Consumers never see a gap, so teams can deploy during working hours.
The probes are the health checks from chapter 17. If something goes wrong, kubectl rollout undo rolls back to the previous version. A Kubernetes deployment does a rolling update by default.
Zero downtime is easier for some services than others. A service that receives work through a message broker, like Kitchen Service consuming order events, is easy: messages simply wait in the broker while instances are swapped. For synchronous services, use rolling updates or blue-green deployment (below). Adopting Kubernetes only to get zero-downtime deploys is overkill.
Old and new versions run side by side during the rollout, so they must be compatible with each other and with their consumers. And the readiness probe only proves that a pod started. A bug that shows up only under real traffic passes the probe, and the rollout spreads it to every pod within minutes.
Deploy is not release: progressive delivery
A rolling update replaces pods safely. It does not limit how many customers a bad version reaches.
Consumer Service v2 passes its tests and its readiness probe. But it fails for customers with more than one saved address. The rolling update finishes in four minutes, and from then on every such customer gets an error on checkout. The SLO alert fires, but by then every customer is on v2.
Deployment installs a version in production. Release lets users reach it. Deploy v2 with no traffic, test it, then shift real users to it a little at a time, and switch back at once if the metrics turn bad. This is progressive delivery: continuous delivery with fine control over the blast radius.
A service mesh such as Istio, from chapter 16, makes this routing possible without code changes. An Envoy proxy runs as a sidecar beside each service and follows routing rules. For Consumer Service, a rule first sends requests that carry a test-user header to v2, and everyone else to v1. Once the testers are happy, the weights move from 95/5 toward 0/100, and finally v1 is deleted.
Routing by weight is one technique. There are several, and they combine:
| Technique | Idea | Note |
|---|---|---|
| Blue-green | Run the new version beside the old, check it, then switch all traffic at once | Simple zero-downtime option; switching back is just as fast |
| Feature toggle | Deploy code hidden behind a switch, and turn it on later | Supports trunk-based work. Start with a config file. |
| Canary release | A small subset of users sees the new version, then widen | Tools such as Spinnaker can ramp up automatically based on error rates |
| Parallel run | Send each request to both versions and compare the results | Only the old version's result is used |
A parallel run suits risky rewrites, such as a new way to calculate delivery fees: the old version stays the source of truth, and the differences tell you whether the new one is right before any customer depends on it.
A mesh adds proxies and routing rules to run. Feature toggles pile up and must be removed once a feature is fully released. Parallel runs need care so that two versions do not both charge a card or both send a ticket to the kitchen. On top of all this sits a large platform that someone has to own.
Is Kubernetes worth it?
FTGO now has containers, a cluster, infrastructure as code and progressive delivery. Time to look at what running that platform costs.
Six FTGO teams share one cluster. The Delivery team's load test fills the nodes and the Order team's pods cannot be scheduled. Teams can see each other's resources more than they should. Two engineers now spend most of their time upgrading the cluster and installing tools such as Kafka into it, instead of working on food delivery.
Kubernetes deliberately left strong multitenancy (many teams safely sharing one cluster) out of its core, so organizations work around it:
For installing and running third-party software in the cluster, the ecosystem offers three tools:
| Tool | Role |
|---|---|
| Helm | A package manager: install third-party software such as Kafka |
| Operator | Manages the ongoing life cycle, such as upgrades. Can be used with Helm. |
| CRD | Custom resource definition: extend the Kubernetes API with your own object types |
Running your own cluster is a big job, so prefer a fully managed one. In the public cloud, first ask whether you need Kubernetes at all: a platform as a service (PaaS) or function as a service (FaaS) offering may hide the complexity completely. Expect Kubernetes to become a layer beneath higher-level platforms such as Knative.
Managed services and higher-level platforms trade control for convenience, and tie FTGO more closely to one vendor. For some services, such as Restaurant Service's rarely changing menus, even a managed cluster may be more machinery than the job needs.
Patterns in this chapter
| Pattern | Problem it solves | What it costs |
|---|---|---|
| Automated deployment | Hand-run release nights that cannot keep up with many services | A production platform and tooling to build |
| Language-specific package | Getting a service onto a server quickly | No isolation or resource limits between services |
| Service as a virtual machine | Noisy neighbors; the stack must travel with the service | Heavy images, slow to build and boot, OS to patch |
| Service as a container | Isolation with fast builds and starts | Images to patch and a container platform to run |
| Desired state management (Kubernetes) | Placing and restarting many containers on many machines by hand | A large system to learn and operate; weak multitenancy |
| Infrastructure as code and GitOps | Hand-made environments that drift and cannot be rebuilt | Code to maintain; state files that can hold secrets |
| Zero-downtime rolling update | Every deployment causes a short outage | Old and new versions must be compatible; bugs still reach every pod |
| Blue-green deployment | Switching versions safely, with a fast way back | Two full environments during the switch |
| Canary release with a service mesh | A bad version reaches every user at once | A mesh and routing rules to run |
| Feature toggle | Deployed code that is not ready for users | Toggle debt to clean up |
| Parallel run | Not knowing whether a rewrite gives the same answers | Double work, and side effects to guard |
Recap
- Many services cannot be deployed by hand, so the production environment must offer a management API, runtime management, monitoring and routing, all driven by automation.
- Each service instance should run isolated, and the four deployment patterns, language package, virtual machine, container and serverless, hide more of the machine at each step.
- Kubernetes keeps a declared desired state true across many machines, using pods, deployments, services, ConfigMaps and Secrets described in YAML.
- Infrastructure as code with Terraform, and GitOps for the cluster's contents, make every environment reproducible and every change reviewed.
- Rolling updates gated by readiness probes give zero-downtime deployment, and separating deployment from release with blue-green, feature toggles, canaries and parallel runs limits the blast radius of a bad version.
- Running Kubernetes is a large job, so prefer a managed cluster, and first ask whether a PaaS or FaaS platform would do.
Quick check
Show answer
Readiness only shows that the pod started. A bug that appears under real traffic would still spread to every pod. Istio lets you send test users to v2 first, then shift real traffic slowly and revert at once.
Show answer
Order Service slows down or fails because it shares the host with a noisy neighbor, and it is hard to see which service is to blame. Isolated execution fixes it: deploy each instance as a container (or VM) with its own CPU and memory limits, so Kitchen's leak only hurts Kitchen.
Show answer
Deploying installs a version in production; releasing lets users reach it. Keeping them apart lets the team test a new version in production with no or few users, widen it gradually, and switch back quickly if it misbehaves.
FTGO can now ship any service safely, many times a week. But it still owns a cluster, nodes, images and a mesh, and someone must patch and upgrade them all. What if FTGO did not have to manage servers at all?