الانتقال إلى المحتوى
DevOps والـ CloudKubernetes· 8 دقائق للقراءة

Kubernetes Doesn't Just Start Containers: It Reconciles Desired State

Objects record what you want, and controllers loop forever to make the cluster match. Commands only change the target.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

A team moves its API from virtual machines to Kubernetes and keeps its old runbook. When a Pod misbehaves, the on-call engineer deletes it and then runs the deploy script again "to bring it back". During a traffic spike, someone runs kubectl scale to eight replicas; an hour later the pipeline's routine kubectl apply drops it back to three, in the middle of the peak. Meanwhile, Pods nobody touched keep getting new names. The team starts to feel the cluster has a mind of its own.

The common misconception

The belief: Kubernetes is a remote control for containers. kubectl run starts one, kubectl delete stops it, kubectl scale adds a few more, and if a Pod dies, it's your job to start a new one.

It's an honest reading of the tools. The verbs sound like orders, and many developers learned containers with docker run, where a stopped container stays stopped until someone starts it again. For one kind of object it's even true: a Pod you create directly isn't replaced when it's deleted, as the first tab below shows.

The half-truth: kubectl does send requests to the cluster. But most of them don't start anything. They change a description of what you want, and the actual work is done by loops that never stop running.

What actually happens

Every object is a record of intent

The Kubernetes docs call an object a "record of intent": "once you create the object, the Kubernetes system will constantly work to ensure that the object exists." Most objects have a spec, the you write, and a status, the current state the cluster reports. The docs state the promise plainly: the control plane "continually and actively manages every object's actual state to match the desired state you supplied."

Controllers are loops, not commands

That work is done by controllers. The docs borrow the from automation, "a non-terminating loop that regulates the state of a system", and use a thermostat as the example: you set the temperature, and it "acts to bring the current state closer to the desired state, by turning equipment on or off." Kubernetes controllers "watch the state of your cluster, then make or request changes where needed."

Controllers rarely touch containers themselves. The Job controller, for example, "does not run any Pods or containers itself. Instead, the Job controller tells the API server to create or remove Pods." The scheduler then assigns each new Pod to a node, and the kubelet on that node "ensures that Pods are running, including their containers." Each piece reacts to what the previous one wrote, and none of them waits for a person.

A Deployment is a chain of loops

Apply a Deployment and several controllers take part. The Deployment controller creates a from your Pod template and labels it with a hash of that template. The ReplicaSet controller keeps a count: it does its job "by creating and deleting Pods as needed to reach the desired number." It counts every Pod that matches its label selector, not a list of Pods it remembers. When we relabeled one of our three Pods to app=debug, a new Pod appeared within seconds, and the relabeled one kept running with no owner at all.

That's also why the Pods docs say "You'll rarely create individual Pods directly in Kubernetes—even singleton Pods." Pods are disposable; the controller above them is what heals. In the docs' words, "if a Node fails, a controller notices that Pods on that Node have stopped working and creates a replacement Pod."

Commands edit the desired state, and the loop does the rest

Seen this way, each command is an edit. kubectl scale writes a new spec.replicas. kubectl delete pod makes the current state differ from the desired one, so the controller repairs it. Changing the image edits the Pod template, which "has no direct effect on the Pods that already exist", so the Deployment rolls out a new ReplicaSet that replaces them. And kubectl apply writes your file back as the desired state, replica count included.

Watching the loop work

We ran this on Docker Desktop's Kubernetes (server v1.34.3, kubectl v1.34.1) in a namespace of our own; --context and -n are left out below for space. This file is the whole desired state:

YAML web.yaml
apiVersion: apps/v1kind: Deploymentmetadata:  name: webspec:  replicas: 3  selector:    matchLabels:      app: web  template:    metadata:      labels:        app: web    spec:      containers:        - name: nginx          image: nginx:1.29.8-alpine
Bash terminal
kubectl run lonely --image=nginx:1.29.8-alpine# pod/lonely createdkubectl delete pod lonely# pod "lonely" deleted from readinw-k8s namespacekubectl get pods# No resources found in readinw-k8s namespace.

The bare Pod in the first tab is gone for good: nothing in the cluster wanted it to exist. In the second tab, the deleted Pod already had a replacement, with a new name, by the time kubectl get pods ran. Right after the scale, the ReplicaSet reported 5 desired but only 3 ready, the gap the loop was closing; a few seconds later it was 5 of 5. Then kubectl apply set the count back to 3, and the controller deleted two Pods. kubectl describe rs lists every step as an event from replicaset-controller: "Created pod", "Deleted pod", never a person.

MythReality
kubectl commands start and stop containersThey edit objects; controllers and the kubelet do the work
If a Pod dies, you start a new oneThe ReplicaSet controller replaces any Pod its count is missing
A ReplicaSet tracks a list of its PodsIt counts the Pods that match its label selector
kubectl scale sets the size for goodThe next kubectl apply of a file with replicas overwrites it
A bare Pod is a small DeploymentNothing replaces it when it's deleted or its node fails

How to apply it in your project

  1. Find Pods nobody ownsRun kubectl get pods -o custom-columns=NAME:.metadata.name,OWNER:.metadata.ownerReferences[0].kind. A <none> in the OWNER column is a Pod that won't come back if it dies. Move long-running ones into a Deployment and one-off tasks into a Job.
  2. Keep the desired state in GitTreat the manifest as the truth and change replicas, images and settings there. Anything you change by hand with kubectl is a temporary edit that the next apply can undo.
  3. Read status as desired versus currentkubectl get rs and kubectl rollout status show the gap the loop is closing. A count stuck below desired means a controller can't do its job, and the events of the ReplicaSet and its Pods usually say why.
  4. Restart by changing the templateTo restart every Pod of a Deployment, run kubectl rollout restart deployment/web. It adds a restartedAt annotation to the Pod template, so the Deployment rolls out a new ReplicaSet and replaces the Pods gradually.

Key takeaways

  • Kubernetes objects are records of intent: a spec you write and a status the cluster reports.
  • Controllers are loops that keep moving the current state toward the desired state, without waiting for you.
  • A Deployment manages a ReplicaSet, which counts the Pods matching its selector and creates or deletes Pods to match.
  • A Pod you create by hand has no controller, so nothing replaces it.
  • Change the desired state in your manifest; a manual kubectl scale holds only until the next apply.

📚 المصادر

  1. Controllers — Kubernetes Documentation
  2. Objects In Kubernetes — Kubernetes Documentation
  3. Pods — Kubernetes Documentation
  4. Deployments — Kubernetes Documentation
  5. ReplicaSet — Kubernetes Documentation

التعليقات (0)