Scheduling and nodesLesson 3 of 10
  1. Requests, limits and quotas, not reconciled
  2. QoS classes and node-pressure eviction, not reconciled
  3. cgroups, memory pressure and the OOM killer, not reconciled
    1. Where a pod’s limits live
    2. Requests are weights, limits are ceilings
    3. Who dies first
    4. The kernel kills, the kubelet evicts
    5. Pressure before failure
    6. Find an OOM kill in three places
  4. How the scheduler places a pod, not reconciled
  5. Affinity, taints and tolerations, not reconciled
  6. Topology spread constraints, not reconciled
  7. Priority and preemption, not reconciled
  8. Nodes and the kubelet, not reconciled
  9. Disruptions and PodDisruptionBudgets, not reconciled
  10. Cluster autoscaling, not reconciled

cgroups, memory pressure and the OOM killer

Requests and limits end up as files in a cgroup tree on the node. Read them, see which process the kernel kills first, and tell a kernel OOM kill from a kubelet eviction by the evidence each leaves.

  • 22 min
  • 5 objectives
  • 6 scenes

Shows the lesson one block at a time. Arrow keys step, number keys jump to a scene, and Escape returns to the page where you were.

A pod’s resources block is a request to the scheduler and an instruction to the kernel. The scheduler reads requests to place the pod; the kubelet turns requests and limits into files in a cgroup tree on the node, and from then on the kernel enforces them, without asking Kubernetes. This lesson reads those files on kx-worker3, which runs a Guaranteed redis pod, cache, and one replica of a Burstable nginx Deployment, web.

Reconcile, in 6 scenes

Where a pod’s limits live

Every kind node runs cgroup v2 with the systemd cgroup driver, and kind sets the kubelet’s cgroupRoot to /kubelet. The kubelet builds its tree under kubelet.slice/kubelet-kubepods.slice:

  • Guaranteed pods get a slice directly under kubelet-kubepods.slice, named after the pod UID with dashes turned into underscores: kubelet-kubepods-pod<uid>.slice.
  • Burstable and BestEffort pods get a slice inside their class’s slice, kubelet-kubepods-burstable.slice or kubelet-kubepods-besteffort.slice.
  • Each container, the pause container included, gets a scope inside its pod’s slice, named by containerd: cri-containerd-<container ID>.scope.
From kubelet.slice to the redis container
kubectl get pod cache -o jsonpath='{.metadata.uid} {.status.qosClass}'
d4d12aeb-4890-49fc-abd4-bf9b42c427ef Guaranteed
docker exec kx-worker3 systemd-cgls --no-pager /kubelet.slice
CGroup /kubelet.slice:
├─kubelet.service
│ └─240 /usr/bin/kubelet --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubel…
└─kubelet-kubepods.slice
  ├─kubelet-kubepods-besteffort.slice
  │ └─kubelet-kubepods-besteffort-pod048dd70a_12d0_4ee0_8274_57569bcf634a.slice
  │   ├─cri-containerd-100c81732ee3ac12e976fa04ed44f4d9015aef7ba1c4e3dee8efc06cf44cf9dd.scope …
  │   │ └─382 /usr/local/bin/kube-proxy --config=/var/lib/kube-proxy/config.con…
  │   └─cri-containerd-6686ce11d3012a3259824fec0453445dd5bc003563052c9a68a48d28f04822bd.scope …
  │     └─373 /pause
  ├─kubelet-kubepods-podd4d12aeb_4890_49fc_abd4_bf9b42c427ef.slice
  │ ├─cri-containerd-5d68a39d6865dbd23db0b6f827339f8d4af6cb89956f0d5af263c37cce37b77d.scope …
  │ │ └─658 redis-server *:6379…
  │ └─cri-containerd-f749dd03637f3166683e54baf01aa539a9432843c9e497727cee80faed040c45.scope …
  │   └─647 /pause
  └─kubelet-kubepods-burstable.slice
    ├─kubelet-kubepods-burstable-podc4116912_cd45_48d5_b692_28da4b996551.slice
    │ ├─cri-containerd-63be22edee76e8b2e043a9e436907479989e3acde915dd46497b6d043868ea65.scope …
    │ │ └─623 /pause
    │ └─cri-containerd-920a3b5f59ab7f42c97a11fe3324af9d824a4a4ba3079e3a9f055c223dad1fbd.scope …
    │   ├─702 nginx: master process nginx -g daemon off;
    │   ├─741 nginx: worker process
    │   ├─742 nginx: worker process
    │   ├─743 nginx: worker process
    │   └─744 nginx: worker process
    └─kubelet-kubepods-burstable-pod29491c6d_0913_4681_989e_c1820948fd4a.slice
      ├─cri-containerd-e23ff41b3ee2fadea562d222bf4dc8c1a20f36f322443452e9dced6a71d6aa45.scope …
      │ └─450 /bin/kindnetd
      └─cri-containerd-efbf4da30f833fd217f46a163efe1d5dc96ffcd744595c72c3541f02e4c83465.scope …
        └─441 /pause
