Overview
الـ ReplicaSet مسؤولة عن الحفاظ على عدد محدد من الـ Pods المطابقة للـ Pod template والـ Label Selector بتاعها.
لكن أحيانًا تلاقي إن عدد الـ Pods مش مطابق للعدد المطلوب، أو الـ ReplicaSet مش بتنشئ Pods جديدة، أو فيه Pods موجودة لكن مش بتتحكم فيها الـ ReplicaSet المتوقعة.
في الدرس ده هنتعلم إزاي نحدد السبب بدل ما نبدأ بتغيير الإعدادات عشوائيًا.
1. Understand the Reconciliation Process
الـ ReplicaSet بتقارن بين الـ Desired State والـ Actual State، وبتحاول توصل للحالة المطلوبة.
مثلًا، لو عندك:
spec.replicas: 3- عدد الـ Pods المطابقة للـ selector هو
2
الـ ReplicaSet بتحاول إنشاء Pod إضافية للوصول إلى العدد المطلوب.
ولو العدد الفعلي أكبر من المطلوب، بتحاول تقليله.
flowchart TD
A(["ReplicaSet Controller"]) --> B(["Read Desired Replica Count"])
B --> C(["Count Matching Pods"])
C --> D{"Actual = Desired?"}
D -- "No: Too Few" --> E(["Create Pods"])
D -- "No: Too Many" --> F(["Remove Excess Pods"])
D -- "Yes" --> G(["Maintain Current State"])
E --> C
F --> C
classDef controller fill:#e7d5bd,stroke:#9b7856,color:#382a20,stroke-width:1.5px
classDef process fill:#f5ebdc,stroke:#b69a78,color:#382a20,stroke-width:1.5px
classDef decision fill:#e7cda7,stroke:#9b7856,color:#382a20,stroke-width:1.5px
classDef action fill:#f0e2d8,stroke:#b88d79,color:#4b3026,stroke-width:1.5px
classDef success fill:#d9e5d0,stroke:#7c966e,color:#293524,stroke-width:1.5px
class A controller
class B,C process
class D decision
class E,F action
class G success
خلي بالك إن الـ ReplicaSet بتقيس عدد الـ Pods المطابقة للـ selector، مش عدد التطبيقات اللي شغالة بشكل سليم. ممكن تلاقي ثلاث Pods موجودة، لكن واحدة منها CrashLoopBackOff أو غير جاهزة لاستقبال traffic.
2. Start with the Actual State
قبل ما تغيّر أي إعدادات، افحص حالة الـ ReplicaSet والـ Pods.
kubectl get replicasets -A
لو عارف الـ Namespace:
kubectl get replicasets -n default
kubectl get pods -n default -o wide
اعرض تفاصيل ReplicaSet محددة:
kubectl describe replicaset <replicaset-name> \
-n <namespace>
ركز على المعلومات التالية:
Desired: عدد النسخ المطلوبة.Current: عدد النسخ الحالية.Ready: عدد النسخ الجاهزة.LabelsوSelector: الـ labels والـ selector المستخدمين.Events: الأحداث اللي ممكن توضح سبب الفشل.
ممكن كمان تعرض أعمدة النسخ بشكل مباشر:
kubectl get replicasets \
-n <namespace> \
-o custom-columns='NAME:.metadata.name,DESIRED:.spec.replicas,CURRENT:.status.replicas,READY:.status.readyReplicas'
لو قيمة READY فاضية أو 0، مش لازم يكون السبب هو الـ ReplicaSet نفسها. ممكن تكون الـ Pods غير جاهزة بسبب مشكلة في الـ image أو التطبيق أو الموارد.
3. Problem: The ReplicaSet Has Fewer Pods Than Expected
مثلًا، عندك ReplicaSet محتاجة ثلاث Pods، لكن الموجود فعليًا Pod واحدة فقط.
ابدأ بالتفاصيل:
kubectl describe replicaset <replicaset-name> \
-n <namespace>
وبعدين افحص الـ Pods:
kubectl get pods -n <namespace>
I. Common causes
Insufficient Resources
الـ scheduler مش لاقي Node عندها CPU أو Memory كفاية لتشغيل Pods جديدة.
Image Pull Failure
اسم الـ image غلط، أو الـ tag غير موجود، أو الوصول إلى الـ registry محتاج credentials.
Invalid Pod Configuration
الـ Pod template فيها إعداد غير صالح، أو الـ admission policy رفضت إنشاء الـ Pod.
Node Problems
الـ Nodes المتاحة مش قادرة تستقبل Pods جديدة بسبب حالتها أو القيود المفروضة عليها.
افحص الأحداث:
kubectl get events \
-n <namespace> \
--sort-by=.metadata.creationTimestamp
لو لقيت FailedScheduling، افحص رسالة الحدث. مثلًا:
Insufficient cpu: مفيش CPU كفاية حسب الـ requests.Insufficient memory: مفيش Memory كفاية حسب الـ requests.- رسائل عن
taintsأوnode affinity: ممكن يكون فيه قيود بتمنع الـ Pod من العمل على الـ Nodes المتاحة.
لو الـ Pod موجودة لكن الـ image مش بتتحمّل:
kubectl describe pod <pod-name> \
-n <namespace>
ابحث عن ErrImagePull أو ImagePullBackOff، ثم افحص الـ container logs لو الـ container بدأت بالفعل.
4. Problem: Pods Exist but Are Not Ready
ممكن تلاقي:
NAME DESIRED CURRENT READY
web-replicaset 3 3 1
ده معناه إن العدد الحالي وصل إلى ثلاث نسخ، لكن نسخة واحدة فقط جاهزة.
افحص حالة الـ Pods:
kubectl get pods -n <namespace>
حالات محتملة:
Pending: الـ Pod لم تُجدول بعد أو ما زالت تنتظر موارد أو متطلبات أخرى.ContainerCreating: الـ container لسه بتتجهز.CrashLoopBackOff: الـ container بتفشل وKubernetes بيحاول يشغّلها مجددًا.RunningمعREADY 0/1: الـ container شغالة، لكن الـ Pod مش جاهزة بالكامل.
لما تلاقي Pod فيها مشكلة:
kubectl describe pod <pod-name> \
-n <namespace>
kubectl logs <pod-name> \
-n <namespace>
ولو الـ container بتعمل restart:
kubectl logs <pod-name> \
-n <namespace> \
--previous
الأمر الأخير مفيد لفحص logs الخاصة بآخر container instance انتهت.
5. Problem: Incorrect Labels and Selectors
الـ Label Selector هو اللي بيحدد أي Pods بتطابق الـ ReplicaSet.
مثال على ReplicaSet بتختار Pods عندها:
selector:
matchLabels:
app: web
لازم الـ Pod template تحتوي على الـ label المطابقة:
template:
metadata:
labels:
app: web
لو الـ selector بتطلب app: web لكن الـ template فيها app: api، هيحصل عدم تطابق، والـ API server هيرفض إنشاء الـ ReplicaSet لو الـ selector مش بتطابق الـ labels الموجودة في الـ template.
مثال صحيح:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: web-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.28
افحص الـ selector الفعلية:
kubectl get replicaset web-replicaset \
-n <namespace> \
-o yaml
اعرض الـ Pods المطابقة للـ label:
kubectl get pods \
-n <namespace> \
-l app=web
ولو محتاج تعرض الـ labels الموجودة على كل Pods:
kubectl get pods \
-n <namespace> \
--show-labels
I. Important selector behavior
- الـ selector لازم تطابق الـ labels الموجودة في الـ Pod template.
- الـ selector بتحدد الـ Pods اللي الـ ReplicaSet بتديرها.
- تغيير labels على Pod ممكن يؤثر على مطابقتها للـ selector.
- الـ selectors في
apps/v1الخاصة بالـ ReplicaSet غير قابلة للتغيير بعد الإنشاء.
لو اكتشفت إن الـ selector نفسها غلط، غالبًا هتحتاج إنشاء ReplicaSet جديدة بالإعداد الصحيح بدل محاولة تعديل الـ selector الحالية.
6. Problem: A ReplicaSet Is Managing Unexpected Pods
دي مشكلة مختلفة عن عدم تطابق الـ labels في الـ template.
لو الـ ReplicaSet عندها selector عام جدًا، مثل app=web، ممكن تتطابق مع Pods أخرى في نفس الـ Namespace عندها الـ label دي، حتى لو اتعملت من مصدر مختلف.
افحص الـ selector:
kubectl describe replicaset <replicaset-name> \
-n <namespace>
ثم اعرض الـ Pods المطابقة:
kubectl get pods \
-n <namespace> \
-l app=web \
--show-labels
ولفحص الـ owner references على Pod معينة:
kubectl get pod <pod-name> \
-n <namespace> \
-o yaml
راجع metadata.ownerReferences لمعرفة الـ owner المسجل للـ Pod، لو موجود.
II. Why ownership matters
الـ label matching والـ ownership مش نفس الحاجة. الـ selector بتحدد المطابقة، بينما الـ ownerReferences بتوضح علاقة الملكية المسجلة بين الموارد.
لو فيه Pod مستقلة بتستخدم نفس الـ labels، متفترضش إن تعديل الـ labels أو حذف الـ Pod هو الحل الصحيح. الأول افهم مين أنشأها، وهل هي مملوكة لـ ReplicaSet أو Deployment أو controller آخر.
7. Problem: Too Many Pods or Unexpected Pods
لو العدد الفعلي أكبر من العدد المطلوب، افحص أولًا هل كل الـ Pods فعلًا بتطابق الـ selector، وهل فيه ReplicaSets أخرى أو controllers بتنشئ Pods إضافية.
kubectl get replicasets,pods \
-n <namespace> \
-o wide
افحص الـ ReplicaSet:
kubectl describe replicaset <replicaset-name> \
-n <namespace>
مهم تميّز بين الحالات التالية:
- Pods managed by the same ReplicaSet: الـ ReplicaSet بتحاول الحفاظ على العدد المطلوب من الـ Pods المطابقة اللي بتديرها.
- Pods managed by another controller: ممكن تكون تابعة لـ ReplicaSet أو Deployment مختلفة.
- Standalone Pods: ممكن تكون أنشئت يدويًا ومش مملوكة لأي controller.
- Terminating Pods: ممكن تفضل ظاهرة أثناء عملية الحذف، وبالتالي عدد الـ Pods الظاهر لحظيًا مش دايمًا بيمثل عدد النسخ الجاهزة.
متستخدمش حذف الـ Pods كحل أول لمجرد إن العدد الظاهر أكبر من المتوقع. حدّد مصدر كل Pod الأول.
8. Problem: The ReplicaSet Cannot Be Created
لو أمر kubectl apply فشل، افحص رسالة الخطأ نفسها.
kubectl apply -f replicaset.yaml
أسباب شائعة:
- YAML syntax غير صحيح.
apiVersionأوkindغير مناسب.selectorلا تطابق labels الموجودة فيtemplate.- الـ
selectorبتحاول استخدام إعداد غير صالح. - قيود من admission policies أو صلاحيات RBAC بتمنع العملية.
تقدر تتحقق من الـ manifest قبل تطبيقه:
kubectl apply \
--dry-run=server \
-f replicaset.yaml
الـ server-side dry run بيطلب من الـ API server التحقق من الطلب من غير حفظ التغيير، لكنه مش ضمان إن كل الـ Pods هتشتغل بنجاح بعد التطبيق.
لو الخطأ متعلق بالصلاحيات، افحصها باستخدام:
kubectl auth can-i create replicasets \
-n <namespace>
ولفحص القدرة على إنشاء Pods:
kubectl auth can-i create pods \
-n <namespace>
الأمرين بيفحصوا صلاحيات المستخدم الحالي حسب إعدادات الوصول الفعالة.
9. Hands-on Lab: Diagnose a ReplicaSet
هننشئ ReplicaSet فيها ثلاث Pods، ونفحص الـ selector والعدد الفعلي والـ ownership.
Create a Namespace
kubectl create namespace rs-troubleshooting-labCreate a ReplicaSet
احفظ الملف التالي باسم
replicaset.yaml:apiVersion: apps/v1 kind: ReplicaSet metadata: name: web-replicaset namespace: rs-troubleshooting-lab spec: replicas: 3 selector: matchLabels: app: web template: metadata: labels: app: web spec: containers: - name: nginx image: nginx:1.28 ports: - containerPort: 80طبّق الملف:
kubectl apply -f replicaset.yamlافحص النتيجة:
kubectl get replicaset,pods \ -n rs-troubleshooting-lab \ --show-labelsالمفروض تلاقي ReplicaSet مطلوبة منها ثلاث Pods، وبعد اكتمال التشغيل تكون النسخ الثلاث جاهزة.
Inspect the Selector
kubectl describe replicaset web-replicaset \ -n rs-troubleshooting-labاعرض الـ Pods المطابقة:
kubectl get pods \ -n rs-troubleshooting-lab \ -l app=webقارن الـ labels بالـ selector الموجود في الـ ReplicaSet.
Delete a Pod and Observe Self-Healing
احذف واحدة من الـ Pods:
kubectl delete pod <pod-name> \ -n rs-troubleshooting-labراقب الـ Pods:
kubectl get pods \ -n rs-troubleshooting-lab \ -wالمفروض الـ ReplicaSet تلاحظ إن عدد النسخ المطابقة أقل من المطلوب، وتحاول إنشاء Pod بديلة.
اخرج من المتابعة باستخدام
Ctrl+Cبعد ملاحظة النتيجة.Inspect Ownership
اختار Pod جديدة أو موجودة:
kubectl get pod <pod-name> \ -n rs-troubleshooting-lab \ -o jsonpath='{.metadata.ownerReferences[*].kind}{"\n"}{.metadata.ownerReferences[*].name}{"\n"}'لو الـ Pod تابعة مباشرة للـ ReplicaSet، هتلاقي الـ owner kind هو
ReplicaSetواسم الـ owner هوweb-replicaset.Clean Up
kubectl delete namespace rs-troubleshooting-labده بيحذف الـ Namespace والموارد اللي بداخلها، فاستخدمه فقط بعد ما تنتهي من اللاب.
10. Troubleshooting Checklist
لما تواجه مشكلة في ReplicaSet، امشِ بالترتيب ده:
DESIRED, CURRENT, وREADY.FailedScheduling، أو مشاكل الـ image، أو رفض إنشاء الموارد.Summary
الـ ReplicaSet بتحافظ على عدد من الـ Pods المطابقة للـ selector، لكنها مش مسؤولة عن ضمان سلامة التطبيق من كل النواحي.
أهم نقاط التشخيص:
DESIREDوCURRENTوREADYبتوضح الفرق بين العدد المطلوب والحالي والجاهز.- الـ Events بتساعد في اكتشاف مشاكل الـ scheduling وإنشاء الـ Pods.
- الـ labels والـ selectors بتحدد الـ Pods المطابقة، بينما الـ owner references بتوضح الملكية المسجلة.
describeوgetوlogsأدوات أساسية لفهم المشكلة.- لو الـ Pods مش جاهزة، افحص سبب الفشل بدل افتراض إن المشكلة في الـ ReplicaSet نفسها.
في الدرس التالي هننتقل إلى Deployment Troubleshooting، ونستخدم نفس أسلوب التشخيص لكن على مستوى الـ Deployment والـ rollout والـ ReplicaSets المرتبطة بيه.