Shumai on AWS EKS with KEDA & Scale-to-Zero On-Demand Workers
This directory contains a complete, production-ready Kubernetes deployment example for running Shumai on Amazon Elastic Kubernetes Service (Amazon EKS), using KEDA (Kubernetes Event-driven Autoscaling) and Scale-to-Zero On-Demand AWS Graviton5 instances for compute-intensive media processing.
The example should also be straightforward to adapt to any standard Kubernetes cluster.
To demonstrate a fully self-hosted setup, it uses Garage as an S3-compatible object storage solution. However, if you're deploying on AWS, consider using Amazon S3 instead of running your own S3-compatible storage server, as it is managed and highly scalable.
Architecture Overview
[ User / Internet ]
│
┌────────────────────┴────────────────────┐
│ (Port 80/3000) │ (Port 3900)
▼ ▼
┌───────────────────────┐ ┌───────────────────────┐
│ shumai-server (ELB) │ │ garage-public (ELB) │
└───────────┬───────────┘ └───────────┬───────────┘
│ │ Direct Browser Uploads
│ ▼ (Presigned URLs)
┌───────────────────┼─────────────────────────────────────────────────────────────────┐
│ General Workload Node (c9g.large On-Demand, ~$0.087/hr) │
│ │ │
│ ▼ │
│ ┌───────────────────────┐ Enqueue DBOS Tasks ┌───────────────────┐ │
│ │ shumai-server ├────────────────────────►│ shumai-postgres │ │
│ │(SHUMAI_WORKERS=agent) │ (task_queue_transcode) │ (pgvector:pg18) │ │
│ │ [Stale Recovery Loop] │ │ │ │
│ └───────────┬───────────┘ └─────────┬─────────┘ │
│ │ Internal S3 Traffic │ │
│ ▼ │ │
│ ┌───────────────────────┐ │ │
│ │ garage-s3 │ │ │
│ │ (dxflrs/garage) │ │ │
│ └───────────▲───────────┘ │ │
└───────────────────┼───────────────────────────────────────────────┼─────────────────┘
│ Read Source / Write Proxies │
│ │ KEDA monitors
│ ▼ dbos.workflow_status
│ ┌───────────────────────────────────────┐
│ │ KEDA Controller: ScaledJob │
│ │ Spawns 1 Job per enqueued task (0..10)│
│ └───────────────────┬───────────────────┘
│ │
│ ▼ Creates K8s Jobs
┌───────────────────┴─────────────────────────────────────────────────────────────────┐
│ Transcode Compute Pool (c9g.xlarge Graviton5 On-Demand, minSize: 0, maxSize: 10) │
│ │
│ ┌──────────────────────────┐ ┌──────────────────────────┐ ┌────────────────────┐ │
│ │ Job: transcode-job-1 │ │ Job: transcode-job-2 │ │Job: transcode-job-N│ │
│ │ (Worker Pod: --once) │ │ (Worker Pod: --once) │ │ (Worker Pod: --once│ │
│ └────────────┬─────────────┘ └────────────┬─────────────┘ └──────────┬─────────┘ │
│ ▼ ▼ ▼ │
│ ┌──────────────────────────┐ ┌──────────────────────────┐ ┌────────────────────┐ │
│ │ Node 1: c9g.xlarge │ │ Node 2: c9g.xlarge │ │ Node N: c9g.xlarge │ │
│ │ (On-Demand) │ │ (On-Demand) │ │ (On-Demand) │ │
│ └──────────────────────────┘ └──────────────────────────┘ └────────────────────┘ │
│ │
│ Scale-to-Zero: When queue is empty, 0 Jobs run and 0 EC2 instances are billed! │
└─────────────────────────────────────────────────────────────────────────────────────┘
Core Design Principles
1. Scale-to-Zero On-Demand Instances (minSize: 0)
Media transcoding with FFmpeg is bursty: hours may pass with zero transcoding activity, followed by sudden batches of 4K video uploads.
- Zero Idle Cost: The transcode node group
eks/eks-cluster.yamlis configured withminSize: 0anddesiredCapacity: 0. When no media transcoding tasks are active, zero transcode EC2 instances exist, costing $0/hr. - No Spot Interruption Risk: Transcoding jobs run on AWS Graviton5 On-Demand instances (
c9g.xlarge, ~$0.174/hr). This eliminates the risk of Spot instances being reclaimed mid-encode by AWS, preventing wasted CPU cycles and avoiding unexpected transcoding delays.
2. KEDA ScaledJob vs. Deployment / StatefulSet
Standard Kubernetes Horizontal Pod Autoscaler (HPA) scales Deployment replicas based on metrics like CPU utilization. However, HPAs on Deployments suffer from severe limitations during video processing:
- When a 1-minute video finishes on Pod A while a 30-minute video is still encoding on Pod B, CPU drops and HPA reduces replica count, which can arbitrarily terminate Pod B mid-encode.
- StatefulSets scale down in strict ordinal order (
pod-4,pod-3, etc.), which would terminatepod-4even ifpod-4is the only pod currently transcoding!
KEDA ScaledJob solves this natively:
- KEDA queries PostgreSQL directly:
SELECT COUNT(*) FROM dbos.workflow_status WHERE queue_name = 'task_queue_transcode' AND status IN ('ENQUEUED', 'PENDING'); - KEDA spawns an independent Kubernetes
Jobfor each enqueued task. - Each Job runs the transcode worker in
--oncemode. - When the job's video finishes transcoding, the pod exits
0and Kubernetes marks the Job completed. - Completing or terminating one job never impacts any other job!
3. Single-Task Worker Execution (--once Mode)
In eks/shumai-transcode.yaml, the worker container runs with bun run /app/apps/transcode/src/index.ts --once:
- Transcode concurrency is strictly set to
1. - The worker claims 1 DBOS workflow task from
task_queue_transcode. - As soon as that task finishes (success, failure, or cancellation), the worker initiates graceful DBOS drain and exits with code
0. - Idle Timeout: If a job was spawned but another worker claimed the task first, the worker waits 15 seconds. If no task arrives within 15 seconds, it exits
0cleanly so the pod and node can shut down.
4. Automated Stale-Heartbeat Recovery Sweep
If a worker node crashes abruptly (e.g. host kernel panic or hardware failure):
- Every active worker updates
WorkflowTask.heartbeatin PostgreSQL every 5 seconds. - The single core web server (
shumai-server) runs a lightweight background sweep every 30 seconds (workflowService.startStaleTaskRecovery()). - Any task stuck in
status = 'processing'withheartbeat < now() - 90sand whose DBOS status is'PENDING'is automatically re-enqueued viaclient.resumeWorkflow(task.id, { queueName }). - Resumed workflows skip already completed steps using DBOS durable operation outputs.
- Poison-Pill Protection: If a malformed media file crashes workers repeatedly, after 3 recovery attempts the task and asset are marked as
failedto prevent infinite restart loops.
5. Zombie Worker & Graceful Shutdown Safety
- When Kubernetes drains a node or sends
SIGTERM,DBOS.shutdown({ workflowCompletionTimeoutMS: 290_000 })gives the worker up to 290 seconds to finish its active FFmpeg step and upload proxies to S3. - Heartbeats remain active throughout the shutdown drain period to prevent the web server from prematurely declaring the worker dead.
- If a slow worker resumes execution after ownership was transferred, DBOS raises
DBOSWorkflowConflictError(code 5). Shumai catches this error and avoids overwriting the active task or asset status.
Step-by-Step Deployment Guide
Prerequisites
- AWS CLI installed and configured (
aws configure). eksctlinstalled.kubectlinstalled.helminstalled.
Step 1: Provision the EKS Cluster
Create the cluster using the declarative eks/eks-cluster.yaml configuration:
eksctl create cluster -f eks/eks-cluster.yaml
This provisions:
- An EKS 1.31 control plane across multiple AZs.
shumai-general: 1xc9g.largeOn-Demand instance (labeledworkload: general) for API, database, and storage.shumai-transcode: Scale-to-zeroc9g.xlargeOn-Demand instance pool (minSize: 0,maxSize: 10,desiredCapacity: 0, tainteddedicated=transcode:NoSchedule).- AWS EBS CSI Driver with IAM OIDC roles.
Step 2: Install KEDA
Install KEDA using Helm:
helm repo add kedacore https://kedacore.github.io/charts
helm repo update
helm install keda kedacore/keda --namespace keda --create-namespace
Verify KEDA operator is running:
kubectl get pods -n keda
Step 3: Deploy the Shumai Stack
Deploy all components with Kustomize:
kubectl apply -k eks/
Verify initial core pods on the general node reach Running state:
kubectl get pods -n shumai -o wide
Step 4: Configure Public Access & S3 Endpoint
Retrieve the external LoadBalancer hostnames created by AWS:
kubectl get svc -n shumai
NAME TYPE EXTERNAL-IP PORT(S)
shumai-server LoadBalancer k8s-shumai-xxx.us-east-1.elb.amazonaws.com 80:31234/TCP,3000:31235/TCP
garage-public LoadBalancer k8s-garage-xxx.us-east-1.elb.amazonaws.com 3900:31236/TCP
Inject the public Garage ELB hostname into shumai-server:
GARAGE_ELB=$(kubectl get svc garage-public -n shumai -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')
kubectl set env deployment/shumai-server -n shumai AWS_PUBLIC_ENDPOINT_URL_S3="http://${GARAGE_ELB}:3900"
(Note: In production with custom domains, point your DNS to the load balancers and configure AWS_PUBLIC_ENDPOINT_URL_S3: https://s3.mycompany.com in eks/config.yaml.)
Step 5: Deploy Kubernetes Cluster Autoscaler
Deploy Cluster Autoscaler to dynamically provision c9g.xlarge EC2 nodes when KEDA jobs are created:
curl -s https://raw.githubusercontent.com/kubernetes/autoscaler/master/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-autodiscover.yaml \
| sed 's/<YOUR CLUSTER NAME>/shumai-eks/g' \
| kubectl apply -f -
# Pin autoscaler to the general workload node
kubectl patch deployment cluster-autoscaler -n kube-system -p '{"spec":{"template":{"spec":{"nodeSelector":{"workload":"general"}}}}}'
Step 6: Test Scale-to-Zero Queue-Driven Autoscaling
- Open
http://<shumai-server-elb>in your browser and log in with your credentials. - Upload a batch of 5–10 video files simultaneously.
- Observe KEDA detecting the tasks and spawning individual Kubernetes Jobs:
kubectl get scaledjobs -n shumai kubectl get jobs -n shumai -w kubectl get pods -n shumai -l app=shumai-transcode -o wide kubectl get nodes -L role,workload - Watch Cluster Autoscaler provision dedicated
c9g.xlargeEC2 instances for the Jobs. - As each video finishes transcoding, its pod exits
0and completes immediately. - Once all jobs are done, KEDA holds 0 jobs, and Cluster Autoscaler terminates all transcode EC2 instances, returning the transcode pool to 0 nodes ($0/hr)!
Step 7: Cluster Teardown & Cost Cleanup
When you are finished testing, delete all resources to prevent ongoing cloud charges:
# 1. Delete Kubernetes namespace to release ELB load balancers and EBS volumes
kubectl delete namespace shumai
# 2. Delete the EKS cluster and CloudFormation stacks
eksctl delete cluster -f eks/eks-cluster.yaml --wait