Cascading Deletion and Finalizers
You delete an object and the API answers at once, yet the object can still be listed afterwards and the command keeps waiting in your terminal. What is holding it back, and when is it finally gone for good?
Play the animation 7 steps, animated, plays on a computer or a tablet
Cascading Deletion and Finalizers, step by step
-
You run "kubectl delete deployment my-app --cascade=foreground". The client sends an HTTP DELETE to /apis/apps/v1/.../deployments/my-app on the API with propagationPolicy=Foreground in the request body.
Play from step 1 -
The API does not remove the object but sets metadata.deletionTimestamp and adds the foregroundDeletion finalizer. Then it commits this to ETCD via Raft at rv=843. The Deployment still exists in cluster state, now marked for deletion.
Play from step 2 -
ETCD acks the committed write back to the API, and the API returns HTTP 200 OK to kubectl with the stamped object in the body. The call has returned, but kubectl waits by default until the object is gone, and most of the deletion is still ahead.
Play from step 3 -
The API broadcasts a MODIFIED event for the Deployment to its watchers. The Deployment controller sees the deletionTimestamp and stops issuing rollouts. The Garbage collector walks the ownerReferences and DELETEs ReplicaSet my-app-7d4 in foreground, then Pod my-app-7d4-x2k9p of that ReplicaSet. Each gets a deletionTimestamp but is not removed from ETCD yet.
Play from step 4 -
The Kubelet on Node-1 has a filtered watch for Pods bound to it. The API streams a MODIFIED event for my-app-7d4-x2k9p carrying its new deletionTimestamp down that watch to Node-1, and the Kubelet starts terminating the Pod.
Play from step 5 -
The terminationGracePeriodSeconds budget (30s by default) started when the Pod was stamped. The container gets SIGQUIT, the STOPSIGNAL the nginx image sets, or SIGTERM otherwise. SIGKILL comes only if the container outlives the timer. Once it stops, the Kubelet sets the terminal phase and sends a DELETE with grace 0. The Graceful Pod Shutdown card covers the budget.
Play from step 6 -
The grace 0 DELETE removes the Pod record from ETCD. With no Pod left under it, the Garbage collector clears the foregroundDeletion finalizer off the ReplicaSet, then off the Deployment, and as each list empties the API removes that record too. The Deployment goes last, at rv=856. That completes the step 1 delete by kubectl, so kubectl stops waiting.
Play from step 7