Pods
الهدف من الدرس
Section titled “الهدف من الدرس”بنهاية هذا الدرس ستعرف كيف تنشئ Pod، تفحص حالته، تقرأ logs، تدخل إلى container، وتفهم لماذا لا نعتمد على Pod وحده في Production.
الفكرة ببساطة
Section titled “الفكرة ببساطة”Pod هو أصغر وحدة تشغيل في Kubernetes. غالبا يحتوي container واحد، وأحيانا يحتوي أكثر من container لو كانوا يحتاجون نفس الشبكة ونفس التخزين المؤقت.
مثال واقعي
Section titled “مثال واقعي”لو عندك تطبيق API بسيط، فإن Kubernetes يشغله داخل Pod. لو مات هذا Pod، Deployment ينشئ Pod جديد. ولو تغير اسم Pod، Service يظل يعطيك عنوان ثابت للتطبيق.
flowchart LR U[User] --> S[Service] S --> P1[Pod api-1] S --> P2[Pod api-2] D[Deployment] --> P1 D --> P2YAML كامل
Section titled “YAML كامل”أنشئ ملف pod.yaml:
apiVersion: v1kind: Podmetadata: name: hello-pod namespace: default labels: app: hello tier: webspec: containers: - name: web image: nginx:1.27 ports: - name: http containerPort: 80 resources: requests: cpu: "50m" memory: "64Mi" limits: cpu: "250m" memory: "128Mi"شرح سريع:
| الجزء | المعنى |
|---|---|
apiVersion | إصدار API المستخدم لهذا النوع من الموارد |
kind | نوع المورد، هنا Pod |
metadata.name | اسم Pod داخل namespace |
labels | علامات تساعد Service و commands على اختيار Pod |
containers | قائمة containers التي تعمل داخل Pod |
resources | أقل موارد مطلوبة وحد أقصى مسموح |
أوامر kubectl
Section titled “أوامر kubectl”kubectl apply -f pod.yamlkubectl get pods -l app=hellokubectl describe pod hello-podkubectl logs hello-podkubectl exec -it hello-pod -- shExpected output:
pod/hello-pod createdNAME READY STATUS RESTARTS AGEhello-pod 1/1 Running 0 10sلو دخلت إلى container، اختبر Nginx:
wget -qO- http://127.0.0.1exitExpected output مختصر:
<!DOCTYPE html><html><head><title>Welcome to nginx!</title>كيف تقرأ حالة Pod؟
Section titled “كيف تقرأ حالة Pod؟”| الحالة | المعنى | أول أمر للتشخيص |
|---|---|---|
Pending | scheduler لم يجد Node مناسب أو image pull لم يبدأ | kubectl describe pod hello-pod |
Running | Pod يعمل | kubectl logs hello-pod |
CrashLoopBackOff | container يبدأ ثم يقع مرارا | kubectl logs hello-pod --previous |
ImagePullBackOff | Kubernetes لا يستطيع سحب image | kubectl describe pod hello-pod |
ContainerCreating | runtime يجهز container أو volume | kubectl describe pod hello-pod |
أخطاء شائعة
Section titled “أخطاء شائعة”| الخطأ | السبب المحتمل | الحل |
|---|---|---|
ImagePullBackOff | اسم image خطأ أو registry يحتاج صلاحيات | صحح image أو أضف imagePullSecrets |
CrashLoopBackOff | الأمر داخل container يفشل | اقرأ logs --previous وعدل command أو config |
| Pod لا يستقبل traffic | لا يوجد Service أو label selector غير مطابق | راجع labels والـ Service selector |
| Pod يظل Pending | resources requests أكبر من المتاح | قلل requests أو أضف Node |
Lab: تشغيل وفحص أول Pod
الهدف: إنشاء Pod من Nginx، فحص حالته، قراءة logs، ثم تنظيف الموارد.
Prerequisites
Section titled “Prerequisites”- Cluster محلي يعمل، مثل kind أو minikube.
kubectl get nodesيرجع Node واحد على الأقل بحالةReady.
- احفظ YAML السابق في ملف
pod.yaml. - طبق الملف:
kubectl apply -f pod.yaml- راقب الحالة:
kubectl get pod hello-pod -w- افتح نافذة terminal أخرى ونفذ:
kubectl describe pod hello-podkubectl logs hello-podkubectl exec -it hello-pod -- sh- من داخل container:
wget -qO- http://127.0.0.1 | headexitExpected output
Section titled “Expected output”NAME READY STATUS RESTARTS AGEhello-pod 1/1 Running 0 20sCleanup
Section titled “Cleanup”kubectl delete -f pod.yamlkubectl get pods -l app=helloExpected output:
No resources found in default namespace.الربط بالمشروع النهائي
Section titled “الربط بالمشروع النهائي”في Project 1 ستشغل Nginx، لكنك لن تستخدم Pod مباشر. ستستخدم Deployment لأن هدفنا ليس فقط تشغيل container، بل تشغيل تطبيق يمكن تحديثه واستعادته.