Skip to content
Kubernetes day 2 · Chapter 4 of 20 Day 2: What a careless drain costs

Day 2: What a careless drain costs

Goal: Measure how many requests a kubectl drain without precautions costs, and understand why · Duration: approx. 45 min · Prerequisite: Day 0

Briefing

  • Every upgrade, every kernel patch and every node replacement starts with kubectl drain: the node is cordoned, and its Pods are evicted.
  • Kubernetes restarts the evicted Pods elsewhere. Whether requests get lost in the process is not decided by Kubernetes but by the application and its description.
  • Today you build a typical application without precautions and measure the damage. Tomorrow you fix it.
What is missing Consequence during a drain
Readiness probe new Pod gets traffic before it can respond
PodDisruptionBudget all replicas may go away at the same time
preStop hook old Pod dies while requests to it are still in flight

Drill 1: An application that needs time to start

Many real applications need seconds to minutes before they accept requests: a JVM loads classes, a cache is filled, migrations run. shop simulates this with nginx and a ten-second wait before startup.

mkdir -p ~/day2/tag02 && cd ~/day2/tag02
cat > shop.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata: {name: shop, namespace: demo}
spec:
  replicas: 2
  selector: {matchLabels: {app: shop}}
  template:
    metadata: {labels: {app: shop}}
    spec:
      containers:
      - name: nginx
        image: nginxinc/nginx-unprivileged:1.29-alpine
        command: [sh, -c, "sleep 10; exec nginx -g \"daemon off;\""]
        ports: [{containerPort: 8080}]
---
apiVersion: v1
kind: Service
metadata: {name: shop, namespace: demo}
spec:
  selector: {app: shop}
  ports: [{port: 80, targetPort: 8080}]
EOF
kubectl apply -f shop.yaml
sleep 3
kubectl -n demo exec last -- curl -sS -m 2 http://shop.demo

Expected:

curl: (7) Failed to connect to shop.demo port 80 after 8 ms: Could not connect to server
command terminated with exit code 7

After a little over ten seconds, shop responds:

kubectl -n demo exec last -- curl -s -o /dev/null -w "%{http_code}\n" -m 2 http://shop.demo

Expected:

200

Save your progress? Sign up for free

Drill 2: Point the load generator at shop

kubectl -n demo delete pod last
sed "s#http://web.demo#http://shop.demo#" ~/day2/tag00/last.yaml | kubectl apply -f -
kubectl -n demo wait --for=condition=Ready pod/last
kubectl -n demo logs last --tail=2
kubectl -n demo get pods -l app=shop -o custom-columns=POD:.metadata.name,NODE:.spec.nodeName

Expected (Pod names and nodes from your output):

19:54:05 200
19:54:05 200
POD                     NODE
shop-745bdbb55d-bzcl4   cp1
shop-745bdbb55d-nzxbb   cp2

Note the node on which a shop Pod runs that is not cp1. In this course it is cp2.

Save your progress? Sign up for free

Drill 3: The careless drain

kubectl drain cp2 --ignore-daemonsets --delete-emptydir-data

Expected:

node/cp2 cordoned
Warning: ignoring DaemonSet-managed Pods: kube-system/cilium-envoy-lwqzv,
kube-system/cilium-sqkql, kube-system/kube-proxy-kv25q
evicting pod demo/shop-745bdbb55d-nzxbb
pod/shop-745bdbb55d-nzxbb evicted
node/cp2 drained

The drain takes two seconds and reports success. Half a minute later, you count:

kubectl -n demo logs last | wc -l
kubectl -n demo logs last | awk '$2 != 200' | wc -l
kubectl -n demo logs last | awk '$2 != 200' | sed -n '1p;$p'

Expected:

196
21
19:54:22 000
19:54:31 000

21 out of 196 requests without a response, for nine seconds. And that although a second shop Pod was running the whole time. Your numbers will differ, the order of magnitude will not: over several runs, the outages lasted between 9 and 17 seconds.

Save your progress? Sign up for free

Drill 4: Why? Ready is not ready

Repeat the drain (after kubectl uncordon cp2 and a new last) and look at the replacement Pod and the Service endpoints right after the drain:

kubectl uncordon cp2
kubectl -n demo get pods -l app=shop -o wide | cut -c1-80
kubectl -n demo get endpointslices -l kubernetes.io/service-name=shop -o jsonpath=\
'{range .items[*].endpoints[*]}{.targetRef.name} ready={.conditions.ready}{"\n"}{end}'

Expected (three seconds after the drain):

NAME                    READY   STATUS    RESTARTS   AGE   IP             NODE
shop-745bdbb55d-bzcl4   1/1     Running   0          76s   10.244.0.220   cp1
shop-745bdbb55d-thdqj   1/1     Running   0          6s    10.244.2.149   cp3
shop-745bdbb55d-bzcl4 ready=true
shop-745bdbb55d-thdqj ready=true

Cause: The new Pod is six seconds old, nginx is still waiting, and yet it shows 1/1 and ready=true. Without a readiness probe, a container counts as ready as soon as its process is running. The Service therefore sends half of the requests to a Pod that is not even listening yet. That is most of the damage. The rest comes from the old Pod: it terminates immediately while kube-proxy and Cilium still know it as a target.

