
الـ requests غير الـ limits: إزاي Kubernetes بيعمل schedule و throttle و OOM-kill للـ pods
- كاتب موثّق
محتويات المقال
limits: { cpu: "8", memory: 64Mi } على كل service "عشان الأمان"، على أساس إن الـ limit سقف وبس. وبعدين scale-up عادي بيسيب replica واقفة في Pending، مع إن الـ dashboards بتقول إن الـ node تقريبًا فاضي. وفي نفس الوقت فيه worker بيعمل restart كل كام ساعة بـ exit code 137، ومحدش رابط ده بالـ 64Mi.
الفكرة الغلط الشائعة
resources block، وبنفس الوحدات، والـ templates غالبًا بتدّيهم نفس القيم. و Kubernetes نفسه بيملا الـ request الناقص من الـ limit. وفي الـ memory، لما يبقوا متساويين، فعلًا بيتصرفوا كأنهم رقم واحد.
اللي بيحصل فعلًا
الـ requests للـ scheduler
الـ limits للـ kernel
الـ requests كمان بتحدد وزن وخط للـ eviction
الـ limit من غير request بيبقى هو الـ request
شوف ده على cluster
--context و -n عشان المساحة. الـ Deployment بيشغّل 3 replicas من busybox:1.37 بتقدّم HTTP. ودي الـ resources blocks بتاعة النسختين اللي عملناهم apply:
resources: limits: # meant as "at most", also becomes the request cpu: "8" memory: 64Miresources: requests: # what the scheduler reserves cpu: 250m memory: 64Mi limits: # what the kernel enforces memory: 64Mikubectl 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}'# Guaranteedkubectl apply -f api.yaml # requests, plus a memory limit# deployment.apps/api createdkubectl get pods# NAME READY STATUS RESTARTS AGE# api-5fd9498bd-5h4jg 1/1 Running 0 10s# api-5fd9498bd-psf42 1/1 Running 0 10s# api-5fd9498bd-zq7sl 1/1 Running 0 10skubectl get pod api-5fd9498bd-5h4jg -o jsonpath='{.status.qosClass}'# Burstablekubectl exec api-5fd9498bd-5h4jg -- cat /sys/fs/cgroup/cpu.weight# 10kubectl exec api-5fd9498bd-5h4jg -- cat /sys/fs/cgroup/cpu.max# max 100000kubectl exec api-5fd9498bd-5h4jg -- cat /sys/fs/cgroup/memory.max# 67108864cpu.weight، ومفيش سقف للـ CPU (max)، والـ memory limit بقى memory.max، يعني 64 MiB بالـ bytes.
tail /dev/zero، اللي بيفضل يخزّن على طول، تحت memory limit قدره 64Mi، و busy loop في الـ shell تحت CPU limit قدره 200m:
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 171cpu.weight | cpu.max و memory.max |
تطبقها إزاي في مشروعك
حط الـ requests من الاستهلاك الحقيقي الـ requests بتحدد كام replica يدخلوا على الـ nodes، فابنيها على الاستهلاك في الـ load العادي. لو عالية بتضيّع nodes وتسيب Pods في Pending، ولو واطية بتخسر وقت الزحمة وبتطلع الأول في الـ eviction.خلّي الـ memory limit زي الـ memory request في رأينا ده أأمن default: الاستهلاك ساعتها مايعدّيش الـ request، فالـ Pod مش هيبقى من أول اللي بيطلعوا في الـ eviction، وحد الـ OOM يبقى رقم انت اخترته. وحدد الاتنين من الـ peak اللي قسته. قرّر في الـ CPU limits عن قصد الـ CPU limit بيبدّل سرعة الـ bursts بالـ predictability: الـ busy loop اللي فوق خد 0.2 من core على node فيه 20 CPU. ومن غيره، الـ container يستخدم الـ CPU الفاضي، والـ requests بتحدد نصيب كل Pod وقت الزحمة. اتأكد من اللي خدته فعلًا شغّل 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 عن قصد.
التعليقات (0)