الانتقال إلى المحتوى
DevOps والـ CloudKubernetes· 9 دقائق للقراءة

الـ requests غير الـ limits: إزاي Kubernetes بيعمل schedule و throttle و OOM-kill للـ pods

الـ requests حجز الـ scheduler بيجمعه، والـ limits سقف الـ kernel بيفرضه: throttling للـ CPU و OOM-kill للـ memory.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

فريق بيحط limits: { cpu: "8", memory: 64Mi } على كل service "عشان الأمان"، على أساس إن الـ limit سقف وبس. وبعدين scale-up عادي بيسيب replica واقفة في Pending، مع إن الـ dashboards بتقول إن الـ node تقريبًا فاضي. وفي نفس الوقت فيه worker بيعمل restart كل كام ساعة بـ exit code 137، ومحدش رابط ده بالـ 64Mi.

الفكرة الغلط الشائعة

الفكرة بتقول إن الـ requests والـ limits اسمين لنفس السقف. الـ container "بياخد" الرقم اللي بتكتبه، فتحط واحد منهم، أو الاتنين بنفس القيمة، وتكمّل.

وده سهل تفهم جاي منين. الاتنين جنب بعض في نفس الـ resources block، وبنفس الوحدات، والـ templates غالبًا بتدّيهم نفس القيم. و Kubernetes نفسه بيملا الـ request الناقص من الـ limit. وفي الـ memory، لما يبقوا متساويين، فعلًا بيتصرفوا كأنهم رقم واحد.

النص الصح في الكلام إنهم ممكن ياخدوا نفس القيمة، بس كل واحد بيقراه component مختلف، في وقت مختلف، عشان قرار مختلف.

اللي بيحصل فعلًا

الـ requests للـ scheduler

الـ docs بتقولها بوضوح: "the kube-scheduler uses this information to decide which node to place the Pod on." وهو بيتأكد إن مجموع الـ requests على الـ node أقل من الـ capacity بتاعته، والاستهلاك الحقيقي مالوش دعوة: "although actual memory or CPU resource usage on nodes is very low, the scheduler still refuses to place a Pod on a node if the capacity check fails." والـ request مش سقف كمان: "possible (and allowed) for a container to use more resource than its request" لما الـ node يكون فيه مكان.

الـ limits للـ kernel

الـ limits "are applied by the kubelet (and container runtime), and are ultimately enforced by the kernel"، عن طريق الـ على Linux. والاتنين بيفشلوا بشكل مختلف. في الـ CPU، الـ limit هو "a hard ceiling on how much CPU time the container can use"، ولما يخلص "the kernel waits before allowing that cgroup to resume execution." وده اسمه ، والـ docs بتضيف إن الـ runtimes "don't terminate Pods or containers for excessive CPU usage." أما في الـ memory، فلو عدّيت الـ limit، "the Linux kernel out-of-memory subsystem" بيتدخل، وغالبًا بـ "stopping one of the processes in the container that tried to allocate memory."

الـ requests كمان بتحدد وزن وخط للـ eviction

الـ CPU request "typically defines a weighting": لما الـ containers تتنافس، "workloads with larger CPU requests are allocated more CPU time." ولما الـ node يقصر في الـ memory، الـ kubelet بيطلّع الأول "BestEffort or Burstable pods where the usage exceeds requests." والـ بتلخّص موقف الـ Pod: Guaranteed لما الـ requests بتاعة الـ CPU والـ memory تساوي الـ limits في كل container، و BestEffort لو مفيش أي حاجة، و Burstable في الباقي.

الـ limit من غير request بيبقى هو الـ request

"If you specify a CPU limit for a Container but do not specify a CPU request, Kubernetes automatically assigns a CPU request that matches the limit"، ونفس الكلام في الـ memory. فالـ limit اللي قصدك بيه "8 CPUs بالكتير" بيتحوّل لـ "احجزولي 8 CPUs على الـ node".

شوف ده على cluster

شغّلنا ده على الـ Kubernetes بتاع Docker Desktop (الـ server نسخة v1.34.3، والـ kubectl نسخة v1.34.1)، على node واحد فيه 20 CPU متاحين، في namespace خاص بينا مفيهوش LimitRange، وشلنا --context و -n عشان المساحة. الـ Deployment بيشغّل 3 replicas من busybox:1.37 بتقدّم HTTP. ودي الـ resources blocks بتاعة النسختين اللي عملناهم apply:

YAML api.yaml
resources:  limits:          # meant as "at most", also becomes the request    cpu: "8"    memory: 64Mi
Bash terminal
kubectl apply -f api.yaml   # 3 replicas, limits onlykubectl get pods# NAME                   READY   STATUS    RESTARTS   AGE# api-5b99d6d69d-8gpp2   1/1     Running   0          8s# api-5b99d6d69d-h5hs7   0/1     Pending   0          8s# api-5b99d6d69d-k92w7   1/1     Running   0          8skubectl get pod api-5b99d6d69d-8gpp2 \  -o jsonpath='{.spec.containers[0].resources.requests}'# {"cpu":"8","memory":"64Mi"}kubectl get pod api-5b99d6d69d-8gpp2 -o jsonpath='{.status.qosClass}'# Guaranteed

