Skip to content

Latest commit

 

History

History
833 lines (591 loc) · 30.6 KB

File metadata and controls

833 lines (591 loc) · 30.6 KB

An API Gateway is a server (or service) that acts as a single entry point for all client requests to your backend services (like microservices, databases, legacy systems, etc.). Think of it as a traffic manager or doorman for APIs—it handles incoming API calls, routes them to the correct backend services, applies policies, and sends the response back to the client.


🚪 Simple Analogy:

If your backend has 20 different microservices, instead of exposing all 20 endpoints directly to users (which is messy and insecure), you expose one API Gateway. The API Gateway figures out where to forward the request, handles security, and combines responses if needed.


⚙️ What an API Gateway does (Responsibilities)

Feature Description
Request Routing Forwards incoming requests to the correct backend service
Authentication & Authorization Validates tokens (e.g., OAuth, JWT), API keys, and ensures the client is allowed to access
Rate Limiting & Throttling Prevents abuse by limiting how many requests a client can make
Load Balancing Distributes requests among multiple backend instances
Caching Stores responses to reduce load and improve performance
Request & Response Transformation Modifies request or response format (e.g., from JSON to XML)
Monitoring & Analytics Tracks API usage, logs requests, and helps in debugging
Security Protects backend services (e.g., SSL termination, IP whitelisting, data masking)
API Versioning Allows clients to access different versions of APIs

📊 Why is API Gateway used?

  1. Single entry point → Simplifies communication between clients and multiple backend services.
  2. Centralized security → You don’t have to implement auth, logging, rate-limiting in each service.
  3. Decouples clients from services → Backend services can evolve (change URLs, merge, split) without impacting clients directly.
  4. Improves performance → Through caching, request batching, and load balancing.

🏗️ Example Workflow (API Gateway flow)

Client → API Gateway → Microservice 1
                       → Microservice 2
                       → Database
                       → External APIs
  • Client sends a request to /user-profile
  • API Gateway authenticates the client and routes the request to UserService
  • UserService fetches data and sends it back to API Gateway
  • API Gateway returns the response to the client

🌍 Popular API Gateway Tools

Tool Type
Apigee Managed API Gateway (by Google)
AWS API Gateway Managed (AWS)
Kong Open-source / Enterprise
Nginx Can be configured as a gateway/reverse proxy
Zuul (Netflix) Java-based
Istio Service mesh with gateway capabilities
Azure API Management Managed (Azure)

API Gateway vs Load Balancer vs Reverse Proxy (Quick view)

Feature API Gateway Load Balancer Reverse Proxy
Focus API management Distribute load Forward requests
Handles auth, rate limiting, etc.? ✅ Yes ❌ No ❌ No
Understands API-level routing? ✅ Yes ❌ No ❌ Limited
Example Apigee, AWS API Gateway Nginx LB, AWS ELB Nginx, HAProxy

📌 In short

API Gateway = Centralized manager and router for APIs, handling security, routing, throttling, analytics, and more — simplifying client-to-microservice interactions.


🏢 Typical Microservices Architecture (without API Gateway)

Clients → Multiple microservices

Problems:

  • Each microservice exposes its own endpoint

  • Clients have to know URLs of all services

  • Each microservice has to implement:

    • Authentication
    • Rate limiting
    • Caching
    • Monitoring → Duplication, Complexity, Hard to maintain

With API Gateway (Recommended Approach)

Clients → API Gateway → Microservices

Now clients interact only with API Gateway. The gateway handles the "messy details" — clean, secure, and scalable!


📊 Typical Company Use Case Example (E-commerce)

Architecture:

Mobile/Web App → API Gateway → 
                    → UserService
                    → ProductService
                    → OrderService
                    → PaymentService

Responsibilities of API Gateway in this setup

Function Example
Routing /users/123 → UserService
/products/567 → ProductService
Authentication Validates JWT token before forwarding request
Rate Limiting Limits users to 1000 requests/day
Caching Caches /products list responses for 5 min
Logging & Monitoring Logs API usage for analytics/debugging
Transformations Converts XML backend response to JSON if client expects JSON
Aggregation Combines responses from multiple microservices into one

📦 How does it work in practice? (Example Flow)

