ankitiitians / ankitiitians

0 stars 0 forks source link

πŸ“š Kubernetes Gateway API #91

Open ankitiitians opened 3 months ago

ankitiitians commented 3 months ago

Kubernetes Gateway API

Yeh guide hai Kubernetes Gateway API ke baare mein, jo ek modern aur flexible tareeka hai ingress aur traffic routing manage karne ka Kubernetes mein. Hum NGINX Gateway Controller use karenge demo ke liye, lekin concepts aur APIs universal hain, aur kisi bhi Gateway API-compatible controller ke saath kaam karte hain.

πŸ“š Official Docs: gateway-api.sigs.k8s.io


1. Gateway API Install Karna with NGINX πŸ› οΈ

Gateway API custom resources define karta hai, lekin unhe implement karne ke liye ek controller chahiye. Hum NGINX Gateway Controller use kar rahe hain jo saare standard Gateway API resources support karta hai.

Commands to install NGINX Gateway Controller:

kubectl kustomize "https://github.com/nginx/nginx-gateway-fabric/config/crd/gateway-api/standard?ref=v1.6.2" | kubectl apply -f -
kubectl kustomize "https://github.com/nginx/nginx-gateway-fabric/config/crd/gateway-api/experimental?ref=v1.6.2" | kubectl apply -f -
helm install ngf oci://ghcr.io/nginx/charts/nginx-gateway-fabric --create-namespace -n nginx-gateway

Kya hota hai yeh?

πŸ”— Reference: NGINX Gateway Fabric Docs


2. GatewayClass: Blueprint for Gateways πŸ—‚οΈ

GatewayClass ek template hai jo batata hai kaunsa controller Gateways manage karega. Isko aise samjho:

Example:

apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
  name: nginx
spec:
  controllerName: nginx.org/gateway-controller

Samjho:

πŸ”— Docs: GatewayClass Reference


3. HTTP Gateway aur Listener Setup πŸ“‘

Gateway resource define karta hai ki traffic kaise cluster mein aayega – protocols, ports, aur routing rules ke saath.

Example for HTTP Gateway:

apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: nginx-gateway
  namespace: default
spec:
  gatewayClassName: nginx
  listeners:
  - name: http
    protocol: HTTP
    port: 80
    allowedRoutes:
      namespaces:
        from: All

Explanation:

Kaam: Yeh HTTP traffic ko port 80 par handle karta hai aur backend services ko forward karta hai.


4. HTTP Routing: Traffic Ko Direct Karna πŸ›€οΈ

HTTPRoute batata hai ki HTTP traffic kaise specific services tak jayega. Yeh Gateway ke saath milke kaam karta hai.

Example:

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: basic-route
  namespace: default
spec:
  parentRefs:
  - name: nginx-gateway
  rules:
  - matches:
    - path:
        type: PathPrefix
        value: /app
    backendRefs:
    - name: my-app
      port: 80

Kya hai yeh?

Kaam: /app se start hone wale requests my-app service tak jayenge.

πŸ”— Docs: HTTP Routing Guide


5. HTTP Redirects aur Rewrites πŸ”„

Redirects aur Rewrites se requests ko modify kar sakte ho before they reach backend.

HTTP to HTTPS Redirect

Force HTTP traffic to HTTPS for security.

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: https-redirect
  namespace: default
spec:
  parentRefs:
  - name: nginx-gateway
  rules:
  - filters:
    - type: RequestRedirect
      requestRedirect:
        scheme: https

Kaise kaam karta hai?

πŸ”— Docs: Redirects Guide

Path Rewrite

Request ka path change karo before forwarding.

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: rewrite-path
  namespace: default
spec:
  parentRefs:
  - name: nginx-gateway
  rules:
  - matches:
    - path:
        type: PathPrefix
        value: /old
    filters:
    - type: URLRewrite
      urlRewrite:
        path:
          replacePrefixMatch: /new
    backendRefs:
    - name: my-app
      port: 80

Kya hota hai?

πŸ”— Docs: Rewrite Guide


6. HTTP Header Modification 🏷️

