Skip to main content
InterviewHubPRO
Section Overview
Q23

Question 23 of 50

Part 2: Core Architecture & Implementation (Mid-Level)

46%
Kubernetes (K8s) (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)

Zero-Downtime Rolling Updates: `maxSurge`, `maxUnavailable`, and `preStop` Hooks.

senior LevelCode ExampleHooksSystem DesignHTTP & REST APIs

In Simple Words: To achieve 100% zero dropped connections during a deployment update:

yaml
strategy:
  type: RollingUpdate
  rollingUpdate:
    maxSurge: 25%         # Can spin up 25% extra new pods above desired count
    maxUnavailable: 0     # ZERO pods are terminated until new pods pass Readiness check!

The Connection Dropping Race Condition & preStop Hook:

When a pod is terminated:

  1. API Server deletes pod from EndpointSlice (async).
  2. API Server sends SIGTERM to the container process.
  3. If the container process shuts down immediately, incoming in-flight TCP packets routed by kube-proxy still hit the dying container, returning 502 Bad Gateway!
  • Fix: Add a preStop sleep hook:
    yaml
    lifecycle:
      preStop:
        exec:
          command: ["/bin/sh", "-c", "sleep 15"]
    
    The container waits 15 seconds before processing SIGTERM, giving load balancers enough time to flush in-flight connections and stop routing new requests to the dying pod.