🛒 Scenario: User opens the app to view their profile and orders.

Without API Gateway

  • Client sends request to UserService → Authenticate
  • Client sends request to OrderService → Authenticate again ✖️ Too many direct calls & duplicate logic

With API Gateway

Client → API Gateway
Step What happens
1 Client sends GET /my-dashboard
2 API Gateway authenticates JWT token
3 API Gateway routes request:
→ Calls UserService /users/123
→ Calls OrderService /orders/user/123
4 API Gateway aggregates both responses
5 API Gateway sends single combined response to client

✅ Cleaner ✅ Secure ✅ Faster (fewer round-trips)


🏆 Benefits for companies

Benefit Why it matters
Simplified client communication Clients don’t need to know 10+ microservice URLs
Centralized security Easier to enforce auth, rate limits at one place
Flexible backend evolution Backend services can change without breaking clients
Faster delivery Common API tasks handled by gateway — less work for dev teams
Improved monitoring Company can analyze API usage patterns easily

🔥 Real-World Examples

Company Gateway Used Example
Netflix Zuul, then migrated to Spring Cloud Gateway Routes billions of API requests/day
Amazon Custom internal gateway (like AWS API Gateway) Exposes services to mobile apps securely
Airbnb Nginx + custom gateway Centralized API routing & auth
Google Apigee API management for internal & external developers

⚠️ Things companies consider when choosing API Gateway

  1. Open source vs Managed service (e.g., Kong vs Apigee)
  2. Cost Managed gateways (like Apigee) can be expensive
  3. Performance & Scalability High-traffic systems need performant gateways
  4. Extensibility Custom plugins, custom auth policies, etc.
  5. Integration with existing infra Does it work with Kubernetes, cloud provider, etc.?

Summary

An API Gateway makes microservices architectures scalable, secure, and maintainable — By centralizing routing, security, and API management, companies reduce complexity for clients and backend teams.


Perfect — let’s see how API Gateway + Kubernetes works together! This is very relevant because modern companies deploy their microservices in Kubernetes clusters. I’ll explain step-by-step, show architecture, and share real-world examples. Let’s go 🚀


🛠️ Kubernetes Microservices Setup (without API Gateway)

Clients → Exposed Kubernetes Services (via Ingress)

Problems:

  • Each microservice (deployed as a Pod) needs its own Ingress rule
  • Exposes too many services to outside world
  • No centralized auth, rate limiting, or monitoring

With API Gateway (Recommended)

We deploy API Gateway inside Kubernetes as a Pod (or multiple Pods) API Gateway sits in front of all microservices


🏗️ Architecture Diagram

Client
   │
   ▼
[API Gateway Pod(s)]  (Ingress + Gateway Pod exposed to internet)
   │
   ├── UserService (K8s Pod + ClusterIP Service)
   ├── ProductService (K8s Pod + ClusterIP Service)
   ├── OrderService (K8s Pod + ClusterIP Service)
   └── PaymentService (K8s Pod + ClusterIP Service)

⚙️ How does this work? (Step-by-step)

Step Description
1 API Gateway (like Kong, Nginx, Ambassador, Istio Ingress Gateway) is deployed inside Kubernetes
2 API Gateway exposes one public endpoint using Kubernetes Ingress or LoadBalancer
3 All external traffic hits API Gateway Pod first
4 Gateway authenticates client, applies rate limits, logs request
5 Gateway forwards request internally to Kubernetes services (via ClusterIP)
6 Gateway aggregates/compresses/caches response (optional)
7 Gateway returns response to client

🚀 Real-world Example (E-commerce app on Kubernetes)

Component Deployment
API Gateway Kong Gateway deployed as K8s Deployment
Exposed via LoadBalancer Service
UserService Deployed as user-deployment.yaml with ClusterIP Service
ProductService Deployed as product-deployment.yaml with ClusterIP Service
API Routing Kong routes /users, /products, /orders internally to services
Security Kong uses JWT auth plugin to authenticate users
Rate Limiting Kong rate limits /products API to 100 req/min

🌍 Popular API Gateways with Kubernetes support