docker exec kx-worker3 sh -c 'cat /proc/$(pidof -s redis-server)/cgroup'
0::/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-podd4d12aeb_4890_49fc_abd4_bf9b42c427ef.slice/cri-containerd-5d68a39d6865dbd23db0b6f827339f8d4af6cb89956f0d5af263c37cce37b77d.scope
docker exec kx-worker3 cat /sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-podd4d12aeb_4890_49fc_abd4_bf9b42c427ef.slice/cri-containerd-5d68a39d6865dbd23db0b6f827339f8d4af6cb89956f0d5af263c37cce37b77d.scope/cpu.max /sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-podd4d12aeb_4890_49fc_abd4_bf9b42c427ef.slice/cri-containerd-5d68a39d6865dbd23db0b6f827339f8d4af6cb89956f0d5af263c37cce37b77d.scope/cpu.weight /sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-podd4d12aeb_4890_49fc_abd4_bf9b42c427ef.slice/cri-containerd-5d68a39d6865dbd23db0b6f827339f8d4af6cb89956f0d5af263c37cce37b77d.scope/memory.max
25000 100000
35
268435456

/proc/<pid>/cgroup is the shortest way from a process to its cgroup: on cgroup v2 it is one line, 0:: and a path under /sys/fs/cgroup. The last command prints three of the scope’s files in order. cpu.max is 25000 100000: 25 milliseconds of CPU time in every 100-millisecond period, the 250m limit. memory.max is 268435456 bytes, the 256Mi limit. cpu.weight comes from the 250m request, through the conversion in the next section.

The pod’s slice holds the same limits summed over its containers, so a pod can never use more than the total of what its containers declare. The class slices have no limits of their own; they exist to group pods for CPU weights.

Open the x-ray on cache at the cgroup layer to see each of these files beside the field of the pod spec that produced it.

ExplainerThe Linux under a pod

cacheGuaranteed pod on kx-worker3, IP 10.244.3.2

Six layers sit under this pod. Press X-ray to lift them apart.

cgroupon kx-worker3

docker exec kx-worker3 cat /sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-podd4d12aeb_4890_49fc_abd4_bf9b42c427ef.slice/cri-containerd-5d68a39d6865dbd23db0b6f827339f8d4af6cb89956f0d5af263c37cce37b77d.scope/memory.max
268435456

The only layer kubectl shows. Everything under it exists because the kubelet on kx-worker3 turned this object into calls to containerd: a sandbox, then a container for each entry in spec.containers.containerd starts the pause process first. It holds the network, UTS and IPC namespaces every container in the pod joins, so they share one IP, localhost and a hostname.The container is these processes: PID 1 inside it is pid 658 on the node. It runs as uid 999, a number because the node's passwd has no name for it. It has its own mount, PID and cgroup namespaces and joins the sandbox's others.Limits.cpu 250m became cpu.max 25000 100000, and requests.cpu 250m became 256 shares and cpu.weight 35. The dashed box is memory.max, 256 MiB, from limits.memory; the fill is memory.current.kindnet's ptp plugin made a veth pair: eth0 inside the pod and veth86a0321f on the node, with a /32 route to 10.244.3.2 through it. kube-proxy's DNAT sends Service traffic down the same route.The root is an overlay of read-only image layers under one writable layer that goes when the container does. Volumes are bind mounts from the node's disk, or tmpfs for Secrets and the service account token.