Save your progress? Sign up for free

Drill 5: A Pod without a controller blocks the drain

On cp1, the load generator runs as a single Pod without a Deployment.

kubectl drain cp1 --ignore-daemonsets --delete-emptydir-data

Expected:

error: unable to drain node "cp1" due to error: cannot delete cannot delete Pods that
declare no controller (use --force to override): demo/last, continuing command...
There are pending nodes to be drained:
 cp1

(Line wrapped.) kubectl drain refuses to delete it because nobody would recreate the Pod. --force would delete it, and then it would be gone. For the load generator there is a better way: evict only the Pods that are not app=last.

kubectl drain cp1 --ignore-daemonsets --delete-emptydir-data --pod-selector='app!=last'
kubectl -n demo get pod last -o wide | cut -c1-60
kubectl uncordon cp1

Expected (abridged):

node/cp1 already cordoned
node/cp1 drained
NAME   READY   STATUS    RESTARTS   AGE   IP            NODE
last   1/1     Running   0          66s   10.244.0.44   cp1
node/cp1 uncordoned

In this course you always drain the node cp1 this way. The load generator keeps running during the upgrade of cp1, because a restart of the kubelet does not stop any containers.

Warning: --force deletes Pods without a controller permanently. In production these are often forgotten debug Pods, but sometimes also something important that someone started by hand. Look first, then decide.

Save your progress? Sign up for free

Drill 6: Chance distributes the replicas

After the two drains:

kubectl -n demo get pods -l app=web -o custom-columns=POD:.metadata.name,NODE:.spec.nodeName

Expected (your distribution may differ):

POD                    NODE
web-776b55fcfb-ff5vh   cp3
web-776b55fcfb-fj4gz   cp2
web-776b55fcfb-p6xjp   cp2

Two of three web Pods run on cp2. A drain of cp2 now takes away two thirds of the capacity at once; with two replicas, in the worst case all of it. The scheduler distributes by free resources, not by fault tolerance. You have to tell it, with topology spread constraints (Day 3).

Save your progress? Sign up for free

Exercise: Speed Drills

  1. Start a new measurement run against shop without changing the file last.yaml. Target time: 1 min
  2. Show only the times of the errors, without duplicates. Target time: 1 min
  3. Cordon cp3 for new Pods without evicting anything, and lift the cordon again. Target time: 1 min

Solution

kubectl -n demo delete pod last
sed "s#http://web.demo#http://shop.demo#" ~/day2/tag00/last.yaml | kubectl apply -f -
kubectl -n demo logs last | awk '$2 != 200 {print $1}' | uniq
kubectl cordon cp3
kubectl get node cp3
kubectl uncordon cp3
node/cp3 cordoned
NAME   STATUS                     ROLES           AGE   VERSION
cp3    Ready,SchedulingDisabled   control-plane   47m   v1.35.10
node/cp3 uncordoned

Save your progress? Sign up for free

Checkpoint

kubectl get nodes

Expected: all three nodes Ready, none SchedulingDisabled.

kubectl -n demo get deploy shop web

Expected:

NAME   READY   UP-TO-DATE   AVAILABLE   AGE
shop   2/2     2            2           3m10s
web    3/3     3            3           45m

Challenge

How many requests does a drain cost when shop has only one replica? Estimate first, then measure.

Hint: kubectl -n demo scale deploy shop --replicas=1, then drain the node of the remaining Pod (for cp1, with --pod-selector).

Solution

kubectl -n demo scale deploy shop --replicas=1
kubectl -n demo get pods -l app=shop -o custom-columns=NODE:.spec.nodeName --no-headers
kubectl -n demo delete pod last
sed "s#http://web.demo#http://shop.demo#" ~/day2/tag00/last.yaml | kubectl apply -f -
kubectl drain <node> --ignore-daemonsets --delete-emptydir-data --pod-selector='app!=last'
kubectl -n demo logs last | awk '$2 != 200' | wc -l
kubectl uncordon <node>
kubectl -n demo scale deploy shop --replicas=2

With one replica, shop fails completely: from the eviction until nginx listens, a little over ten seconds plus image pull and scheduling. At 0.2 seconds between requests, that is about 50 errors, in other words every request during that time.

Save your progress? Sign up for free

Cleanup

All nodes must be schedulable again:

kubectl uncordon cp1 cp2 cp3

shop and last (pointed at shop) stay in place for Day 3.

Interview Note

  • What exactly does kubectl drain do? Cordon the node, then evict every Pod via the Eviction API. DaemonSet Pods and static Pods stay; Pods without a controller block the drain unless you use --force.
  • Why do requests get lost even though two replicas are running? Without a readiness probe, the replacement Pod counts as ready immediately, and the old Pod ends before all endpoints are updated.
  • What is the difference between cordon and drain? Cordon only blocks new Pods; drain cordons and evicts.

Enterprise Note

  • Maintenance windows are often justified with error budgets (SLOs). A drain that measurably loses requests eats into the error budget.
  • The cluster autoscaler and node upgrades in the cloud drain automatically. What happens here by hand happens there without asking.
  • A load generator like this one belongs in every upgrade dress rehearsal; in production it is replaced by synthetic checks from the monitoring solution.

Contents

More