You type kubectl apply -f deployment.yaml, hit enter, and a few hundred milliseconds later your cluster has a new Deployment. It feels like a single atomic action. It is not. That one command triggers a chain of HTTP calls, JSON patch computations, optimistic locking checks, and a distributed consensus protocol replicating your 40-line YAML file across multiple machines before anyone can read it back.
This post walks through that chain step by step, with enough detail that the next time an apply hangs for 30 seconds you'll know exactly where to look.
🚀 The Illusion of a Single Command
kubectl apply is not a single API call. It's a client-side three-way merge:
- kubectl reads the live object from the API server (if it exists)
- It reads the last-applied-configuration annotation (the previous apply)
- It computes a strategic merge patch between last-applied, your new local file, and the live object
- It sends that patch as a
PATCH(orPOSTfor create) request
That means kubectl apply on an existing object is a PATCH to /api/v1/namespaces/<ns>/pods/<name> or the equivalent for your GVK, not a raw PUT of your file contents. This matters because the patch only touches fields you actually changed, which is exactly why two people applying different partial configs to the same resource don't always stomp on each other.
🔍 The Admission Chain
Once the request lands on kube-apiserver, it goes through a pipeline before anything touches storage:
Request
-> Authentication (who are you)
-> Authorization (RBAC: can you PATCH this resource)
-> Mutating Admission Webhooks + built-in mutators
-> Object Schema Validation (OpenAPI / CRD schema)
-> Validating Admission Webhooks
-> etcd write
The mutating stage is where defaults get injected — things like imagePullPolicy, resource requests from a LimitRange, or an Istio sidecar container getting stitched into your Pod spec. Every mutating webhook you've registered runs synchronously here, in series, each with its own timeout:
yaml
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
name: sidecar-injector
webhooks:
- name: inject.istio.io timeoutSeconds: 10 failurePolicy: Fail clientConfig: service: name: istiod namespace: istio-system path: "/inject"
If that webhook's pod is unhealthy, failurePolicy: Fail means your kubectl apply blocks for up to timeoutSeconds and then fails outright. This is the single most common cause of "apply just hangs" in clusters running service meshes or policy engines like OPA Gatekeeper or Kyverno. Nothing is wrong with etcd — your request never got that far.
🔒 Optimistic Concurrency via resourceVersion
Assuming admission passes, the API server needs to write the object. Kubernetes doesn't lock resources. It uses optimistic concurrency control keyed on resourceVersion, which is actually the etcd mod revision of that key, exposed to you as an opaque string.
Every object carries one:
yaml
metadata:
name: payments-api
resourceVersion: "184823"
When the apiserver writes an update, it does a conditional transaction against etcd roughly equivalent to:
IF mod_revision(key) == 184823
THEN put(key, new_value)
ELSE return conflict
etcd exposes this directly via its transaction API:
go
txn := client.Txn(ctx).If(
clientv3.Compare(clientv3.ModRevision("/registry/deployments/default/payments-api"), "=", 184823),
).Then(
clientv3.OpPut("/registry/deployments/default/payments-api", encodedObject),
).Else(
clientv3.OpGet("/registry/deployments/default/payments-api"),
)
resp, err := txn.Commit()
If someone else updated the object between your GET and your PATCH, the compare fails, the apiserver returns 409 Conflict, and kubectl apply retries the three-way merge against the new resourceVersion automatically. This is why concurrent controllers reconciling the same object don't corrupt it — they race, one wins, the loser retries. No distributed lock needed, no deadlocks possible, at the cost of occasional retries under contention.
🗳️ Raft Turns One Write Into a Cluster Decision
Here's the part most people wave their hands at: etcd isn't a single process, it's a raft cluster, typically 3 or 5 members. A write isn't "done" when one node accepts it — it's done when a majority of the raft group has durably persisted it.
The flow for that single PUT from above:
- The apiserver's etcd client sends the write to whichever member it's connected to
- If that member isn't the raft leader, it forwards the request to the leader
- The leader appends the entry to its local raft log and broadcasts
AppendEntriesto followers - Once a quorum (2 of 3, or 3 of 5) has persisted the entry to their own WAL, the leader commits it
- The leader applies it to its boltdb-backed MVCC store and responds success
- Followers apply the committed entry to their own stores asynchronously
This is why etcd documentation obsesses over disk fsync latency — AppendEntries isn't acknowledged until the entry is fsynced to disk on each voting member. A slow disk on any member of the quorum directly adds latency to every write in the cluster, not just writes routed to that node.
bash
check real disk commit latency etcd is experiencing
etcdctl --endpoints=https://127.0.0.1:2379 endpoint status --write-out=table
look at raft-level metrics directly
curl -s http://127.0.0.1:2381/metrics | grep etcd_disk_wal_fsync_duration_seconds
If etcd_disk_wal_fsync_duration_seconds p99 creeps above ~25ms, writes across the entire cluster slow down, and that includes every apiserver write, every lease renewal, every controller's status update.
🐢 When Applies Silently Stall
With the full picture in mind, a stalled kubectl apply has a short list of real suspects:
- Webhook timeout: a mutating or validating webhook's backend is down or slow. Check
failurePolicyand webhook pod health first — this is the most common cause by far. - 409 conflict storm: a controller is hot-looping updates on the object you're trying to patch, so your resourceVersion is stale before the patch lands. Watch for repeated
Conflictentries in apiserver audit logs. - etcd quorum loss: fewer than a majority of etcd members are reachable, so no writes can commit at all, even though reads of stale data might still work against a minority partition.
- Slow disk / fsync latency on an etcd member: writes succeed but take seconds instead of milliseconds.
- apiserver -> etcd network partition or cert expiry: look for
etcdserver: request timed outerrors in apiserver logs.
A quick triage sequence:
bash
is etcd quorum healthy?
ETCDCTL_API=3 etcdctl endpoint health --cluster
is there a leader, and how fresh is replication?
ETCDCTL_API=3 etcdctl endpoint status --cluster -w table
apiserver's own view of etcd latency
curl -s http://127.0.0.1:6443/metrics | grep etcd_request_duration_seconds
audit log check for conflicts on your object
kubectl get events --field-selector involvedObject.name=payments-api
If endpoint health shows a healthy quorum and etcd_request_duration_seconds looks fine, the problem is almost certainly upstream in admission, not in etcd at all — which is the opposite of where most people start looking.
🤔 Where Have You Actually Seen This Break?
I've seen far more apply-hangs caused by a misbehaving mutating webhook than by actual etcd degradation — etcd tends to fail loudly (quorum loss, obvious latency spikes) while webhooks fail quietly with a generic timeout. What's been the actual root cause the last time an apply mysteriously stalled in your cluster? Webhook, RBAC, a controller fighting over resourceVersion, or etcd itself?
Understanding this chain end to end is less about trivia and more about knowing which layer to check first when something's slow: admission before etcd, resourceVersion conflicts before raft, and disk fsync latency before you start suspecting the network.