API Gateway How it integrates with K8s
Kong Gateway Helm charts, CRDs, native K8s Ingress
NGINX NGINX Ingress Controller (acts as API Gateway + Ingress)
Istio Gateway Envoy proxy based, advanced traffic management
Ambassador Built specifically for K8s, CRDs for API management
AWS API Gateway Can route to services in Amazon EKS (Kubernetes)

🏆 Benefits of API Gateway in Kubernetes

Benefit Why it matters
Single public entry point Only API Gateway is exposed — rest of services private
Centralized security No need to add auth/rate limit on each service
Service discovery Automatically routes to K8s services (ClusterIP)
Scalability Gateway scales horizontally with Kubernetes deployments
Resiliency Circuit breakers, retries, timeouts centralized
Developer-friendly Use Kubernetes CRDs to configure routes/plugins

⚠️ Things to keep in mind

  1. Resource usage — API Gateway Pod should scale with traffic
  2. TLS termination — Let Gateway handle HTTPS
  3. RBAC & Secrets — Secure API Gateway configs
  4. Observability — Enable logging + monitoring on Gateway
  5. Multi-cluster? — Use global API Gateway (Istio or Kong + Kuma)

Summary

API Gateway + Kubernetes = Scalable, Secure, Centralized Microservices Entry Point

You deploy API Gateway inside K8s → expose ONE public endpoint → route/manage ALL microservices traffic from there. This is how modern companies (Netflix, Airbnb, Stripe, etc.) build robust platforms.


🥇 1️⃣ NGINX as API Gateway (Step-by-Step)

Good for small-medium setupsFast, lightweight, easy to understandWorks even outside Kubernetes (on VM/Server)


🛠️ Setup Example

  • You have 2 microservices:

    • UserService (localhost:5001)
    • ProductService (localhost:5002)
  • You want API Gateway to expose:

    • /users → forwards to UserService
    • /products → forwards to ProductService

⚙️ Step 1 — Install Nginx

sudo apt update
sudo apt install nginx

⚙️ Step 2 — Configure Nginx as reverse proxy

Open Nginx config:

sudo nano /etc/nginx/sites-available/api-gateway

Paste this 👇

server {
    listen 80;

    location /users/ {
        proxy_pass http://localhost:5001/;
    }

    location /products/ {
        proxy_pass http://localhost:5002/;
    }
}

⚙️ Step 3 — Enable config & restart Nginx

sudo ln -s /etc/nginx/sites-available/api-gateway /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

⚙️ Step 4 — Test

curl http://localhost/users/    # Routes to UserService
curl http://localhost/products/ # Routes to ProductService

🎉 Done — Nginx is now your basic API Gateway!


Optional Enhancements

Feature Config snippet
Auth auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.htpasswd;
Rate limiting limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
Caching proxy_cache my_cache;
TLS/HTTPS listen 443 ssl; ssl_certificate /path/to/cert;

🥈 2️⃣ Kong Gateway (Step-by-Step)

Production-grade API GatewayAdvanced features (auth, rate-limit, analytics, plugins)Works great inside Kubernetes too


🚀 Kong Gateway — Outside Kubernetes (quick demo)

⚙️ Step 1 — Install Kong (Docker)

docker network create kong-net

docker run -d --name kong-database \
    --network=kong-net \
    -p 5432:5432 \
    -e "POSTGRES_USER=kong" \
    -e "POSTGRES_DB=kong" \
    -e "POSTGRES_PASSWORD=kong" \
    postgres:13

docker run --rm --network=kong-net \
    -e "KONG_DATABASE=postgres" \
    -e "KONG_PG_HOST=kong-database" \
    kong:3.6 kong migrations bootstrap

docker run -d --name kong \
    --network=kong-net \
    -e "KONG_DATABASE=postgres" \
    -e "KONG_PG_HOST=kong-database" \
    -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
    -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
    -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
    -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
    -e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \
    -p 8000:8000 -p 8001:8001 \
    kong:3.6

⚙️ Step 2 — Configure Routes & Services

✅ Expose UserService (on localhost:5001)

curl -i -X POST http://localhost:8001/services/ \
  --data "name=users-service" \
  --data "url=http://host.docker.internal:5001"

curl -i -X POST http://localhost:8001/services/users-service/routes \
  --data "paths[]=/users"

✅ Expose ProductService (on localhost:5002)

