Services
الهدف من الدرس
Section titled “الهدف من الدرس”ستتعلم لماذا نحتاج Service، وكيف تختار بين ClusterIP و NodePort و LoadBalancer، وكيف تشخص مشاكل selector و endpoints.
الفكرة ببساطة
Section titled “الفكرة ببساطة”Pods مؤقتة. Service يعطيك اسم DNS و IP ثابت داخل cluster، ثم يوجه traffic إلى Pods التي تطابق selector.
شكل الحركة
Section titled “شكل الحركة”flowchart LR C[Client Pod] --> DNS[web.default.svc.cluster.local] DNS --> S[Service web] S --> E[EndpointSlices] E --> P1[Pod web-1] E --> P2[Pod web-2]YAML كامل
Section titled “YAML كامل”استخدم Deployment من الدرس السابق، ثم أنشئ service.yaml:
apiVersion: v1kind: Servicemetadata: name: web namespace: default labels: app: webspec: type: ClusterIP selector: app: web ports: - name: http protocol: TCP port: 80 targetPort: httpشرح سريع:
| الجزء | المعنى |
|---|---|
type: ClusterIP | Service داخلي داخل cluster |
selector.app: web | يختار Pods التي عندها label app=web |
port | المنفذ الذي يتصل به العميل على Service |
targetPort | المنفذ داخل Pod أو اسم المنفذ |
أنواع Service
Section titled “أنواع Service”| النوع | متى أستخدمه؟ |
|---|---|
ClusterIP | اتصال داخلي بين التطبيقات داخل cluster |
NodePort | تجربة محلية أو بيئة بسيطة بدون load balancer |
LoadBalancer | تعريض خدمة مباشرة عبر cloud load balancer |
ExternalName | اسم داخلي يشير إلى DNS خارجي |
في Production غالبا تستخدم ClusterIP خلف Ingress أو Gateway، وليس NodePort مباشرة.
أوامر kubectl
Section titled “أوامر kubectl”kubectl apply -f deployment.yamlkubectl apply -f service.yamlkubectl get svc webkubectl get endpoints webkubectl get endpointslices -l kubernetes.io/service-name=webExpected output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEweb ClusterIP 10.96.120.10 <none> 80/TCP 20s
NAME ENDPOINTS AGEweb 10.244.0.10:80,10.244.0.11:80 20sاختبار Service من داخل cluster
Section titled “اختبار Service من داخل cluster”kubectl run curl --rm -it --image=curlimages/curl:8.10.1 --restart=Never -- curl -s http://webExpected output:
<!DOCTYPE html><html><head><title>Welcome to nginx!</title>تشخيص مشاكل Service
Section titled “تشخيص مشاكل Service”flowchart TD A[Service لا يعمل] --> B{هل endpoints موجودة؟} B -->|لا| C[راجع selector و labels] B -->|نعم| D{هل port صحيح؟} D -->|لا| E[راجع port و targetPort] D -->|نعم| F[افحص التطبيق و NetworkPolicy]| المشكلة | أمر التشخيص | الحل |
|---|---|---|
| لا توجد endpoints | kubectl get endpoints web | طابق selector مع labels الخاصة بالـ Pods |
| connection refused | kubectl describe svc web | تأكد من targetPort والمنفذ داخل container |
| DNS لا يعمل | kubectl exec داخل Pod ثم nslookup web | افحص CoreDNS و namespace |
| يعمل داخليا ولا يعمل خارجيا | kubectl get ingress أو kubectl get gateway | راجع Ingress/Gateway وليس Service فقط |
Lab: Service داخلي لتطبيق web
الهدف: إنشاء Service يوجه traffic إلى Deployment، ثم اختبار الاتصال من Pod مؤقت.
Prerequisites
Section titled “Prerequisites”- Deployment باسم
webيعمل بثلاث Pods. - labels في Pods تحتوي
app: web.
- احفظ YAML في
service.yaml. - طبق Service:
kubectl apply -f service.yamlkubectl get svc web- تأكد من endpoints:
kubectl get endpoints web- اختبر الاتصال:
kubectl run curl --rm -it --image=curlimages/curl:8.10.1 --restart=Never -- curl -s http://web- غيّر selector عمدا إلى
app: wrongوطبق الملف، ثم راقب endpoints:
kubectl apply -f service.yamlkubectl get endpoints web- أعد selector إلى
app: web.
Expected output
Section titled “Expected output”قبل الخطأ:
web 10.244.0.10:80,10.244.0.11:80,10.244.0.12:80بعد الخطأ:
web <none>Cleanup
Section titled “Cleanup”kubectl delete -f service.yamlالربط بالمشروع النهائي
Section titled “الربط بالمشروع النهائي”في كل المشاريع ستستخدم Services داخلية بين frontend و backend و database. Ingress سيقف أمام frontend أو API، أما Service فسيظل طبقة التوجيه الداخلية.