Orchestration — study guide
The concept's fragments, read in order.
For your capstone A personal cloud you can destroy and rebuild
Orchestration is what makes destroying a homelab machine routine. You declare which services run, how many copies and what each reserves, in files in your repository, and controllers keep chasing that declaration while you wipe machines out from under it. On a few modest boxes, a lightweight distribution such as k3s is the same system in a smaller package.
Say your notes app's deployment declares replicas: 2, each pod requesting 256Mi, and you pull the plug on node3. Within a minute it is marked not ready; five minutes later its pod is evicted, the controller counts one running against two declared, and the scheduler places a replacement on node1. If node1 has no unreserved room for that request, the pod sits in Pending, and two copies is a wish, not a fact.
Saying what should be running
One container on one machine is easy to look after: you start it with podman run, and if it dies you notice and start it again. Ten services, each running as several copies, spread over a handful of machines, is a different job. Something has to decide which machine each copy runs on, notice when one crashes or a whole machine goes dark, start replacements, and keep a stable address in front of copies that keep changing. Doing that by hand means someone watching all day and getting it right at three in the morning.
An orchestrator takes that job. You hand it a declaration of what should be running, such as three copies of this image, each reserving half a CPU core and 512 MiB of memory, reachable under this name, and it works continuously to make the cluster match. The idea is the one infrastructure as code is built on, desired state described in files, with one change: the orchestrator does not wait for you to run an apply. It checks and corrects all the time, so a crash at three in the morning gets handled by the system, not by whoever is on call.
Kubernetes is the famous orchestrator, an open-source project, and it is the worked example throughout this concept: its control loop, its scheduler, how it heals, the few objects you need to read a deployment, and the health checks that decide where traffic goes. One clarification, because the word gets reused: this is cluster-level orchestration of machines and containers. Coordinating the work of AI agents, with queues and retries, is a different concept that happens to share the name, and it does not require any of this.
Desired state, actual state, and the loop between them
Everything an orchestrator does comes back to one pattern, the control loop. A controller holds two things side by side: the desired state, written in an object's specification, and the actual state, what is really running in the cluster right now. It compares them, takes whatever action shrinks the difference, and then does it again, without end like cruise control in a car: you set the speed once, and it keeps easing the throttle up and down as hills come and go, without being told about each hill. If you declare three copies and two are running, the controller creates one. If four are running, it removes one. If three are running, it does nothing, and checks again.
This is reconciliation, the same compare-then-act move infrastructure as code makes when you apply a configuration, but it does not wait to be asked. A configuration tool reconciles when someone runs it; an orchestrator's controllers are always running, watching for changes and correcting as they happen. Drift that infrastructure as code would only catch at the next apply gets corrected here without anyone running anything, whether the cause was a crash, a lost machine or someone deleting the wrong thing.
Kubernetes is built as many small controllers rather than one big program. One keeps the right number of copies of each workload; another watches the machines, called nodes, and reacts when one stops reporting; the scheduler is a controller of its own, whose only job is giving unplaced work a node. The desired state they all read lives behind one API, stored in a database called etcd, and an agent on every node, the kubelet, makes sure the containers assigned to its node are actually running.
The payoff of this design is that the system does not need to know how it got into a bad state. It only needs to know where it is and where it should be. A cluster that has just lost a node, half-finished an update and had its count edited by hand is, to a controller, one more starting point to converge from.
Pods, deployments, and services
Kubernetes has many object types, but three are enough to make sense of a typical one-app setup. The smallest is the pod: one or more containers that are always scheduled together onto the same node and share one network address, so they can reach each other on localhost. Often a pod holds just one container. The image inside is the same kind of image you build and push with podman; the nodes pull it from a registry with their own container runtime, but the bytes are identical. Pods are treated as disposable. When one fails it is not repaired, it is replaced, and the replacement comes back with a new name and usually a new address.
A deployment is the object that keeps a set of identical pods running. It holds a pod template, the description every copy is stamped from, and a desired count, written as replicas: 3. Under the hood it hands that count to a smaller controller, a ReplicaSet, whose loop compares the matching pods it sees against the number and creates or deletes pods until they agree. Delete one of the three by hand and a replacement appears moments later, because nothing in the declaration changed.
Since pods come and go with new addresses, nothing should be configured to talk to one directly. A service is the stable front: it has its own virtual address and a DNS name that stay fixed like a business's main phone number, which stays the same while the people answering it change from shift to shift, and it finds its pods by label rather than by name. Pods carry labels such as app: web, the service selects every pod with that label, and Kubernetes keeps the list of matching pods up to date as they are replaced. Clients call the service; the service spreads requests across whichever matching pods are currently ready.
Labels are the glue between the three. The deployment stamps its label onto every pod it creates, and the service selects on the same label, so the two never need to know each other's names. Get the label wrong on either side and the pods run perfectly well while the service routes to nothing.
Placing work by what it asks for
When a deployment creates a pod, the pod exists but has nowhere to run. The scheduler's job is to give it a node. It works in two passes. First it filters: every node that cannot satisfy the pod's requirements is ruled out, whether for lack of room, for a rule that keeps this pod off that kind of machine, or for a volume the node cannot reach. Then it scores the nodes that remain, using preferences such as spreading copies apart or balancing how much of each node is reserved, and binds the pod to the highest-scoring one. From then on the kubelet on that node is responsible for running it.
The room check is where resource requests come in. Each container can state how much CPU and memory it needs, for example cpu: 500m, half a core, and memory: 512Mi, 512 mebibytes. The scheduler adds up what the pods already on a node have requested, subtracts that from what the node offers, and asks whether the new pod's request fits in what is left like a restaurant host seating parties by the size of their booking, filling tables by what was reserved rather than by how much each party will actually eat. It does not look at how busy the node actually is at that moment. A node whose pods requested nearly everything counts as full even if they are idle, and a node with plenty left to reserve is a candidate even if its existing pods are using more than they asked for.
If no node passes the filter, and the pod is not configured with a high enough priority to have lower-priority pods evicted to make room, it is not placed anywhere. It stays in the Pending state, and the scheduler tries again as conditions change: a pod finishes, a node joins, a request is lowered. A pending pod is often the first visible sign that a cluster is out of reserved capacity, or that a request was written far larger than anything a node can offer.
This is why requests are worth writing honestly. They are all the scheduler knows about how much a pod needs, so a request that is far too low packs more work onto a node than it can carry, and one that is far too high leaves capacity reserved and unused.
Requests reserve, limits cap
A container in Kubernetes can carry two numbers for each resource, and they do different jobs. The request is a reservation: the amount the scheduler counts when it decides whether the pod fits on a node. The limit is a ceiling: the most the container is allowed to use once it is running. A container might request 500m of CPU, half a core, and 512Mi of memory, with limits of 1000m, one whole core, and 1Gi. It is placed only on a node with room for the first pair, and held to the second.
The limits are not enforced by Kubernetes watching and stepping in. The kubelet and the container runtime write them into the container's cgroups, and the Linux kernel enforces them, the same walls inside one kernel that resource isolation is built on. What the kernel does at the ceiling depends on the resource, because CPU time and memory behave differently when you run out.
CPU can be rationed. A container that tries to use more than its CPU limit is throttled: the kernel lets it run only up to its quota in each period (cpu.max), and it keeps running, only slower. The request is not only for the scheduler either: the kubelet turns the CPU request into the container's cpu.weight, so when the node is busy each container still gets about the share it requested. Memory cannot be rationed that way, because a program that needs more memory cannot just wait for it. When a container's memory use reaches its limit and the kernel cannot reclaim enough, the kernel's out-of-memory (OOM) killer ends it (on cgroup v2 nodes Kubernetes has the whole container killed together, using the memory.oom.group setting described in resource isolation), the container is reported as OOMKilled, and the restart policy starts it again. A slow service and a service that restarts every few minutes can be the same mistake, one limit set too low, depending on which resource it was.
So the two numbers answer two questions. Requests are what you need to run well, and the scheduler takes them at their word. Limits are where you want the kernel to stop a container that is misbehaving, so one runaway cannot starve its neighbors on the node. Set them from measurements, the way any capacity number should be set, not from round numbers that looked reasonable.
Restart, then reschedule
Self-healing is the control loop applied to failure, and it works at two levels. The first is the container. When a container exits or is killed, the kubelet on its node starts it again in the same pod, because the default restart policy is Always. If it keeps crashing, the kubelet waits a little longer before each new attempt, up to a cap, and the pod shows the status CrashLoopBackOff. That status is the loop telling you it is still trying and something is still wrong.
The second level is the node. When a node stops reporting, Kubernetes marks it not ready or unreachable once a grace period of under a minute has passed, and by default its pods are then allowed to stay bound to it for five more minutes in case it comes back. After that they are evicted. The deployment's controller then sees fewer running pods than the declared count and creates replacements, and the scheduler places them on the nodes that are left. They are new pods with new names and new addresses, not the old ones moved, which is exactly why clients reach them through a service that follows labels rather than through any pod's address.
Two things follow. Losing a node heals in minutes, not seconds, so the surviving nodes have to carry the load in the meantime, which is a capacity question as much as an orchestration one. And the surviving nodes need unreserved room for the replacements; if they have none, the new pods sit in Pending and the declared count is a wish rather than a fact.
Self-healing also has a clear edge. A restart fixes a process that crashed or hung; it does not fix a bug that crashes the process every time, and it does not bring back anything the old pod kept only on its node's local disk or in memory. The orchestrator restores the running copies you declared. Keeping data safe across those replacements is a separate design decision.
Alive is not the same as ready
A container whose process is running is not necessarily doing its job. It might be deadlocked, still loading data at startup, or unable to reach something it depends on. Kubernetes lets each container declare probes, small checks the kubelet runs on a schedule, such as an HTTP request to a health endpoint or an attempt to open a port. By default a probe runs every 10 seconds, waits 1 second for an answer, and acts only after 3 failures in a row. There are two probes that matter most, and they ask different questions.
A liveness probe asks whether the container is stuck in a way a restart would fix. When it fails enough times, the kubelet kills the container and starts it again. A readiness probe asks whether the container can serve requests right now like a shop whose staff have arrived but have not yet turned the sign to open: everyone inside is fine, but customers are only sent in once the sign says open. When it fails, nothing is restarted: the pod is marked not ready and taken out of every service that selects it, so it stops receiving traffic, and the probe keeps running until it passes again and the pod is put back.
Readiness is the probe that protects users. A new pod can be running for many seconds before the app inside can answer a request: loading configuration, opening database connections, warming a cache. Without a readiness check the service would send it traffic the moment it started, and those requests would fail. The same gate is what makes a rolling update safe, since each new copy takes traffic only once it reports ready, and it lets a pod that is temporarily overwhelmed step out of the rotation without being killed. For apps that take a long time to start, a third kind, the startup probe, holds off the other two until startup has finished.
The trap is a liveness probe that checks a dependency. If every pod's liveness check includes the shared database, a short database outage fails every check at once, the kubelet restarts every pod, and the restarts add load just as the database recovers. Restarting your app cannot fix someone else's outage. Keep liveness about the process itself, and leave the question of whether this pod can serve a request right now to readiness, which takes a pod out of rotation without killing it. Be careful with readiness too: if every pod's readiness depends on the same shared service, an outage there takes every pod out of rotation at once, and clients get connection failures instead of your app's own error responses. Check what this pod itself needs to be able to answer.
Change the file, not the cluster
In an orchestrated system, almost every change is an edit to desired state. The deployment, its service and their settings live in manifest files, plain YAML that sits in version control beside the code it runs, and kubectl apply -f hands a file to the cluster, which compares it with the live objects and changes what differs. It is the declarative half of infrastructure as code, with the same benefits: every change is a commit someone can review, and the history says what was running and when.
Ship a new version by changing the image tag in the deployment's template and applying it. The deployment's controller sees a new template and replaces the pods in a rolling update, a few at a time, by default allowing up to 25 percent extra pods above the desired count and up to 25 percent unavailable while it works. How to choose those allowances, when to set the unavailable one to zero, and the other ways to ship a change belong to release strategies. Scale by changing replicas and applying again; the same loop that replaces failed pods creates or removes copies until the count matches.
The count can also be handed to another controller. Kubernetes' horizontal autoscaler watches a metric such as average CPU use against a target and edits the replica count for you, adding copies quickly and removing them slowly. How it calculates the new count, and why it waits before scaling in, is capacity planning's territory. The point here is that the autoscaler is one more controller writing desired state, not a different mechanism.
The commands that change live objects directly, such as kubectl scale or kubectl edit, are handy for an experiment and a source of drift for anything you keep. Someone runs kubectl scale to five during a busy hour, the file still says three, and the next apply of that file puts it back to three, because the file sets that field. The Kubernetes documentation advises managing each object one way, so for anything that matters, change the file and apply it.
When an orchestrator is more than you need
An orchestrator earns its keep when there is real work for the control loop: several services, several copies of each, more than one machine, and failures you want handled without a person in the loop. Take most of that away and what is left is the cost. A Kubernetes cluster is itself a system to run. The control plane, its etcd database, the network layer that gives every pod an address and every service a name, and storage that follows pods between nodes all have to be set up, secured, monitored and understood when they break.
It also has to be kept current. The Kubernetes project releases a new minor version about three times a year and patches each one for roughly 14 months, so a cluster left on one version stops receiving fixes, security fixes included, a little over a year after that version came out. Upgrading a cluster is routine work, but it is work, and it recurs whether or not the app changed.
For one machine running a few services, lighter tools do the job. Podman can run containers as services under the system's service manager, systemd, which can restart them when they fail and start them at boot, with no control plane at all. It can also read Kubernetes-style pod YAML with podman kube play and run it on that one host, which keeps the declarative habit without a cluster, though it creates what the file describes once, with no control loop watching it afterwards, and by default only one copy of a Deployment. What this gives up is everything that needs more than one machine: scheduling across nodes, rescheduling off a dead one, and one service address spread over copies on different hosts.
When you do want real Kubernetes, the burden can be made smaller or handed off. Lightweight distributions such as k3s package a fully compliant Kubernetes as a single binary, a practical fit for a few modest machines (on a single server, k3s by default even swaps etcd for a simpler built-in database), and it is the same system, not an imitation: what you learn on it transfers. Cloud providers offer managed clusters where they run the control plane for you. Either way the honest test is the same. Adopt an orchestrator when the failures and the scale it handles are ones you actually have.
A system that keeps its own promises
Sooner or later a set of services outgrows the person looking after it. A few services spread over a couple of machines at home, or an app taken to production with several copies behind one address, both reach the point where something has to restart what crashed, move work off a machine that died, and keep traffic away from copies that are not ready. The question is whether that something is you or a control loop.
Orchestration is the answer that writes the promise down. You declare what should be running, with what resources, behind which name, and with which health checks, and a set of controllers keeps chasing that declaration for as long as the cluster runs. Small Kubernetes on a few machines of your own and a managed cluster from a cloud provider run the same system, so the same ideas carry from one to the other: desired state, a scheduler that places work by what it requests, and checks that decide when a copy gets traffic.
That is also why the details here pay off even before you run a cluster. Writing honest resource requests, separating liveness from readiness, keeping state off a container's local disk and keeping every change in a file are habits that make an app easy for any orchestrator to run well. Rolling out new versions safely and sizing how many copies to run are the next questions, and release strategies and capacity planning take them up.