curl -i -X POST http://localhost:8001/services/ \
  --data "name=products-service" \
  --data "url=http://host.docker.internal:5002"

curl -i -X POST http://localhost:8001/services/products-service/routes \
  --data "paths[]=/products"

⚙️ **Step 3 — Test

curl http://localhost:8000/users    # Routed to UserService
curl http://localhost:8000/products # Routed to ProductService

🎉 Kong Gateway is live!


Optional Features (1-liner plugins)

Feature Command
JWT Auth curl -X POST http://localhost:8001/services/users-service/plugins --data "name=jwt"
Rate limiting curl -X POST http://localhost:8001/services/users-service/plugins --data "name=rate-limiting" --data "config.minute=5"
Monitoring curl -X POST http://localhost:8001/plugins/ --data "name=prometheus"

🚀 Bonus: Kong Gateway in Kubernetes

  • Install via Helm chart
  • Expose via Kubernetes LoadBalancer
  • Manage routes via KongIngress CRDs

Summary

Gateway Pros Cons
NGINX Simple, Fast, Lightweight, Easy No built-in auth/rate-limit/analytics (need manual config)
Kong Enterprise-grade, Plugins, Analytics, API management UI Heavier, Needs DB (Postgres), Slightly more complex

🥇 1️⃣ NGINX as API Gateway (Step-by-Step)

Good for small-medium setupsFast, lightweight, easy to understandWorks even outside Kubernetes (on VM/Server)


🛠️ Setup Example

  • You have 2 microservices:

    • UserService (localhost:5001)
    • ProductService (localhost:5002)
  • You want API Gateway to expose:

    • /users → forwards to UserService
    • /products → forwards to ProductService

⚙️ Step 1 — Install Nginx

sudo apt update
sudo apt install nginx

⚙️ Step 2 — Configure Nginx as reverse proxy

Open Nginx config:

sudo nano /etc/nginx/sites-available/api-gateway

Paste this 👇

server {
    listen 80;

    location /users/ {
        proxy_pass http://localhost:5001/;
    }

    location /products/ {
        proxy_pass http://localhost:5002/;
    }
}

⚙️ Step 3 — Enable config & restart Nginx

sudo ln -s /etc/nginx/sites-available/api-gateway /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

⚙️ Step 4 — Test

curl http://localhost/users/    # Routes to UserService
curl http://localhost/products/ # Routes to ProductService

🎉 Done — Nginx is now your basic API Gateway!


Optional Enhancements

Feature Config snippet
Auth auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.htpasswd;
Rate limiting limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
Caching proxy_cache my_cache;
TLS/HTTPS listen 443 ssl; ssl_certificate /path/to/cert;

🥈 2️⃣ Kong Gateway (Step-by-Step)

Production-grade API GatewayAdvanced features (auth, rate-limit, analytics, plugins)Works great inside Kubernetes too


🚀 Kong Gateway — Outside Kubernetes (quick demo)

⚙️ Step 1 — Install Kong (Docker)

docker network create kong-net

docker run -d --name kong-database \
    --network=kong-net \
    -p 5432:5432 \
    -e "POSTGRES_USER=kong" \
    -e "POSTGRES_DB=kong" \
    -e "POSTGRES_PASSWORD=kong" \
    postgres:13

docker run --rm --network=kong-net \
    -e "KONG_DATABASE=postgres" \
    -e "KONG_PG_HOST=kong-database" \
    kong:3.6 kong migrations bootstrap

docker run -d --name kong \
    --network=kong-net \
    -e "KONG_DATABASE=postgres" \
    -e "KONG_PG_HOST=kong-database" \
    -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
    -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
    -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
    -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
    -e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \
    -p 8000:8000 -p 8001:8001 \
    kong:3.6

⚙️ Step 2 — Configure Routes & Services

✅ Expose UserService (on localhost:5001)

curl -i -X POST http://localhost:8001/services/ \
  --data "name=users-service" \
  --data "url=http://host.docker.internal:5001"

curl -i -X POST http://localhost:8001/services/users-service/routes \
  --data "paths[]=/users"

✅ Expose ProductService (on localhost:5002)

