This deployment provides a Kubernetes-based solution for serving JSON data via a RESTful API, with automatic hourly refresh capabilities.
- FastAPI: Python RESTful API that generates and serves JSON data
- Nginx: Reverse proxy that forwards requests to the FastAPI backend
- Background Thread: Automatically refreshes the JSON file every hour
- Kubernetes: Container orchestration for deployment and scaling
kernel_service.py: FastAPI application with JSON generation and serving logicrequirements.txt: Python dependenciesnginx.conf: Nginx configuration for reverse proxystart.sh: Startup script that launches both nginx and the APIDockerfile: Container image definition
nginx-deployment.yml: Kubernetes Deployment configurationnginx-service.yml: Kubernetes Service configuration (NodePort on port 30080)
- Automatic JSON Generation: Generates initial JSON if file doesn't exist
- Hourly Refresh: Automatically reloads JSON file every hour
- RESTful API: Multiple endpoints for accessing JSON data
- Health Checks: Built-in health and readiness probes
- Path-based Access: Access nested JSON data via path parameters
GET /: API information and statusGET /health: Health check endpointGET /api/data: Get all JSON dataGET /api/data/{path}: Get specific section by path (e.g.,/api/data/data/items/0)POST /api/refresh: Manually trigger JSON refresh
cd nginx_kuber
docker build -t nginx-json-api:latest .If using a local cluster (like minikube or kind):
# For minikube
minikube image load nginx-json-api:latest
# For kind
kind load docker-image nginx-json-api:latestOr push to a container registry and update the image reference in nginx-deployment.yml.
kubectl apply -f nginx-deployment.yml
kubectl apply -f nginx-service.yml# Check deployment status
kubectl get deployments nginx-json-api
# Check pods
kubectl get pods -l app=nginx-json-api
# Check service
kubectl get svc nginx-json-api
# View logs
kubectl logs -l app=nginx-json-apiIf using NodePort (default):
- Get node IP:
kubectl get nodes -o wide - Access API:
http://<NODE_IP>:30080
Or port-forward:
kubectl port-forward svc/nginx-json-api 8080:80
# Then access at http://localhost:8080Edit the generate_json() function in kernel_service.py to customize the JSON structure.
Modify the sleep duration in the refresh_json_periodically() function (currently 3600 seconds = 1 hour).
The application watches for file changes. If you have an external process that updates /app/data/reference.json, the API will pick it up on the next hourly refresh, or you can trigger a manual refresh via POST /api/refresh.
To persist JSON data across pod restarts, modify nginx-deployment.yml to use a PersistentVolumeClaim instead of emptyDir.
JSON_FILE_PATH: Path to the JSON file (default:/app/data/reference.json)API_PORT: Port for FastAPI backend (default:8000)
- Check pod logs:
kubectl logs -l app=nginx-json-api - Check pod status:
kubectl describe pod -l app=nginx-json-api - Test API directly:
kubectl exec -it <pod-name> -- curl http://localhost:80/health - Verify JSON file:
kubectl exec -it <pod-name> -- cat /app/data/reference.json