Cluster starting
Press X-ray to lift the pod apart into the six Linux layers under it, then pick one to see the command that shows it on the node and what that command prints. Put on prompt puts the command in the lesson terminal for you to run.

Requests are weights, limits are ceilings

A CPU request never caps anything. The kubelet converts it to CPU shares, 1024 per core, so 250m is 256 shares and 100m is 102, and cgroup v2 expresses shares as cpu.weight, from 1 to 10000. Two conversions are in use on one node. runc writes a container scope’s weight with the conversion in opencontainers/cgroups, which keeps the default weight of 100 for one core; systemd sets the pod and class slices from shares with a linear mapping. The numbers differ, but what matters is the ratio between siblings, and both conversions preserve the order.

A weight only matters when siblings compete. If web and cache both want every CPU on the node, the kernel divides time between their slices in proportion to their weights. If nothing else wants CPU, either can use every core up to its own cpu.max.

A CPU limit is a quota, enforced per period whether the node is busy or not. A container whose threads use up the quota inside a period waits for the next one; that wait is throttling. cpu.stat counts it: nr_periods is the number of periods in which the cgroup ran, nr_throttled the number in which it hit its quota, and throttled_usec the time it spent waiting. Because the quota is per 100 milliseconds, a multithreaded container can be throttled while its average usage is far below its limit: four threads use a 500m quota in 12.5 milliseconds.

One Burstable pod, a weight and a ceiling
kubectl get pods -l app=web -o wide
NAME                   READY   STATUS    RESTARTS   AGE   IP           NODE         NOMINATED NODE   READINESS GATES
web-6784bfff78-dq87d   1/1     Running   0          30s   10.244.3.3   kx-worker3   <none>           <none>
web-6784bfff78-nshlv   1/1     Running   0          30s   10.244.2.2   kx-worker2   <none>           <none>
web-6784bfff78-pggpj   1/1     Running   0          30s   10.244.1.3   kx-worker    <none>           <none>
docker exec kx-worker3 cat /sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-burstable.slice/kubelet-kubepods-burstable-podc4116912_cd45_48d5_b692_28da4b996551.slice/cri-containerd-920a3b5f59ab7f42c97a11fe3324af9d824a4a4ba3079e3a9f055c223dad1fbd.scope/cpu.weight /sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-burstable.slice/kubelet-kubepods-burstable-podc4116912_cd45_48d5_b692_28da4b996551.slice/cri-containerd-920a3b5f59ab7f42c97a11fe3324af9d824a4a4ba3079e3a9f055c223dad1fbd.scope/cpu.max
17
50000 100000
docker exec kx-worker3 cat /sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-burstable.slice/kubelet-kubepods-burstable-podc4116912_cd45_48d5_b692_28da4b996551.slice/cpu.weight /sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-burstable.slice/kubelet-kubepods-burstable-podc4116912_cd45_48d5_b692_28da4b996551.slice/cpu.max
4
50000 100000
docker exec kx-worker3 cat /sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-burstable.slice/kubelet-kubepods-burstable-podc4116912_cd45_48d5_b692_28da4b996551.slice/cri-containerd-920a3b5f59ab7f42c97a11fe3324af9d824a4a4ba3079e3a9f055c223dad1fbd.scope/cpu.stat
usage_usec 100000
user_usec 13000
system_usec 87000
nice_usec 0
nr_periods 200
nr_throttled 6
throttled_usec 890004
nr_bursts 0
burst_usec 0

