Ch.74: Kubernetes System Design: How an App Starts Serving

Outline

Transcript

0:00 Kubernetes runs application containers across machines. We'll explain the cluster's core, from an operator's saved deployment goal to a copy answering a request. Take this report-preview API. The operator requests three copies, but only two serve requests. The deployment request succeeded, but usable capacity is missing. How do we find the failure? That success message is almost asking to be misread. It only confirms the request was saved. Exactly. Saving the request, finding a machine and answering a customer are different accomplishments. Now look at the design we're heading toward.

0:33 The upper group coordinates work, the machines below run it, and the separate arrow follows a caller. Are those three healthy copies our starting point? That's the destination, including a worker we'll add. We'll return to this map once the 3rd copy can serve. So, our functional requirements: specify the image, the packaged application, and how many copies we want, then start the missing copies on suitable workers. Callers need a stable route to ready copies. We need progress reports and replacements after a loss.

1:03 And I want to close the terminal without abandoning that 3rd copy. Then the saved goal must outlive that connection. And our non-functional requirements are keeping that goal through restarts and fitting each copy's CPU and memory on one worker. Also keep serving separate from coordination, and allow for recovery delays. Our copies are stateless, with durable report data elsewhere. Couldn't we just make the deployment command wait until all three are usable? We can wait. But we'd still need to distinguish missing capacity from a stalled image download. Fair. Then what did the cluster actually save?

1:38 First, the operator sends a Deployment with the image and copy count to the API server, the management entry point. Etcd stores that control state. So etcd holds the description, not the running application. What turns that count into work for a machine? A replica set records the target for this application version. Its controller acts on that target by maintaining Pods. Each Pod is assigned to one worker; ours holds one preview container. So the saved record says three, the controller sees two Pods, and creates the missing Pod record through the API.

2:12 That record still has no worker. Right. That controller creates work; the scheduler places it, and a worker's node agent arranges startup. They aren't one giant deployment transaction. And if the controller restarts, it can read the target and Pod records again. The Kubernetes docs favor simple, independent controllers because they can fail too. I've linked that explanation in the description. The 3rd record exists. Now we need a worker that can fit it. Next, consider the new Pod. It requests two CPU units and 4 gibibytes of memory, the binary memory units marked Gi.

2:46 These are fictional exercise values. Does the table have room? Let's unpack the table: each worker has unreserved allocatable capacity, after existing requests. Allocatable means what remains available to workloads after the machine's own reserved needs. Both requested resources must fit together. Worker A has the two CPU units, but only one Gi. B has six Gi of memory, but only one CPU. C fails both tests. So no worker fits. Yet the columns sum to four CPU and eight Gi. Those cluster totals look comfortably larger than the Pod if you ignore their locations.

3:21 Hold on. Adding the columns counted memory on B alongside CPU on A. Our one Pod can't use that combination across separate machines. Exactly. It's like checking a doorway's width and height. Extra width at another doorway doesn't make this opening taller. Each candidate worker must satisfy the complete request. Okay, we need per-worker evidence. How does the scheduler use it? First it filters for feasible workers, then ranks the eligible ones and records an assignment. Here the feasible set is empty.

3:52 There isn't an assignment to make. But my first thought is to lower the Pod's request. The CPU usage chart is quiet. Wouldn't the scheduler then see the space we're apparently wasting? Say you lower it: the scheduler sees a different reservation. But momentary consumption and requested capacity answer different questions. Existing requests still count even while those applications are temporarily quiet. So lowering it is a workload decision. If two CPU is excessive, change it with evidence. If it reflects the application's needs, a smaller number just hides the competition.

4:25 Right. Keep the request for this exercise. Have the operator add suitable worker D: four CPU, eight Gi allocatable, instead of disguising what the application needs. Now D fits the full Pod. After reserving its resources, D has two CPU and four Gi unreserved. We added real eligible capacity; the scheduler didn't create a machine. With the assignment recorded, how does D find out, and who actually starts our application? D's node agent, the kubelet, sees its assigned Pod specifications through the API.

4:54 It coordinates startup through the container runtime, the software that runs containers. Recording the assignment didn't perform that work. Suppose D can't reach the image registry, the store of packaged applications. It has the assignment, but can't download this image. We're still short a serving copy. Yes. Pending means the Pod was accepted, but its containers aren't all set up to run yet. That includes waiting for placement or downloading an image. Check whether it's assigned before blaming the scheduler; here we'd inspect D's image retrieval and setup.

5:26 Right. Even once the process starts, it can still be loading what it needs to answer requests. We shouldn't send a customer there yet. That unfinished initialization raises a traffic question. A readiness check asks whether this copy should receive traffic. Our new process can be Running while its Ready condition is false. Suppose we attach a liveness check to initialization. Would restarting the application make it usable sooner? Only if restarting addresses the problem. Liveness answers whether to restart the container.

