Explain in detail the chronology of the process of stopping and deleting the Pod (Termination Lifecycle) and what steps are necessary to achieve Zero-Downtime?
When you issue a delete Pod command, the API Server determines the deletionTimestamp and two parallel processes start: the Endpoints Controller removes the Pod IP from the service and routing path in Kube-Proxy/Ingress, the Kubelet executes a preStop hook, then sends a SIGTERM signal, then waits for terminationGracePeriodSeconds, and finally sends a SIGKILL signal if the pod does not close.
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
The exact timeline of the Graceful Shutdown process:
- The user executes the deletion command or the Deployment performs a gradual update.
kube-apiserverrecords thedeletionTimestampfield and theTerminatingstate.- First parallel path (network management):
- The EndpointSlice controller detects the deletion status and removes the pod’s IP address from the endpoints.
- The
kube-proxyand Ingress Controllers components update iptables/IPVS or NGINX configuration tables to stop routing new requests to them.
- The second parallel path (Kubelet on the node):
- Kubelet executes a
preStophook (if present). - After
preStopcompletes, it sends aSIGTERMsignal (Signal 15) to PID 1 inside the container. - The system waits for the timeout specified in
terminationGracePeriodSeconds(default 30 seconds). - If the container does not close automatically, Kubelet sends a
SIGKILLsignal (Signal 9) to forcibly terminate the process.
- Kubelet executes a
spec:
terminationGracePeriodSeconds: 45
containers:
-name: api
lifecycle:
preStop:
exec:
# Give the network 10 seconds to delete the pod from the load balancers before terminating the application
command: ["/bin/sh", "-c", "sleep 10"]- Believe that Pod removal from Service Endpoints occurs immediately before SIGTERM is sent; there is a delay in propagating the change across the network requiring a short sleep mode on the preStop hook to avoid 502/504 errors.
- The SIGTERM signal is not processed within the application code, breaking open connections to the databases.
- What is the default value for the terminationGracePeriodSeconds field?
- How does placing sleep 5 inside the preStop hook affect the avoidance of interruption to ongoing user requests?
What are QoS Classes in Kubernetes, and how do they affect the order of pod eviction when a node runs out of resources?
Pods are divided into three categories: Guaranteed (when requests match memory and processor limits in all containers), Burstable (when requests are set less than limits), and BestEffort (no requests or limits are set). When memory runs out, BestEffort pods are evicted first, then Burstable, and Guaranteed pods are evicted last.
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
Kubernetes automatically determines the QoS class without an explicit field, based on resources values:
- Guaranteed:
- Each container inside the pod has
requests.cpu == limits.cpuandrequests.memory == limits.memory. oom_score_adj = -997(highest kill protection) is set.
- Each container inside the pod has
- Burstable:
- The container has
requestsspecified, but less thanlimits, or one resource is specified but not the other. - The kernel calculates
oom_score_adjrelative to the percentage of memory consumption compared to the required limits.
- The container has
- BestEffort:
- There are no
requestsorlimitsspecified for memory or processor. oom_score_adj = 1000is set, and the pod is the first victim of the OOM Killer when the node experiences Node MemoryPressure.
- There are no
- Do not set requests at all, which automatically puts the pod in the BestEffort category and is vulnerable to immediate killing by the OOM Killer at any server stress.
- Setting limits too high without realistic requests, confuses the scheduling system and causes congestion on the node.
- How is the value of oom_score_adj calculated in the Linux kernel based on the QoS class of the Pod?
- What is the difference between killing the container due to OOMKilled (Exit Code 137) and expelling the entire Pod (Eviction) with Kubelet?
When should you use StatefulSet instead of Deployment, and what unique properties does it offer for Stateful Applications?
StatefulSet is used for applications that require a stable, unchanging network identity, dedicated persistent storage for each copy, and a specific order of starting, updating, and stopping (such as distributed databases Kafka, Elasticsearch, and PostgreSQL), while Deployment is used for stateless applications in which copies are identical and replaceable without ordering.
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
Fundamental differences between Deployment and StatefulSet:
| Feature | Deployment (Stateless) | StatefulSet(Stateful) |
|---|---|---|
| Name of capsules | unexpected randomness (web-79bb8f-xyz) |
A stable sequence starting from zero (db-0, db-1, db-2) |
| Network Identity | Random IP served by Service Random | Unique DNS domain name for each pod via Headless Service (db-0.db-service.ns.svc.cluster.local) |
| Permanent storage | Public or shared disks | Template volumeClaimTemplates associates each pod with a dedicated, permanent PV disk that cannot be deleted by deleting the pod |
| Order of publication | parallel and fast | strict serial (db-1 does not start until db-0 becomes Running & Ready) |
- Using Deployment with a ReadWriteOnce-style shared PersistentVolumeClaim for a distributed database, results in disk mount failure when moving the pod to another node.
- Forgetting to create the Headless Service (which has clusterIP: None) when defining a StatefulSet, which is needed to create unique DNS records for each pod.
- How are StatefulSet capsules and tablets (PVC Templates) named?
- What is the difference between RollingUpdate and OnDelete update strategy in StatefulSet?