curl -i -X POST http://localhost:8001/services/ \
  --data "name=products-service" \
  --data "url=http://host.docker.internal:5002"

curl -i -X POST http://localhost:8001/services/products-service/routes \
  --data "paths[]=/products"

⚙️ **Step 3 — Test

curl http://localhost:8000/users    # Routed to UserService
curl http://localhost:8000/products # Routed to ProductService

🎉 Kong Gateway is live!


Optional Features (1-liner plugins)

Feature Command
JWT Auth curl -X POST http://localhost:8001/services/users-service/plugins --data "name=jwt"
Rate limiting curl -X POST http://localhost:8001/services/users-service/plugins --data "name=rate-limiting" --data "config.minute=5"
Monitoring curl -X POST http://localhost:8001/plugins/ --data "name=prometheus"

🚀 Bonus: Kong Gateway in Kubernetes

  • Install via Helm chart
  • Expose via Kubernetes LoadBalancer
  • Manage routes via KongIngress CRDs

Summary

Gateway Pros Cons
NGINX Simple, Fast, Lightweight, Easy No built-in auth/rate-limit/analytics (need manual config)
Kong Enterprise-grade, Plugins, Analytics, API management UI Heavier, Needs DB (Postgres), Slightly more complex

🚀 API Gateway vs Ingress Controller — Clear Difference

API Gateway Ingress Controller
Primary Purpose Manage API traffic (L7, HTTP/REST/gRPC)
Security + Routing + API management
Expose Kubernetes services to external users (mainly L7 HTTP/S routes)
Scope API management for internal + external clients (auth, rate limiting, analytics) Ingress traffic only (external → K8s services)
No deep API features
Typical Usage Centralize microservices traffic management Just expose K8s services to outside world
Features Routing, auth, rate limiting, caching, logging, transformations, API versioning Basic routing (hostname/path based), TLS termination
Authentication Built-in (JWT, OAuth2, API key plugins) Usually none (or very minimal via annotations)
Analytics & Monitoring Built-in (dashboards, usage stats, logging) None (need external Prometheus/Grafana setup)
Examples Kong, Apigee, AWS API Gateway, Istio Gateway, Ambassador NGINX Ingress Controller, Traefik, HAProxy Ingress
Traffic Direction External + Internal (client → gateway → internal services) Only external → K8s services
Cost/Complexity Heavier, more features, more resource usage Lightweight, simple to configure
Granularity API-level control (limit /users API to 100 req/sec) Service-level routing (route foo.com → svc A)

🏗️ Architecture Diagrams

📍 Ingress Controller (simple)

Client
   │
[Ingress Controller]
   │
 ┌─┴─────────────┐
Svc A (ClusterIP) Svc B (ClusterIP)
  • Maps host/path → Kubernetes services
  • No auth/rate limit/analytics built-in

📍 API Gateway (richer)

Client
   │
[API Gateway]
   │
 ┌─┴─────────────┐
UserSvc   ProductSvc   OrderSvc
  • Traffic control, auth, rate limiting
  • Can call multiple services, do response transformations
  • Often handles internal AND external traffic

Real-world Analogy (Simple)

Scenario Which to use?
I just want to expose my frontend app (React/Angular) or service API to internet → no API policies Ingress Controller
I want to expose multiple microservices APIs with security, API versioning, rate limits API Gateway

Quick thumb rule

Ingress Controller = Gate to the cluster API Gateway = Manager of API traffic


🏆 Real Company Usage Example

Company How they use
Netflix API Gateway (Zuul/Envoy) to manage all microservice APIs (with auth, rate limit, fallback)
Small startup NGINX Ingress Controller to expose frontend + backend app
Stripe API Gateway to manage secure APIs for developers

Summary

Use case Use Ingress if Use API Gateway if
Simple route Just expose 1–2 services Need smart API routing
Security Handled elsewhere Want built-in auth/rate-limit
Analytics Not required Want usage stats/logging
Complexity Keep things simple Handle complex API traffic

📢 Final Note (Very Important)

👉 Many companies combine both! Ingress Controller → forwards traffic → API Gateway

Example (Best practice):

Client
   │
[NGINX Ingress Controller (TLS termination)]
   │
[API Gateway (Kong)]
   │
[Microservices (ClusterIP)]