GPU-Aware Autoscaler for ML Inference Microservices
I'll build a complete, production-ready GPU-aware autoscaler blueprint for Kubernetes clusters running ML inference microservices (e.g., using KServe, Seldon Core, or Triton Inference Server). This system handles:
Custom scheduler plugin or mutating webhook for GPU bin-packing. Uses First-Fit Decreasing (FFD) for GPU memory shards (e.g., shard a 24GB A100 into 4x6GB for small models).
Custom Autoscaler Controller (Go)
Clone/fork: Use kubebuilder to generate.
go
// main.go (simplified controller-runtime)
package main
import (
"context"
"k8s.io/apimachinery/pkg/util/intstr"
autoscalingv2 "k8s.io/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
// ...
)
type GPUBinPacker struct {
client.Client
}
func (r *GPUBinPacker) binPackPods(pods []corev1.Pod, gpuNodes []corev1.Node) []corev1.Pod {
// Sort pods by GPU mem desc (FFD)
sort.Slice(pods, func(i, j int) bool {
return pods[i].Annotations["gpu-mem-gb"] > pods[j].Annotations["gpu-mem-gb"]
})
for _, pod := range pods {
memGB := parseGPUAnn(pod.Annotations["gpu-mem-gb"])
for _, node := range gpuNodes {
freeShards := getFreeGPUShards(node) // From DCGM metrics
if canFit(freeShards, memGB) {
assignToShard(node, pod, memGB)
break
}
}
}
return pods
}
func canFit(shards []int, size int) bool {
// Bin-packing: find shard >= size
for _, s := range shards {
if s >= size { return true }
}
return false
}
// Reconcile: Scale based on custom metrics (queue length > 10, GPU util > 80%)
func (r *GPUBinPacker) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
// Fetch HPA-like target: scale replicas
// Query Prometheus: queue_length > threshold
// Bin-pack new replicas on GPU nodes
// Apply HPA scale-up/down
}
Deploy as Deployment + RBAC. Use Custom Metrics API for HPA:
This is fully GPU-aware, handles multi-model serving, and scales to 100s of GPUs. Extend with Ray or Volcano for advanced scheduling. Questions? Let's iterate! 🚀