Skip to content
BIASED
Back to topic

Optional

Replica reconciliation loop

Desired=3, current=2+CrashLoop: what the Deployment/ReplicaSet controller does on the next tick.

Version
v1
Minutes
~20
Evidence
Self-check in the notebook

Until platform auto-check exists, compare cell output with the expected numbers in the notebook. This is not a debt and does not affect mastery.

Download .ipynb

Download the notebook and run it locally. Its environment requirements are listed inside.

Notebook contents

Readable on your phone. The code below has not been executed in the browser; download the notebook to run it.

DevOps · K8s maxSurge / surge реплик

Activity version: 1 · Seed: n/a · Данные: Deployment на 4 репликах, maxSurge/maxUnavailable = 25%.

surge=⌈0.25×4⌉=1\mathrm{surge}=\lceil 0.25\times 4\rceil=1, unavail=⌊0.25×4⌋=1\mathrm{unavail}=\lfloor 0.25\times 4\rfloor=1 → максимум 5 Pods, минимум 3 available.

Ожидаемые числа
Quantity Value Tolerance
maxSurge pods 1 exact
maxUnavailable 1 exact
max total pods 5 exact
min available 3 exact
Самопроверка в ноутбуке

Не влияет на mastery.

import math
 
replicas = 4
max_surge_pct = 0.25
max_unavail_pct = 0.25
 
surge = math.ceil(max_surge_pct * replicas)
unavail = math.floor(max_unavail_pct * replicas)
max_total = replicas + surge
min_available = replicas - unavail
 
# Reconcile tick: desired=3, ready=2, crashloop=1 → create until desired ready slots.
desired = 3
ready = 2
crashloop = 1  # still counts toward replicas until replaced
# ReplicaSet creates pods until len(pods)==desired; CrashLoop pod still occupies a slot.
pods_now = ready + crashloop
to_create = max(0, desired - pods_now)
 
print("surge", surge, "unavail", unavail, "max_total", max_total, "min_available", min_available)
print("reconcile_to_create", to_create)
assert surge == 1 and unavail == 1
assert max_total == 5 and min_available == 3
assert to_create == 0  # already at desired count; controller may replace crashloop via restart policy
assert math.ceil(0.25 * 4) == 1
print("OK: K8s surge math")
 
Самопроверка в ноутбуке
  • surge = 1, unavail = 1
  • max total = 5, min available = 3