Headers ko add, set, ya remove kar sakte ho requests ya responses mein.

Example:

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: header-mod
  namespace: default
spec:
  parentRefs:
  - name: nginx-gateway
  rules:
  - filters:
    - type: RequestHeaderModifier
      requestHeaderModifier:
        add:
          x-env: staging
    backendRefs:
    - name: my-app
      port: 80

Kaise kaam karta hai?

Use: Environment-specific metadata add karne ke liye useful hai.

πŸ”— Docs: Header Guide


7. HTTP Traffic Splitting: Divide and Rule 🚦

Traffic ko multiple backend services mein split kar sakte ho, jaise canary deployments ya A/B testing ke liye.

Example:

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: traffic-split
  namespace: default
spec:
  parentRefs:
  - name: nginx-gateway
  rules:
  - backendRefs:
    - name: v1-service
      port: 80
      weight: 80
    - name: v2-service
      port: 80
      weight: 20

Kya hota hai?

Use: New features test karne ya rollout ke liye perfect hai.

πŸ”— Docs: Traffic Splitting Guide


8. HTTP Request Mirroring πŸͺž

Requests ki copy ek secondary service ko bhej sakte ho for testing ya analysis, bina primary service ko affect kiye.

Example:

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: request-mirror
  namespace: default
spec:
  parentRefs:
  - name: nginx-gateway
  rules:
  - filters:
    - type: RequestMirror
      requestMirror:
        backendRef:
          name: mirror-service
          port: 80
    backendRefs:
    - name: my-app
      port: 80

Kaise kaam karta hai?

Use: New services test karne ya traffic analyze karne ke liye useful.

πŸ”— Docs: Request Mirroring Guide


9. TLS Configuration: Secure Karo πŸ”’

TLS se traffic encrypt hota hai, ensuring secure communication. Gateway level par TLS terminate kar sakte ho using a Kubernetes Secret.

Example for TLS Termination:

apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: nginx-gateway-tls
  namespace: default
spec:
  gatewayClassName: nginx
  listeners:
  - name: https
    protocol: HTTPS
    port: 443
    tls:
      mode: Terminate
      certificateRefs:
      - kind: Secret
        name: tls-secret
    allowedRoutes:
      namespaces:
        from: All

Explanation:

Kaam: Secure communication ke liye, backend services ko unencrypted traffic milta hai.

πŸ”— Docs: TLS Guide


10. TCP, UDP, aur Other Protocols 🌐

Gateway API sirf HTTP nahi, TCP, UDP, aur gRPC jaise protocols bhi support karta hai, making it versatile for databases, DNS, microservices, etc.

TCP Example (Databases ke liye)

apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: tcp-gateway
  namespace: default
spec:
  gatewayClassName: nginx
  listeners:
  - name: tcp
    protocol: TCP
    port: 3306
    allowedRoutes:
      namespaces:
        from: All

Kya hai?

UDP Example (DNS ya Streaming ke liye)

apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: udp-gateway
  namespace: default
spec:
  gatewayClassName: nginx
  listeners:
  - name: udp
    protocol: UDP
    port: 53
    allowedRoutes:
      namespaces:
        from: All

Kya hai?

gRPC Example (Microservices ke liye)

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: grpc-route
  namespace: default
spec:
  parentRefs:
  - name: nginx-gateway
  rules:
  - matches:
    - method:
        service: my.grpc.Service
        method: GetData
    backendRefs:
    - name: grpc-service
      port: 50051

Kya hai?


Conclusion: Kyun Use Karein? 🎯

Kubernetes Gateway API ek powerful aur structured tareeka hai traffic manage karne ka. Features jaise header rewrites, traffic splits, aur protocol flexibility make it super expressive. Start with HTTP basics, then move to advanced protocols like TLS, TCP, ya gRPC. Yeh ensure karta hai smooth, secure, aur scalable ingress strategy for your Kubernetes clusters.

Ab try karo aur apne cluster ko next level par le jao! πŸš€

ankitiitians commented 3 months ago

Kubernetes-CKA-0800-Networking.pdf

ankitiitians commented 3 months ago

Ingress.pdf