A distributed microservices architecture built with FastAPI, gRPC, and Kubernetes, featuring an API Gateway that routes requests to multiple backend services.
┌─────────────────┐ ┌─────────────────┐
│ API Gateway │ │ Load Balancer │
│ (FastAPI) │◄───┤ (Kubernetes) │
│ Port: 8000 │ │ │
└─────────┬───────┘ └─────────────────┘
│
├─────────────────────────────────┐
│ │
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ Users Service │ │ Jobs Service │
│ (gRPC) │ │ (gRPC) │
│ Port: 50051 │ │ Port: 50052 │
└─────────────────┘ └─────────────────┘
│
▼
┌─────────────────┐
│Pipeline Service │
│ (gRPC) │
│ Port: 50053 │
└─────────────────┘
- Technology: FastAPI with JWT authentication
- Port: 8000
- Purpose: Routes HTTP requests to appropriate microservices via gRPC
- Features:
- JWT token-based authentication
- Request routing and load balancing
- Error handling and response formatting
- Health checks and monitoring
- Technology: gRPC with Protocol Buffers
- Port: 50051
- Purpose: Manages user data and operations
- Endpoints:
CreateUser: Create new usersGetUser: Retrieve user information
- Technology: gRPC with Protocol Buffers
- Port: 50052
- Purpose: Handles job management and processing
- Endpoints:
CreateJob: Create new jobsGetJob: Retrieve job status and information
- Technology: gRPC with Protocol Buffers
- Port: 50053
- Purpose: Manages data pipelines and workflows
- Endpoints:
CreatePipeline: Create new pipelinesGetPipeline: Retrieve pipeline status and information
- Docker and Docker Compose
- Kubernetes cluster (minikube, kind, or cloud provider)
- kubectl CLI tool
- Python 3.9+ (for local development)
git clone <repository-url>
cd microservices-architecture# Build API Gateway
docker build -t api-gateway:latest ./api-gateway
# Build Users Service
docker build -t users-service:latest ./microservices/users
# Build Jobs Service
docker build -t jobs-service:latest ./microservices/jobs
# Build Pipeline Service
docker build -t pipeline-service:latest ./microservices/pipeline# Create namespace
kubectl create namespace microservices
# Create JWT secret
kubectl create secret generic jwt-secret \
--from-literal=JWT_SECRET_KEY=your-secret-key-here \
--from-literal=JWT_ALGORITHM=HS256 \
--from-literal=JWT_EXPIRE_MINUTES=30 \
-n microservices
# Deploy services
kubectl apply -f microservices/users/k8s/
kubectl apply -f microservices/jobs/k8s/
kubectl apply -f microservices/pipeline/k8s/
# Deploy API Gateway
kubectl apply -f api-gateway/k8s/# Get the API Gateway external IP
kubectl get service api-gateway -n microservices
# Or use port forwarding for local access
kubectl port-forward service/api-gateway 8000:80 -n microservicesFirst, obtain a JWT token:
curl -X POST http://localhost:8000/login \
-H "Content-Type: application/json" \
-d '{"username": "admin", "password": "secret"}'Response:
{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
"token_type": "bearer"
}Create User:
curl -X POST http://localhost:8000/users/ \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json" \
-d '{"id": 1, "name": "John Doe", "email": "john@example.com"}'Get User:
curl -X GET http://localhost:8000/users/1 \
-H "Authorization: Bearer YOUR_JWT_TOKEN"Create Job:
curl -X POST http://localhost:8000/jobs/ \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json" \
-d '{"job_name": "Data Processing Job"}'Get Job:
curl -X GET http://localhost:8000/jobs/1 \
-H "Authorization: Bearer YOUR_JWT_TOKEN"Create Pipeline:
curl -X POST http://localhost:8000/pipelines/ \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json" \
-d '{"pipeline_name": "ML Training Pipeline"}'Get Pipeline:
curl -X GET http://localhost:8000/pipelines/1 \
-H "Authorization: Bearer YOUR_JWT_TOKEN"- Start Users Service:
cd microservices/users
pip install -r requirements.txt
python app/main.py- Start Jobs Service:
cd microservices/jobs
pip install -r requirements.txt
python app/main.py- Start Pipeline Service:
cd microservices/pipeline
pip install -r requirements.txt
python app/main.py- Start API Gateway:
cd api-gateway
pip install -r requirements.txt
# Set environment variables
export JWT_SECRET_KEY=your-secret-key-here
export JWT_ALGORITHM=HS256
export JWT_EXPIRE_MINUTES=30
export USER_SERVICE_URL=localhost:50051
export JOBS_SERVICE_URL=localhost:50052
export PIPELINE_SERVICE_URL=localhost:50053
python -m uvicorn app.main:app --host 0.0.0.0 --port 8000 --reloadIf you modify the .proto files, regenerate the Python files:
# For Users Service
cd microservices/users/app
python -m grpc_tools.protoc --python_out=. --grpc_python_out=. users.proto
# For Jobs Service
cd microservices/jobs/app
python -m grpc_tools.protoc --python_out=. --grpc_python_out=. jobs.proto
# For Pipeline Service
cd microservices/pipeline/app
python -m grpc_tools.protoc --python_out=. --grpc_python_out=. pipelines.protoAPI Gateway:
JWT_SECRET_KEY: Secret key for JWT token signingJWT_ALGORITHM: JWT signing algorithm (default: HS256)JWT_EXPIRE_MINUTES: Token expiration time in minutesUSER_SERVICE_URL: Users service gRPC endpointJOBS_SERVICE_URL: Jobs service gRPC endpointPIPELINE_SERVICE_URL: Pipeline service gRPC endpoint
Each service includes:
- Deployment: Defines the container specifications and replicas
- Service: Exposes the service within the cluster
- Ingress: (API Gateway only) External access configuration
- API Gateway:
GET /- Returns service status - gRPC Services: TCP socket health checks on respective ports
All services include:
- Liveness Probe: Checks if the container is running
- Readiness Probe: Checks if the service is ready to accept traffic
-
Import Errors in gRPC Services:
- Ensure the Python path includes the correct directories
- Check that all
_pb2.pyfiles are properly generated
-
Authentication Issues:
- Verify JWT secret is correctly set in Kubernetes secrets
- Check token expiration time
-
Service Discovery Issues:
- Ensure all services are running and accessible
- Check Kubernetes service DNS resolution
- Check Pod Status:
kubectl get pods -n microservices
kubectl describe pod <pod-name> -n microservices- View Logs:
kubectl logs <pod-name> -n microservices- Test Service Connectivity:
kubectl exec -it <pod-name> -n microservices -- curl http://service-name:port