Overview
الـ Service في Kubernetes بيوفّر نقطة اتصال مستقرة لمجموعة من الـ Pods، لكن وجود الـ Service لوحده مش معناه إن التطبيق قابل للوصول.
ممكن الـ Service يكون موجود، والـ Pods تكون Running، ومع ذلك الاتصال يفشل بسبب selector غلط، أو targetPort غير صحيح، أو مشكلة في DNS أو الشبكة.
الهدف هنا إنك تتبع مسار الاتصال خطوة بخطوة بدل ما تغيّر إعدادات عشوائيًا.
1. Understand the Request Path
لما تطبيق يحاول يتصل بـ Service داخل الـ Cluster، فيه أكتر من مرحلة لازم تشتغل صح:
flowchart TD
A(["Client Pod"]) --> B(["DNS Resolution"])
B --> C(["Service Name and Port"])
C --> D(["Service Routing"])
D --> E(["EndpointSlices"])
E --> F(["Ready Pod Endpoint"])
F --> G(["Application Process"])
B -.-> H(["DNS Failure"])
C -.-> I(["Wrong Name or Port"])
D -.-> J(["Selector or Routing Issue"])
E -.-> K(["Missing Endpoints"])
G -.-> L(["Application or Target Port Issue"])
classDef client fill:#eee4d4,stroke:#9b8265,color:#392f25,stroke-width:1.5px
classDef routing fill:#e3d2b9,stroke:#8c7152,color:#30251b,stroke-width:1.5px
classDef endpoint fill:#d9c4a5,stroke:#856747,color:#30251b,stroke-width:1.5px
classDef failure fill:#f1dfdc,stroke:#b56e62,color:#572c27,stroke-width:1.5px
class A,B client
class C,D routing
class E,F,G endpoint
class H,I,J,K,L failure
المخطط بيوضّح إن المشكلة ممكن تكون في أي مرحلة. عشان كده لازم نحدد هل الفشل في DNS resolution، ولا الوصول للـ Service، ولا الوصول للـ Pod نفسه، ولا التطبيق اللي جوّاه.
Service
نقطة الاتصال المستقرة اللي بيستخدمها الـ Client للوصول للتطبيق.
EndpointSlices
بتعرض عناوين الـ endpoints المرتبطة بالـ Service، وبتساعدك تعرف هل فيه Pods مستهدفة أصلًا.
Application
حتى لو الـ Service والـ endpoints مضبوطين، لازم التطبيق يكون شغّال وبيستمع على المنفذ الصحيح.
2. Start with Basic Inspection
ابدأ بجمع المعلومات الأساسية قبل ما تعدّل أي إعداد.
kubectl get services -n default
kubectl describe service web-service -n default
kubectl get pods -n default -o wide --show-labels
ركّز على:
TYPE: نوع الـ Service.CLUSTER-IP: عنوانه الداخلي، لو كان له Cluster IP.PORT(S): المنافذ المعرّضة من الـ Service.SELECTOR: الـ labels اللي بيستخدمها لاختيار الـ Pods.Endpoints: ممكن تظهر في تفاصيل الـ Service، حسب إصدار وأدوات Kubernetes.- Pod status و
READY: هل الـ Pods جاهزة لاستقبال الترافيك؟
بعد كده افحص الـ EndpointSlices، لأنها الواجهة الحديثة الأساسية لعرض endpoints الخاصة بالـ Service.
kubectl get endpointslices \
-n default \
-l kubernetes.io/service-name=web-service
ولفحص تفاصيلها:
kubectl describe endpointslice \
-n default \
-l kubernetes.io/service-name=web-service
ممكن كمان تستخدم kubectl get endpoints كطريقة قديمة للفحص، لكن الأفضل تتعوّد على EndpointSlices.
3. Troubleshoot Common Service Issues
I. Case 1: Service Has No Endpoints
دي من أشهر المشاكل: الـ Service موجود، لكن مفيش endpoints متاحة للتوجيه.
افحص الـ selector:
kubectl get service web-service \
-n default \
-o jsonpath='{.spec.selector}{"\n"}'
وافحص labels الخاصة بالـ Pods:
kubectl get pods \
-n default \
--show-labels
مثلًا، لو الـ Service عنده:
selector:
app: web
لكن الـ Pods عليها:
labels:
app: nginx
مش هيحصل match، وبالتالي الـ Service مش هيختار الـ Pods دي.
كمان اتأكد إن الـ Pods موجودة في نفس الـ namespace؛ الـ Service العادي بيختار Pods من الـ namespace بتاعه، مش من namespace تاني.
بعد التأكد من الـ selector، افحص حالة الـ Pods:
kubectl get pods -n default
kubectl describe pods -n default
الأسباب المحتملة تشمل:
- مفيش Pods مطابقة للـ selector.
- الـ Pods اتعملت في namespace مختلف.
- الـ Pods مش موجودة أو اتعمل لها deletion.
- الـ readiness checks مش ناجحة، وبالتالي الـ endpoints ممكن تكون غير جاهزة لاستقبال الترافيك.
- الـ Service معمول من غير selector، وده ممكن يكون مقصودًا لو الـ endpoints بتتدار يدويًا أو بآلية أخرى.
II. Case 2: Service Selects the Wrong Pods
أحيانًا الـ Service بيكون عنده endpoints، لكن endpoints دي بتشير لتطبيق غير المقصود.
افحص الـ selector والـ labels جنب بعض:
kubectl get service web-service \
-n default \
-o yaml
kubectl get pods \
-n default \
-L app,environment
مثال على selector واسع زيادة عن اللزوم:
selector:
environment: production
لو عندك أكتر من تطبيق عليه label environment: production، ممكن الـ Service يختار Pods من تطبيقات مختلفة.
الأفضل استخدام labels بتحدد هوية التطبيق بوضوح، مثل:
selector:
app: web
وتأكد إن الـ labels دي موجودة على الـ Pod template في الـ Deployment، مش بس على الـ Deployment object نفسه.
III. Case 3: Service Exists but Connection Fails
لو الـ Service عنده endpoints لكن الاتصال بيرجع Connection refused أو بيعمل timeout، افحص المنافذ أولًا.
kubectl get service web-service \
-n default \
-o yaml
kubectl get deployment web \
-n default \
-o yaml
افهم الفرق بين المنافذ دي:
| Field | وظيفته |
|---|---|
port |
المنفذ اللي الـ Client بيستخدمه على الـ Service. |
targetPort |
المنفذ اللي الـ Service بيوجّه الترافيك ليه على الـ Pod. |
containerPort |
معلومة عن المنفذ المعلن في تعريف الـ Container؛ مش أمر لتشغيل التطبيق أو فتح المنفذ. |
مثال:
ports:
- port: 80
targetPort: 8080
ده معناه إن الـ Client يتصل بالـ Service على المنفذ 80، والـ Service يوجّه الترافيك للمنفذ 8080 على الـ Pod.
لو التطبيق فعليًا بيستمع على 3000، لازم إعداد targetPort يعكس المنفذ الصحيح.
كمان افحص الآتي:
- هل التطبيق بيستمع على المنفذ المتوقع؟
- هل بيستمع على عنوان شبكة الـ Pod، مثل
0.0.0.0، ولا على127.0.0.1فقط؟ - هل البروتوكول صحيح، مثل TCP أو UDP؟
- هل الـ Pod جاهز؟
- هل فيه
NetworkPolicyبتمنع الاتصال؟ - هل الـ CNI أو مسار الشبكة في الـ Cluster فيه مشكلة؟
وجود containerPort: 8080 في الـ manifest مش معناه إن التطبيق بدأ يسمع على 8080 تلقائيًا.
IV. Case 4: DNS Lookup Fails
لو الاتصال باستخدام اسم الـ Service مش شغال، اختبر الـ DNS من داخل الـ Cluster.
اسم Service في نفس الـ namespace غالبًا بيكون:
web-service
ولو الـ Client في namespace مختلف، استخدم اسم الـ Service مع الـ namespace:
web-service.default
والاسم الكامل المعتاد:
web-service.default.svc.cluster.local
cluster.local هو الـ default الشائع، لكن اسم الـ cluster domain ممكن يختلف حسب إعدادات البيئة.
شغّل اختبار DNS من Pod مؤقت:
kubectl run dns-test \
-n default \
--image=busybox:1.36 \
--restart=Never \
--rm -it \
-- nslookup web-service.default.svc.cluster.local
لو الـ DNS فشل:
-
راجع اسم الـ Service والـ namespace.
-
اختبر الـ DNS باستخدام الاسم الكامل.
-
افحص حالة CoreDNS:
kubectl get pods -n kube-system -
افحص logs الخاصة بـ CoreDNS عند الحاجة:
kubectl logs \ -n kube-system \ -l k8s-app=kube-dns \ --tail=100الـ label وطريقة نشر CoreDNS ممكن يختلفوا بين توزيعات Kubernetes؛ لو الـ selector مش موجود، افحص أسماء الـ Pods مباشرة.
-
افحص هل فيه
NetworkPolicyبتمنع DNS traffic، خصوصًا UDP/TCP على المنفذ53.
مهم: نجاح DNS مش دليل على إن التطبيق قابل للوصول؛ DNS بيحل الاسم لعنوان، لكنه مش بيضمن نجاح الاتصال بعد كده.
V. Case 5: Service Works Internally but Not Externally
لو الـ Service شغال من داخل الـ Cluster لكن مش متاح من خارجها، افحص نوعه وطريقة النشر.
| Service Type | طريقة الوصول |
|---|---|
ClusterIP |
داخليًا داخل الـ Cluster بشكل افتراضي. |
NodePort |
عبر عنوان Node والمنفذ المخصّص، بشرط إن الشبكة والـ firewall يسمحوا بالوصول. |
LoadBalancer |
يحتاج دعمًا أو تكاملًا مناسبًا من الـ platform أو مزوّد الـ cloud. |
ExternalName |
بيرجع DNS CNAME لاسم خارجي؛ مش بيعمل proxy للترافيك. |
افحص نوع الـ Service:
kubectl get service web-service \
-n default \
-o wide
لو ClusterIP، عدم إمكانية الوصول المباشر من الإنترنت سلوك متوقع. حسب احتياجك، ممكن تستخدم Ingress مع Ingress Controller، أو LoadBalancer، أو NodePort في البيئات المناسبة.
لو النوع LoadBalancer، افحص حالة الـ Service:
kubectl describe service web-service -n default
وجود EXTERNAL-IP في حالة Pending ممكن يشير إن الـ platform مش بتوفّر LoadBalancer integration أو إن فيه مشكلة في تخصيصه.
كمان افحص الـ external firewall، والـ cloud security rules، والـ routing، وإعدادات الـ Ingress Controller لو مستخدمه.
VI. Case 6: Headless Service Behaves Differently
الـ Headless Service بيتعرّف عادةً بالشكل ده:
spec:
clusterIP: None
في الحالة دي، Kubernetes مش بيوفّر Cluster IP افتراضيًا للـ Service. بدلًا من كده، DNS ممكن يرجّع عناوين الـ endpoints نفسها.
ده سلوك مقصود في حالات زي StatefulSets أو التطبيقات اللي محتاجة تتعامل مع عناوين الـ Pods مباشرة.
لو متوقع عنوان Cluster IP ثابت للـ Service، راجع هل هو Headless بالفعل. ولو الـ DNS مش بيرجّع العناوين المتوقعة، افحص الـ selectors والـ EndpointSlices والـ readiness، وكمان نوع الـ Service وطريقة إعداد DNS.
VII. Case 7: ExternalName Does Not Forward Traffic
الـ ExternalName بيعمل mapping من اسم Service داخل Kubernetes إلى اسم DNS خارجي باستخدام CNAME.
مثلًا:
apiVersion: v1
kind: Service
metadata:
name: external-api
namespace: default
spec:
type: ExternalName
externalName: api.example.com
ده مش معناه إن Kubernetes بيعمل proxy للطلبات إلى api.example.com.
لو DNS resolution ناجح لكن الاتصال فاشل، افحص:
- هل الاسم الخارجي بيتحل بشكل صحيح؟
- هل الـ Client يقدر يوصل للوجهة الخارجية؟
- هل الـ TLS certificate واسم الـ host صحيحين؟
- هل إعدادات الـ HTTP
Hostheader مناسبة للتطبيق الخارجي؟
4. Test Each Layer Separately
عشان تفصل بين مشاكل DNS ومشاكل الاتصال، نفّذ اختبارات مستقلة من داخل الـ Cluster.
اختبر DNS:
kubectl run curl-test \
-n default \
--image=curlimages/curl:8.12.1 \
--restart=Never \
--rm -it \
-- curl -v http://web-service.default.svc.cluster.local/
اختبر الـ Service بالاسم القصير لو الـ Pod المؤقت في نفس الـ namespace:
kubectl run curl-test \
-n default \
--image=curlimages/curl:8.12.1 \
--restart=Never \
--rm -it \
-- curl -v http://web-service/
لما تستخدم curl -v، لاحظ المرحلة اللي فشل عندها الاتصال:
- فشل حل الاسم: راجع DNS.
Connection refused: راجع المنفذ والتطبيق المستمع.- Timeout: راجع الشبكة، و
NetworkPolicy، والـ routing. - HTTP
4xxأو5xx: الاتصال وصل إلى HTTP server، لكن محتاج تفحص استجابة التطبيق والـ URL والـ host headers.
النتائج دي مؤشرات تشخيصية وليست قواعد مطلقة؛ تفاصيل الخطأ بتعتمد على التطبيق والـ network implementation.
5. Hands-on Lab: Diagnose an Empty Service
في اللاب ده هننشئ Service شغال، وبعد كده هنكسر الـ selector عمدًا ونصلحه.
Step 1: Create the Workload
kubectl create namespace service-troubleshooting-lab
kubectl create deployment web \
-n service-troubleshooting-lab \
--image=nginx:1.28 \
--replicas=2
kubectl rollout status deployment/web \
-n service-troubleshooting-lab
اتأكد من الـ labels اللي اتعملت:
kubectl get deployment web \
-n service-troubleshooting-lab \
-o jsonpath='{.spec.template.metadata.labels}{"\n"}'
الـ Deployment اللي اتعمل باستخدام kubectl create deployment web بيستخدم عادةً label app=web.
Step 2: Create the Service
kubectl expose deployment web \
-n service-troubleshooting-lab \
--name=web-service \
--port=80 \
--target-port=80
افحص الـ Service والـ endpoints:
kubectl get service web-service \
-n service-troubleshooting-lab
kubectl get endpointslices \
-n service-troubleshooting-lab \
-l kubernetes.io/service-name=web-service
kubectl get pods \
-n service-troubleshooting-lab \
--show-labels
Step 3: Verify Connectivity
kubectl run curl-test \
-n service-troubleshooting-lab \
--image=curlimages/curl:8.12.1 \
--restart=Never \
--rm -it \
-- curl -v http://web-service/
المفروض يرجع NGINX response طالما الـ Pods جاهزة والـ cluster networking شغالة.
Step 4: Introduce a Selector Failure
غيّر الـ selector عمدًا لقيمة مش موجودة على الـ Pods:
kubectl patch service web-service \
-n service-troubleshooting-lab \
--type=merge \
-p '{"spec":{"selector":{"app":"wrong"}}}'
افحص الـ EndpointSlices:
kubectl get endpointslices \
-n service-troubleshooting-lab \
-l kubernetes.io/service-name=web-service
ممكن تلاقي الـ EndpointSlice موجود لكن من غير endpoints فعالة، أو تشوف تفاصيل مختلفة حسب إصدار Kubernetes وطريقة إدارة الـ EndpointSlices. المهم إن الـ Service مش هيلاقي Pods مطابقة للـ selector الجديد.
جرّب الاتصال تاني:
kubectl run curl-test \
-n service-troubleshooting-lab \
--image=curlimages/curl:8.12.1 \
--restart=Never \
--rm -it \
-- curl --max-time 5 -v http://web-service/
الاختبار ممكن ينتهي بـ timeout بدلًا من Connection refused؛ النتيجة الدقيقة بتعتمد على سلوك الـ Service proxy والـ network implementation.
Step 5: Restore the Correct Selector
kubectl patch service web-service \
-n service-troubleshooting-lab \
--type=merge \
-p '{"spec":{"selector":{"app":"web"}}}'
اتأكد إن الـ endpoints رجعت:
kubectl get endpointslices \
-n service-troubleshooting-lab \
-l kubernetes.io/service-name=web-service
وبعدين أعد اختبار الاتصال:
kubectl run curl-test \
-n service-troubleshooting-lab \
--image=curlimages/curl:8.12.1 \
--restart=Never \
--rm -it \
-- curl -v http://web-service/
Step 6: Clean Up
kubectl delete namespace service-troubleshooting-lab
6. Troubleshooting Decision Tree
flowchart TD
A(["Service Request Fails"]) --> B(["Inspect Service and Pods"])
B --> C{"Matching Endpoints?"}
C -->|No| D(["Check Selector and Labels"])
D --> E(["Check Namespace and Pod Readiness"])
C -->|Yes| F{"DNS Resolves?"}
F -->|No| G(["Check Service Name and CoreDNS"])
F -->|Yes| H{"Connection Established?"}
H -->|No| I(["Check targetPort and Listener"])
I --> J(["Check NetworkPolicy and CNI"])
H -->|Yes| K{"External Access Required?"}
K -->|No| L(["Inspect Application Response"])
K -->|Yes| M(["Check Service Type and External Network"])
classDef start fill:#eee4d4,stroke:#9b8265,color:#392f25,stroke-width:1.5px
classDef check fill:#e3d2b9,stroke:#8c7152,color:#30251b,stroke-width:1.5px
classDef action fill:#d9c4a5,stroke:#856747,color:#30251b,stroke-width:1.5px
classDef failure fill:#f1dfdc,stroke:#b56e62,color:#572c27,stroke-width:1.5px
class A start
class B,C,F,H,K check
class D,E,G,I,J,L,M action
7. Troubleshooting Checklist
قبل ما تعتبر المشكلة اتحلت، راجع النقاط دي:
- الـ Service موجود في الـ namespace الصحيح.
- الـ selector بيطابق labels الخاصة بالـ Pods المطلوبة فقط.
- الـ Pods موجودة و
Ready. - الـ EndpointSlices بتعرض endpoints المتوقعة.
-
portوtargetPortوالبروتوكول مضبوطين. - التطبيق بيستمع على العنوان والمنفذ الصحيحين.
- DNS بيحل اسم الـ Service الصحيح.
- اختبرت الاتصال من داخل الـ Cluster.
- راجعت
NetworkPolicyوالـ CNI عند وجود مؤشرات على مشكلة شبكية. - لو الاتصال خارجي، راجعت Service type والـ Ingress أو LoadBalancer والـ firewall.
8. Best Practices
Inspect Before Changing
اجمع حالة الـ Service والـ Pods والـ EndpointSlices قبل ما تعدّل الـ manifests.
Use Specific Labels
استخدم selectors واضحة تمنع الـ Service من اختيار Pods لتطبيقات غير مقصودة.
Test Layer by Layer
افصل اختبار DNS عن اختبار TCP/HTTP وعن التحقق من استجابة التطبيق.
Prefer Declarative Fixes
بعد ما تحدد السبب، عدّل ملفات الـ manifests أو مصدر الـ GitOps عشان الإصلاح يفضل ثابتًا.
Summary
الـ Service troubleshooting بيعتمد على تتبّع الاتصال من الاسم لحد التطبيق:
- افحص الـ Service والـ Pods والـ labels.
- تحقق من الـ EndpointSlices والـ Pod readiness.
- راجع
portوtargetPortوالتطبيق المستمع. - اختبر DNS والاتصال من داخل الـ Cluster كلٌّ على حدة.
- افحص NetworkPolicy وCoreDNS والـ CNI عند وجود دليل على مشكلة شبكية.
- لو المشكلة خارجية، راجع طريقة نشر الخدمة والـ firewall وتكامل الـ platform.
القاعدة الأساسية: وجود Service لا يضمن وجود endpoints، ووجود endpoints لا يضمن نجاح الاتصال بالتطبيق. كل مرحلة محتاجة تحقق مستقل.