5:57 Readiness controls traffic eligibility. A startup check can protect slow initialization before liveness begins; a restart can discard that initialization progress. Oh, right. We could keep killing a healthy startup for being unfinished. That's a very effective way to keep it unfinished. So the initialization check should keep this copy out of traffic, without treating that wait as proof of a stuck process. Yes, let it finish. Once initialization completes, its readiness check succeeds. The copy becomes an eligible destination after the associated updates propagate.

6:31 A state change and every worker observing it aren't simultaneous. Let's follow the caller now. Another application inside our cluster uses DNS, the name lookup system, to find the preview Service's stable address. How does that address find the new copy? The Service selects Pods labeled app equals preview. A control-plane controller updates its endpoint slices, the records of those addresses and their readiness. Our new address becomes eligible for ordinary traffic when the copy is ready. So membership changes while the caller keeps the same name.

7:02 Does the request go back through the API server to choose a copy? No. The worker networking forwards it using the destination information. The selected Pod reads report data, builds the preview and returns the result. The API server coordinates membership changes; it isn't a hop in each preview request. Oh, good. The management path doesn't have to answer every preview request. But a newly ready copy still needs its destination update to reach the forwarding rules. Exactly. Those updates take time, and an existing connection doesn't instantly jump to another Pod because readiness changed. Keep that delay in mind when we lose a worker.

7:40 Now consider a failure case: worker C becomes unreachable. The remaining application copies are on A and D. For a while, callers can encounter C's old destination and see failures. The cluster needs evidence of the loss before responding. What happens when the node stops reporting? Do we just move that Pod over to another machine? No, recovery creates a replacement with a new Pod identity, once failure detection and the configured removal and replacement rules allow it. Kubernetes doesn't transport C's running process and memory to D.

8:12 And unreachable doesn't prove C's process stopped. A network partition can prevent us hearing from it while it continues running. Removing a record wouldn't prove its external activity ended either. Right. We're replacing preview capacity, with the report data outside the copy. Yes. Keeping the data elsewhere doesn't prevent requests reaching a stale destination. Failed readiness and node-state changes update serving destinations as observations propagate. Some requests can fail during that interval.

8:41 The surviving copies help only while they're reachable and have capacity. And our replacement still needs a place to run. Losing C didn't remove the placement rules we started with. Now check the surviving budgets for that replacement. A still lacks memory, B lacks CPU, and D has exactly the replacement's two CPU and four Gi. So D passes the resource test. But D already has a preview Pod. If our placement policy requires one preview copy per worker, D is forbidden. With a preference, D stays eligible.

9:13 The scheduler favors separating copies when it ranks the workers that fit. Here's the operator's objection: I need the 3rd copy now, and a new machine costs money. Why leave a fitting machine unused to preserve separation? I think temporary sharing is acceptable for this preview service, with that risk explicit. Two copies on D restore the count, but D's next failure takes both away. I'd keep separation required if that shared loss were unacceptable. Okay, that changes the outcome. A hard placement rule rejects D; a soft preference influences its ranking.

9:49 We choose the preference here, rather than promising independent failures we didn't pay for. A further suitable worker can buy that separation back. The two policy cards on screen are different operational choices, not a strict rule and a cheaper way to get the same guarantee. So we separated serving from coordination. Does that mean an unavailable control plane is harmless as long as our existing Pods are healthy? Harmless is too strong. Installed networking rules and running application processes may keep serving, but new assignments, replacements and configuration changes still need working coordination. The three etcd members keep copies of that saved cluster state.

10:26 A majority must agree before updates can be saved. Here that's two; with only one reachable, new writes stop. And surviving traffic isn't evidence that replacements still work. During an outage, I'd check those capabilities separately. The report API might look fine right up until another worker disappears. The diagnostic ladder gives us better questions too. Saved but unassigned: inspect placement. Assigned but not running: inspect the worker and image. Running but not ready: inspect initialization and the check.

10:55 Ready but unreachable sends us toward selection, routing and the network. Those are investigation starting points, not complete diagnoses. The status tells us which evidence to seek, rather than which random component to restart. Finally, on the complete map, we can explain the opening mismatch. The goal was saved, but this copy had nowhere to fit. Worker D matched our resource request. The kubelet and runtime started the process, readiness made it eligible, and the Service's destinations caught up.

11:24 Our caller got its preview from a ready copy through the same Service name. Its request skipped the API server. Recovery needs working coordination, suitable capacity and an acceptable placement rule. If we share D, both copies depend on that worker staying available. Thanks for listening to Learning Podcasts.