Ingress و Gateway API
الهدف من الدرس
Section titled “الهدف من الدرس”ستتعلم كيف يصل المستخدم من خارج cluster إلى Service داخلي، وكيف تكتب Ingress كامل، ومتى تبدأ التفكير في Gateway API.
الفكرة ببساطة
Section titled “الفكرة ببساطة”Service من نوع ClusterIP يعمل داخل cluster. Ingress يضيف layer HTTP أمام Services حتى توجه traffic حسب host أو path، وغالبا يضيف TLS.
مسار الطلب
Section titled “مسار الطلب”flowchart LR U[Browser] --> LB[Load Balancer] LB --> IC[Ingress Controller] IC --> I[Ingress rules] I --> S[Service ClusterIP] S --> P[Pods]YAML كامل: Service و Ingress
Section titled “YAML كامل: Service و Ingress”service.yaml:
apiVersion: v1kind: Servicemetadata: name: web labels: app: webspec: type: ClusterIP selector: app: web ports: - name: http port: 80 targetPort: httpingress.yaml:
apiVersion: networking.k8s.io/v1kind: Ingressmetadata: name: web annotations: nginx.ingress.kubernetes.io/rewrite-target: /spec: ingressClassName: nginx rules: - host: web.localtest.me http: paths: - path: / pathType: Prefix backend: service: name: web port: name: httpاختبار محلي
Section titled “اختبار محلي”لو تستخدم minikube:
minikube addons enable ingresskubectl apply -f deployment.yamlkubectl apply -f service.yamlkubectl apply -f ingress.yamlkubectl get ingress webcurl -H "Host: web.localtest.me" http://$(minikube ip)لو تستخدم kind، غالبا تحتاج تثبيت NGINX Ingress Controller الخاص بـ kind ثم تستخدم localhost حسب إعداداتك.
Expected output:
NAME CLASS HOSTS ADDRESS PORTS AGEweb nginx web.localtest.me 192.168.49.2 80 1mTLS بشكل مختصر
Section titled “TLS بشكل مختصر”Ingress يدعم TLS بهذا الشكل:
spec: tls: - hosts: - web.example.com secretName: web-tls rules: - host: web.example.com http: paths: - path: / pathType: Prefix backend: service: name: web port: name: httpفي Production لا تنشئ certificate يدويا غالبا. استخدم cert-manager مع ClusterIssuer حتى يصدر ويجدد certificates تلقائيا.
Gateway API
Section titled “Gateway API”Gateway API هو الجيل الأحدث من APIs الخاصة بالـ networking. يفصل بين:
| المفهوم | الدور |
|---|---|
GatewayClass | نوع controller أو provider |
Gateway | نقطة دخول traffic |
HTTPRoute | قواعد توجيه HTTP |
مثال مختصر:
apiVersion: gateway.networking.k8s.io/v1kind: HTTPRoutemetadata: name: webspec: parentRefs: - name: public-gateway hostnames: - web.example.com rules: - backendRefs: - name: web port: 80متى أستخدم ماذا؟
Section titled “متى أستخدم ماذا؟”| الحالة | الاختيار العملي |
|---|---|
| تطبيق بسيط وcontroller موجود | Ingress |
| منصة كبيرة تحتاج delegation بين teams | Gateway API |
| TCP/UDP خاص أو service mesh | Gateway API أو controller-specific CRDs |
| تجربة محلية سريعة | Ingress مع NGINX أو Traefik |
أخطاء شائعة
Section titled “أخطاء شائعة”| المشكلة | التشخيص | الحل |
|---|---|---|
| Ingress بلا ADDRESS | kubectl get ingress | ثبت Ingress Controller أو راجع ingressClassName |
| 404 من controller | kubectl describe ingress web | راجع host و path |
| 502/503 | kubectl get endpoints web | Service لا يملك endpoints أو Pod غير ready |
| TLS لا يعمل | kubectl describe certificate | راجع cert-manager و Secret و DNS |
Lab: تعريض Nginx عبر Ingress
الهدف: إنشاء Service و Ingress، ثم اختبار routing وتشخيص endpoints.
Prerequisites
Section titled “Prerequisites”- Deployment باسم
webيعمل. - Ingress Controller مثبت.
- طبق Service و Ingress:
kubectl apply -f service.yamlkubectl apply -f ingress.yaml- افحص Ingress:
kubectl get ingress webkubectl describe ingress web- اختبر الطلب:
curl -H "Host: web.localtest.me" http://127.0.0.1- لو فشل الطلب:
kubectl get endpoints webkubectl logs -n ingress-nginx -l app.kubernetes.io/component=controller --tail=50Expected output
Section titled “Expected output”<title>Welcome to nginx!</title>Cleanup
Section titled “Cleanup”kubectl delete -f ingress.yamlkubectl delete -f service.yamlالربط بالمشروع النهائي
Section titled “الربط بالمشروع النهائي”Project 4 سيجمع Ingress و TLS و NetworkPolicy. هذا هو أول شكل قريب من دخول traffic في بيئة Production.