Octonote AI
Make your own →

What happens when you run kubectl apply

Sunil Tiwari · Oct 12, 2026 · 5 min read

From a YAML file to running containers: the API server, etcd, controllers, the scheduler and the kubelet.

You type kubectl apply -f deployment.yaml and a few seconds later three pods are running. In between, half a dozen components cooperate without ever calling each other. None of them knows the others exist. They all talk to one thing, the API server, and they all follow one pattern, watch and reconcile.

This page follows a single Deployment from your terminal to a running container.

kubectl apply, end to end
kubectl apply, end to end

The manifest

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 3
  selector:
    matchLabels: { app: web }
  template:
    metadata:
      labels: { app: web }
    spec:
      containers:
        - name: web
          image: nginx:1.27
          ports: [{ containerPort: 80 }]

This file describes desired state: "there should be three copies of nginx 1.27, labelled app: web". It says nothing about how to get there. Getting there is the cluster's job.

Step 1: kubectl prepares the request

kubectl is just an HTTP client. It reads your kubeconfig for the cluster address and credentials, parses the YAML and asks the API server whether deployment/web already exists.

  • If it does not, kubectl sends a POST to create it.
  • If it does, kubectl computes a three-way merge between the live object, your file and the last-applied-configuration annotation it stored last time, then sends a PATCH with only the differences.

With --server-side, the merge moves into the API server instead, which tracks which manager owns each field.

Step 2: the API server checks and stores it

Every write passes through the same pipeline before it is allowed anywhere near the database.

Inside the API server
Inside the API server
  1. Authentication. Who is calling? A client certificate, a bearer token or an OIDC token from your identity provider.
  2. Authorization. May this identity create Deployments in this namespace? Usually answered by RBAC rules.
  3. Mutating admission. Plugins and webhooks may change the object: fill in defaults, inject a sidecar, add labels.
  4. Schema validation. Is this a well-formed apps/v1 Deployment?
  5. Validating admission. Policies get the final say: resource quotas, Pod Security admission, OPA Gatekeeper or Kyverno rules.
  6. Persist. The object is written to etcd, the cluster's only database, and gets a new resourceVersion.

kubectl receives 201 Created and exits. Notice what has not happened yet: no pod exists, no container is running. All you have is a record of intent.

Step 3: controllers turn intent into objects

Inside the controller manager, dozens of controllers each hold a watch on the API server: a long-lived streaming request that delivers an event every time an object of their type changes.

Who does what, in order
Who does what, in order
  • The Deployment controller sees a new Deployment with no ReplicaSet. It creates one, named after a hash of the pod template (web-7c9d).
  • The ReplicaSet controller sees a ReplicaSet that wants 3 pods and has 0. It creates 3 Pod objects. Their spec.nodeName is empty: nobody has decided where they run.
  • The scheduler watches for exactly that, pods without a node. For each one it filters out nodes that cannot run it (not enough CPU or memory, taints, affinity rules), scores the rest, and writes a binding that sets nodeName.
  • The kubelet on each node watches for pods bound to its node. When one appears, it starts work.

Every arrow in that sequence goes to the API server. The scheduler never calls the kubelet. The controllers never call the scheduler. They only read and write objects.

Step 4: the kubelet makes it real

The kubelet on node-2 now has a pod to run. Through the Container Runtime Interface (CRI) it asks the runtime, usually containerd, to:

  1. create the pod sandbox, a network namespace shared by the pod's containers,
  2. call the CNI plugin, which gives the pod its IP address (say 10.244.2.17),
  3. pull nginx:1.27 if the node does not already have it,
  4. create and start the container with its cgroups, mounts and environment.

Then it runs the pod's startup, readiness and liveness probes and reports status back to the API server: phase Running, condition Ready: true. That status is what kubectl get pods shows you.

Who owns what

The objects created along the way form a chain of ownership, recorded in each child's metadata.ownerReferences.

Who owns what
Who owns what

When you change the pod template (say, a new image tag), the Deployment controller creates a new ReplicaSet and gradually scales it up while scaling the old one down. That is a rolling update. The old ReplicaSet stays around at zero replicas, which is what makes kubectl rollout undo instant. Delete the Deployment and garbage collection follows the owner references down to every pod.

The key idea: reconciliation

Every controller in Kubernetes, built-in or custom, runs the same loop:

The reconciliation loop
The reconciliation loop

Compare the desired state (in etcd) with the actual state, act to close the gap, then wait for the next change. The loop is level-triggered: it reacts to the current state, not to the event that announced it. If a controller misses an event or restarts, it simply looks again and still does the right thing.

That one design choice explains a lot of everyday Kubernetes behaviour:

  • Delete a pod and a new one appears. The ReplicaSet still wants 3 and now sees 2.
  • A node dies and its pods are recreated elsewhere. Nobody "moves" them, the gap is just closed again.
  • Applying the same file twice is harmless. The second time there is no difference to act on.
Nothing in Kubernetes calls anything else. Everything watches the API server and reconciles.

Check it yourself

kubectl apply -f deployment.yaml
kubectl get events --sort-by=.metadata.creationTimestamp   # the controllers' trail
kubectl get deploy,rs,pods -l app=web -o wide            # the ownership chain
kubectl get pod <name> -o jsonpath='{.metadata.ownerReferences}'
kubectl delete pod <name> && kubectl get pods -w         # watch reconciliation happen
Make a page like this. Free.Notes and diagrams side by side, written by you or by Claude Code and Codex. Publish it in one click.
Make your own, free