A cloud-native inference API that detects wildlife in images, built to explore how a stateless ML service behaves under load on a self-managed Kubernetes cluster. The focus of this project is systems design: caching, autoscaling, and infrastructure automation, not model accuracy.
Stack: FastAPI · YOLOv8n · Redis · Docker · Kubernetes (kubeadm on GCP VMs) · Ansible · Locust
The API accepts a base64-encoded image and returns bounding boxes and species labels using a YOLOv8 nano model. A Redis cache sits in front of inference so repeated images skip the model entirely. The whole stack, from bare GCP VMs to a running Kubernetes cluster to a deployed, autoscaling application, is provisioned by a single Ansible playbook.
| Endpoint | Description |
|---|---|
POST /api/predict |
Returns detection boxes, labels, and confidence scores for an image |
POST /api/annotate |
Returns the same image with bounding boxes drawn on it |
┌─────────────┐
Client ────────► │ Service │
│ (K8s SVC) │
└──────┬──────┘
│
┌────────────┼────────────┐
▼ ▼ ▼
┌────────┐ ┌────────┐ ┌────────┐
│ Pod 1 │ │ Pod 2 │ │ Pod N │ ◄── HPA: 1-8 replicas,
│FastAPI │ │FastAPI │ │FastAPI │ scales on 70% CPU
│+YOLOv8n│ │+YOLOv8n│ │+YOLOv8n│
└───┬────┘ └───┬────┘ └───┬────┘
└────────────┼────────────┘
▼
┌──────────┐
│ Redis │ ◄── cache-before-decode:
│ Cache │ identical images skip
└──────────┘ inference entirely
3-node cluster (1 master, 2 workers) on GCP, provisioned end-to-end
by Ansible: VM creation -> kubeadm cluster bootstrap -> Flannel CNI
-> app + Redis deployment -> HPA + metrics-server.
Model baked into the Docker image, not on a PersistentVolume. A PV approach was tried first. It added startup latency and made readiness probes unreliable during pod scale-up, since every new pod had to mount and load the model before serving traffic. Baking the model into the image trades a larger image size for fast, predictable pod startup, which matters when the whole point of the project is autoscaling behavior.
Cache-before-decode. Incoming images are hashed before decoding, so a Redis cache hit skips base64 decoding, OpenCV processing, and the YOLO forward pass entirely. This is the single biggest latency win in the system.
Asyncio locks around the cache were tried and reverted. The idea was to prevent a cache stampede (many concurrent requests for the same uncached image all triggering inference at once). In practice the lock overhead made latency worse than the stampede it was meant to prevent, so it was removed. Documented here because the instinct to add it was reasonable, and the benchmark data is what proved it wrong.
Infrastructure as one command. ansible-playbook site.yaml takes a clean GCP project to a running, autoscaling cluster. Getting here required solving stale PKI state on cluster resets, CNI bridge conflicts, SSH known-hosts collisions, and Windows-originated CRLF line endings breaking YAML parsing on the remote hosts.
Benchmarked with Locust across 1, 2, 4, and 8 pod configurations using a rotating set of wildlife images. Rotation matters here: an earlier run with too few images caused Redis to fully warm and made every request look artificially fast, which is a benchmark validity issue rather than a real result.
- Throughput plateaued around 700-770 requests/second across all pod counts. This was traced to Locust's own request-generation capacity as the bottleneck, not the application or
kube-proxy, an important distinction for interpreting the results honestly. - The 1-pod configuration met the target latency criteria (under 500ms average response time).
See benchmarking/plots/ for the latency and throughput charts and benchmarking/results/ for raw Locust CSV output per pod count.
.
├── api.py # FastAPI application
├── Dockerfile # Multi-stage build, model baked in
├── requirements.txt
├── k8s/ # Kubernetes manifests
│ ├── namespace.yaml
│ ├── app-deployment.yaml # health probes, anti-affinity
│ ├── app-service.yaml
│ ├── hpa.yaml # 1-8 replicas, 70% CPU target
│ ├── configmap.yaml
│ ├── redis-deployment.yaml
│ └── redis-service.yaml
├── iac/ # Ansible: bare VM -> running cluster
│ ├── site.yaml
│ └── roles/
│ ├── provision/ # GCP VM creation
│ ├── common/ # base packages, containerd, kubeadm
│ ├── master/ # control plane init, metrics-server
│ └── worker/ # node join
├── benchmarking/
│ ├── locustfile.py
│ ├── plots.py
│ ├── results/ # raw CSV per pod count
│ └── plots/ # latency, RPS, failure rate charts
├── test_images/ # sample images used for load testing
└── docs/
├── sample-detections/ # example annotated output
├── demo-videos/ # video detections
└── yolo-wild-animals-detection.ipynb
Prerequisites: Python 3.10+, Ansible, a GCP project with Compute Engine enabled, and an SSH key pair.
pip install ansible
ansible-galaxy collection install -r iac/requirements.yaml
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.jsonProvision the cluster and deploy the app in one step:
cd iac
ansible-playbook site.yamlThis provisions 1 master and 2 worker VMs on GCP, bootstraps Kubernetes with Flannel, and deploys the app, Redis, and the HPA.
Verify:
ssh -i ~/.ssh/cloudeco_key ubuntu@<master-ip>
kubectl get pods -n cloudeco
kubectl get service -n cloudecoRun load tests from the master node:
source ~/locust-venv/bin/activate
locust -f ~/locustfile.py --host=http://localhost:30080 \
--headless --users 1000 --spawn-rate 25 --run-time 3m --csv=resultsScale manually (disable the HPA first so it doesn't fight your changes):
kubectl delete hpa cloudeco-hpa -n cloudeco
kubectl scale deployment cloudeco-deployment --replicas=<n> -n cloudecoSet via k8s/configmap.yaml:
| Variable | Default | Description |
|---|---|---|
CACHE_TTL |
3600 | Redis cache expiry, seconds |
REDIS_HOST |
redis-service | Redis hostname |
MODEL_CONF |
0.5 | YOLO confidence threshold |