The web container requests 100m and is limited to 500m: its scope reads weight 17 from runc’s conversion and cpu.max of 50000 100000, while its pod slice reads weight 4 from systemd’s. Memory requests have no file here. By default the kernel never sees them; the scheduler uses them for placement and the kubelet for eviction order and for the OOM score below.

Who dies first

When memory runs out, in a cgroup or on the whole node, the kernel’s OOM killer picks a process by its oom_score, which is mostly the process’s memory use plus /proc/<pid>/oom_score_adj. The adjustment runs from -1000, never kill, to 1000, kill first, and the kubelet writes it for every container from its QoS class:

Processoom_score_adj
Guaranteed container-997
BestEffort container1000
Burstable container1000 - 1000 × memory request ÷ node memory, kept between the Guaranteed and BestEffort values
Any container in a system-node-critical pod-997, whatever its QoS class
A pod’s pause container-998
The kubelet and containerd-999

A sidecar, an init container with restartPolicy: Always, gets the lower of its own Burstable score and the score of the pod’s regular container with the smallest memory request, so the kernel never prefers a pod’s sidecar over the containers it serves. kube-proxy is BestEffort but writes -999 for itself, from its own oomScoreAdj setting.

Scores on kx-worker3
docker exec kx-worker3 sh -c 'cat /proc/$(pidof -s redis-server)/oom_score_adj'
-997
docker exec kx-worker3 sh -c 'cat /proc/$(pidof -s nginx)/oom_score_adj'
993
docker exec kx-worker3 sh -c 'cat /proc/$(pidof -s pause)/oom_score_adj'
-998
docker exec kx-worker3 sh -c 'cat /proc/$(pidof -s kube-proxy)/oom_score_adj'
-999
docker exec kx-worker3 sh -c 'cat /proc/$(pidof -s kubelet)/oom_score_adj'
-999

nginx reads 993: its 64Mi request is 7 thousandths of the node’s 8 GiB. Every process a container starts inherits the value, including a shell you open with kubectl exec.

The kernel kills, the kubelet evicts

Two different mechanisms end a container for memory, and they leave different evidence.

The kernel kills when a cgroup reaches its memory.max and reclaiming page cache inside it cannot free enough. The OOM killer acts within that cgroup only, and the kubelet sets memory.oom.group to 1 on each container’s scope, so the kernel kills every process in the container together rather than the largest alone; singleProcessOOMKill: true in the kubelet configuration turns that off. containerd reports the exit, code 137 for SIGKILL, and the kubelet records reason: OOMKilled in the container’s lastState and restarts it in the same pod: same name, same UID, same IP, a new container ID and so a new scope.

A container fills its limit
kubectl exec web-6784bfff78-dq87d -- sh -c 'tail /dev/zero'
command terminated with exit code 137
sleep 5
kubectl get pod web-6784bfff78-dq87d
NAME                   READY   STATUS    RESTARTS     AGE
web-6784bfff78-dq87d   1/1     Running   1 (4s ago)   35s
kubectl get pod web-6784bfff78-dq87d -o jsonpath='{.status.containerStatuses[0].lastState.terminated.reason} {.status.containerStatuses[0].lastState.terminated.exitCode}'
OOMKilled 137
docker exec kx-worker3 cat /sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-burstable.slice/kubelet-kubepods-burstable-podc4116912_cd45_48d5_b692_28da4b996551.slice/memory.events
low 0
high 0
max 38
oom 1
oom_kill 2
oom_group_kill 1
sock_throttled 0
docker exec kx-worker3 sh -c 'dmesg | grep constraint='
[304061.448082] oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=cri-containerd-920a3b5f59ab7f42c97a11fe3324af9d824a4a4ba3079e3a9f055c223dad1fbd.scope,mems_allowed=0,oom_memcg=/docker/c21018315dfc997aeb720fc4523f095159d61c61985da8de264c5274c62b518d/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-burstable.slice/kubelet-kubepods-burstable-podc4116912_cd45_48d5_b692_28da4b996551.slice,task_memcg=/docker/c21018315dfc997aeb720fc4523f095159d61c61985da8de264c5274c62b518d/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-burstable.slice/kubelet-kubepods-burstable-podc4116912_cd45_48d5_b692_28da4b996551.slice/cri-containerd-920a3b5f59ab7f42c97a11fe3324af9d824a4a4ba3079e3a9f055c223dad1fbd.scope,task=tail,pid=47061,uid=0
docker exec kx-worker3 systemd-cgls --no-pager /kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-burstable.slice/kubelet-kubepods-burstable-podc4116912_cd45_48d5_b692_28da4b996551.slice
CGroup /kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-burstable.slice/kubelet-kubepods-burstable-podc4116912_cd45_48d5_b692_28da4b996551.slice:
├─cri-containerd-63be22edee76e8b2e043a9e436907479989e3acde915dd46497b6d043868ea65.scope …
│ └─623 /pause
└─cri-containerd-8d80dcd4aff8dda6c9c6821d1baab4a5455ec7e83f612b9e02e8aaa9b233ea3d.scope …
  ├─918 nginx: master process nginx -g daemon off;
  ├─957 nginx: worker process
  ├─958 nginx: worker process
  ├─959 nginx: worker process
  └─960 nginx: worker process