في الـ tab الأولى، replicas اتنين حجزوا 16 CPU من الـ node، والتالتة ملقتش مكان، والـ event بتاعها قال "0/1 nodes are available: 1 Insufficient cpu." والـ requests المنسوخة خلّت الـ Pods كمان Guaranteed، أحسن QoS class، على capacity مالمسوهاش. وفي التانية، التلاتة دخلوا. وجوه الـ container تشوف كل رقم راح فين: الـ request بقى cpu.weight، ومفيش سقف للـ CPU (max)، والـ memory limit بقى memory.max، يعني 64 MiB بالـ bytes.

وبعدين ادّينا Pod اتنين شغلانة بتعدّي الـ limits: tail /dev/zero، اللي بيفضل يخزّن على طول، تحت memory limit قدره 64Mi، و busy loop في الـ shell تحت CPU limit قدره 200m:

Bash terminal
kubectl apply -f leak.yaml   # tail /dev/zero, memory limit 64Mikubectl get pod leak# NAME   READY   STATUS      RESTARTS     AGE# leak   0/1     OOMKilled   1 (8s ago)   8skubectl get pod leak -o jsonpath='{..lastState.terminated.reason}'# OOMKilledkubectl apply -f spin.yaml   # busy loop, cpu limit 200mkubectl exec spin -- cat /sys/fs/cgroup/cpu.max# 20000 100000kubectl exec spin -- \  grep -E "usage_usec|nr_periods|nr_throttled" /sys/fs/cgroup/cpu.stat# usage_usec 1359686# nr_periods 67# nr_throttled 67# (the same command, 10 seconds later)# usage_usec 3430017# nr_periods 171# nr_throttled 171

الـ leak اتقتل في ثواني بـ exit code 137 وعمل restart. أما الـ busy loop فعمره ما اتقتل. الـ quota بتاعته 20 ms من الـ CPU في كل period طولها 100 ms، واتعمله throttle في كل period فيهم: في الـ 104 periods اللي بين القرايتين خد حوالي 2.07 ثانية CPU، يعني 0.2 من core تقريبًا بالظبط.

الـ Requestالـ Limit
مين بيقراهالـ scheduler، وبعدين الـ kernel كـ وزن للـ CPUالـ kubelet والـ runtime، والـ kernel بيفرضه
الـ CPUمحجوز على node، ونصيبك وقت الزحمةسقف، وفوقه الـ container بيتعمله throttle
الـ Memoryمحجوز على node، وخط الـ evictionسقف، وفوقه الـ container بيتعمله OOM-kill
لو ماكتبتوشبيتنسخ من الـ limit، لو موجودمفيش سقف، إلا لو LimitRange حط واحد
في الـ cgroupcpu.weightcpu.max و memory.max

تطبقها إزاي في مشروعك

  1. حط الـ requests من الاستهلاك الحقيقيالـ requests بتحدد كام replica يدخلوا على الـ nodes، فابنيها على الاستهلاك في الـ load العادي. لو عالية بتضيّع nodes وتسيب Pods في Pending، ولو واطية بتخسر وقت الزحمة وبتطلع الأول في الـ eviction.
  2. خلّي الـ memory limit زي الـ memory requestفي رأينا ده أأمن default: الاستهلاك ساعتها مايعدّيش الـ request، فالـ Pod مش هيبقى من أول اللي بيطلعوا في الـ eviction، وحد الـ OOM يبقى رقم انت اخترته. وحدد الاتنين من الـ peak اللي قسته.
  3. قرّر في الـ CPU limits عن قصدالـ CPU limit بيبدّل سرعة الـ bursts بالـ predictability: الـ busy loop اللي فوق خد 0.2 من core على node فيه 20 CPU. ومن غيره، الـ container يستخدم الـ CPU الفاضي، والـ requests بتحدد نصيب كل Pod وقت الزحمة.
  4. اتأكد من اللي خدته فعلًاشغّل kubectl get pod <name> -o jsonpath='{.status.qosClass}'، وبص على "Allocated resources" في kubectl describe node. وممكن LimitRange في الـ namespace يكون ضاف limits ماكتبتهاش.

الخلاصة

  • الـ requests حجز للـ scheduling، والـ limits سقف الـ kernel بيفرضه.
  • الـ scheduler بيجمع الـ requests مش الاستهلاك، فالـ requests الكبيرة بتسيب Pods في Pending على nodes فاضية.
  • فوق الـ CPU limit الـ container بيتعمله throttle، وفوق الـ memory limit بيتعمله OOM-kill بـ exit code 137.
  • الـ limit من غير request بيبقى هو الـ request، وده ممكن يحجز أكتر بكتير من اللي قصدته.
  • حدد الـ memory request والـ limit من الـ peak اللي قسته، واختار الـ CPU limits عن قصد.

📚 المصادر

  1. Resource Management for Pods and Containers — Kubernetes Documentation
  2. Pod Quality of Service Classes — Kubernetes Documentation
  3. Assign CPU Resources to Containers and Pods — Kubernetes Documentation
  4. Node-pressure Eviction — Kubernetes Documentation

التعليقات (0)