
Kubernetes Doesn't Just Start Containers: It Reconciles Desired State
- كاتب موثّق
محتويات المقال
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
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.
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.
What actually happens
Every object is a record of intent
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
A Deployment is a chain of loops
app=debug, a new Pod appeared within seconds, and the relabeled one kept running with no owner at all.
Commands edit the desired state, and the loop does the rest
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
--context and -n are left out below for space. This file is the whole desired state:
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-alpinekubectl 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.kubectl apply -f web.yaml# deployment.apps/web createdkubectl delete pod web-86c74745c6-gtj47# pod "web-86c74745c6-gtj47" deleted from readinw-k8s namespacekubectl get pods# NAME READY STATUS RESTARTS AGE# web-86c74745c6-4hxtf 1/1 Running 0 1s# web-86c74745c6-jsvw7 1/1 Running 0 7s# web-86c74745c6-pb2xz 1/1 Running 0 7skubectl scale deployment web --replicas=5# deployment.apps/web scaledkubectl get rs# NAME DESIRED CURRENT READY AGE# web-86c74745c6 5 5 3 22skubectl apply -f web.yaml # the file still says 3# deployment.apps/web configuredkubectl get rs# NAME DESIRED CURRENT READY AGE# web-86c74745c6 3 3 3 26skubectl 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.
kubectl commands start and stop containers | |
kubectl scale sets the size for good | kubectl apply of a file with replicas overwrites it |
How to apply it in your project
Find Pods nobody owns Run 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.Keep the desired state in Git Treat the manifest as the truth and change replicas, images and settings there. Anything you change by hand with kubectlis a temporary edit that the next apply can undo.Read status as desired versus current kubectl get rsandkubectl rollout statusshow 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.Restart by changing the template To restart every Pod of a Deployment, run kubectl rollout restart deployment/web. It adds arestartedAtannotation 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 specyou write and astatusthe 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 scaleholds only until the next apply.
التعليقات (0)