tail /dev/zero reads a file with no newlines and keeps all of it, so it grows until the 128Mi limit stops it. The pod’s slice counts the event in memory.events: max is how often usage hit the limit, oom how often the OOM killer ran, oom_kill how many processes it killed, and oom_group_kill how many times it took a whole group. The kernel log names the cgroup: constraint=CONSTRAINT_MEMCG means a cgroup’s limit triggered the kill, and cpuset= names the old scope. systemd-cgls shows the pod’s slice with a new nginx scope beside the same pause scope, because the sandbox and its network namespace survived.

The kubelet evicts when the node, not the container, runs low: memory.available falls below a threshold such as the default 100Mi. The kubelet picks a pod by the ranking in QoS classes and node-pressure eviction, stops all its containers and sets the pod’s phase to Failed with reason Evicted. Nothing restarts it in place; its controller creates a replacement with a new name. If memory runs out faster than the kubelet notices, the kernel acts first, and its log then shows constraint=CONSTRAINT_NONE, an OOM kill on the node as a whole, decided by the scores above.

Press Play to watch the same kill: memory climbs to the limit in the pod’s cgroup, the kernel kills the container, and the kubelet starts a new one in the same pod.

kind cluster kx

Starting

Starting the cluster…

$

    A container fills its memory limit; the kernel kills it; the kubelet restarts it. Press Play: the scene runs its commands on this page's cluster, and the dock's terminal shows each one.

    Pressure before failure

    An OOM kill is the end of a long struggle. As a cgroup approaches memory.max, the kernel reclaims page cache, then dirty pages, and every allocation in the cgroup waits for that reclaim. Pressure stall information, PSI, measures the waiting. Each cgroup has a memory.pressure file with two lines:

    • some: the share of time in which at least one task in the cgroup was stalled on memory.
    • full: the share of time in which every non-idle task was stalled at once, so the cgroup made no progress at all.

    avg10, avg60 and avg300 are percentages over the last 10, 60 and 300 seconds, and total is the cumulative stall in microseconds. A container with a rising some is close to its limit and slower for it, well before memory.events counts a kill; that makes PSI the signal to alert on, rather than memory usage, which says nothing about whether the cgroup is struggling.

    Find an OOM kill in three places

    The button below runs the same allocating process in a web container on this lesson’s cluster. Afterwards, find the kill in the pod’s lastState, in its slice’s memory.events, and in dmesg on its node.

    Run a web container out of memory

    Each button runs its command on the cluster. This line follows what happens next.

    On your own cluster, save this Deployment as hog.yaml. It runs one nginx replica on kx-worker with a 64Mi limit:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: hog
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: hog
      template:
        metadata:
          labels:
            app: hog
        spec:
          nodeSelector:
            kubernetes.io/hostname: kx-worker
          containers:
          - name: hog
            image: nginx:1.30
            resources:
              requests:
                memory: 32Mi
              limits:
                memory: 64Mi

    Make a pod OOM and find the evidence

    1. kubectl --context kind-kx apply -f hog.yaml
    2. kubectl --context kind-kx rollout status deployment hog
    3. kubectl --context kind-kx exec deploy/hog -- sh -c 'tail /dev/zero'
    4. sleep 5
    5. kubectl --context kind-kx get pods -l app=hog
    6. kubectl --context kind-kx get pods -l app=hog -o jsonpath='{.items[0].status.containerStatuses[0].lastState.terminated.reason}'
    7. docker exec kx-worker sh -c 'dmesg | grep constraint='
    8. docker exec kx-worker crictl ps -a --name hog
    9. kubectl --context kind-kx delete deployment hog

    For your own kind cluster kx: copy a command and paste it into your terminal. Run in the simulator runs the same command in this page's simulated cluster instead; nothing on this page touches your machine. The exec ends with exit code 137. The pod keeps its name with one restart and OOMKilled in lastState, the kernel log on kx-worker names the killed container's scope with CONSTRAINT_MEMCG, and crictl lists the new container at attempt 1.

    Actual state

    Check your understanding

    5 checks

    Your first answer to each check is the one that counts. After a miss you can answer again; passing every check reconciles the lesson.

    1. Check 1 of 5

      A container requests 250m CPU with a limit of 500m. On an idle node, how much CPU can it use?

    2. Check 2 of 5

      Check runs your command on a private cluster; what it prints shows here.

    3. Check 3 of 5

      A Burstable container requests 1Gi of memory on a node with 8Gi. Roughly what oom_score_adj does the kubelet give it?

    4. Check 4 of 5

      A pod keeps its name, shows RESTARTS 1, and the node's kernel log has Memory cgroup out of memory with constraint=CONSTRAINT_MEMCG. What happened?

    5. Check 5 of 5

      A container's memory.pressure reads some avg10=30.00 and full avg10=5.00. What does that say?

    Lesson not yet reconciled

    Desired: 5 objectives, 0 metReconciled: 5 of 5 objectives met

    • Find a container's cgroup on a node and read the files its requests and limits became. (not yet met)
    • Explain why a CPU request is a weight and a CPU limit is a quota, and read throttling from cpu.stat. (not yet met)
    • Predict a container's oom_score_adj from its QoS class and memory request. (not yet met)
    • Find a kernel OOM kill in the pod's status, the cgroup's memory.events and the kernel log. (not yet met)
    • Read memory pressure from PSI before a container reaches its limit. (not yet met)

    Pass every check above to reconcile this lesson.

    Every check has passed, so the desired state is met. First-try accuracy: 0 of 5. This lesson's review cards are in your queue.

    Sources

    Checked against kubernetes.io, kernel.org and upstream source for Kubernetes 1.37 on .

    1. docs.kernel.org/admin-guide/cgroup-v2.html
    2. docs.kernel.org/accounting/psi.html
    3. kubernetes.io/docs/concepts/architecture/cgroups
    4. kubernetes.io/docs/concepts/workloads/pods/pod-qos
    5. kubernetes.io/docs/concepts/configuration/manage-resources-containers
    6. kubernetes.io/docs/concepts/scheduling-eviction/node-pressure-eviction
    7. kubernetes.io/docs/tasks/administer-cluster/kubelet-config-file
    8. kubernetes.io/docs/reference/config-api/kubelet-config.v1beta1
    9. github.com/kubernetes/kubernetes/blob/v1.37.0/pkg/kubelet/qos/policy.go
    10. github.com/kubernetes/kubernetes/blob/v1.37.0/pkg/kubelet/cm/cgroup_manager_linux.go
    11. github.com/opencontainers/cgroups/blob/v0.0.6/utils.go

    cgroups, memory pressure and the OOM killer

    ↓ ↑ step, 1 to 6 scene, Esc leave

    kind cluster kxLast command: Nothing run yet