如何使用KEDA metrics-api触发器?请求解决配置后出现的HPA无效报错问题
Let's break down what's happening here and walk through how to fix it:
The Root Cause
That error message tells us KEDA is trying to create an HPA (HorizontalPodAutoscaler) with a target value of 0, which Kubernetes explicitly blocks—HPA requires a positive threshold to scale against. This usually stems from one of two issues:
- Your metrics API endpoint is returning a
0for thetasksfield you're targeting - The
valueLocationpath in your ScaledObject isn't correctly pointing to a valid numeric value in the API's response
Step-by-Step Fixes
- Verify Your Metrics API Response
First, confirm exactly what data your mock API returns. Run this command from your terminal (or inside a cluster pod to mimic KEDA's network context):
curl http://mockbin.org/bin/9cb318e0-e8d8-4dc5-a18f-f6135a0e2d4a
Check if the tasks field exists and has a positive numeric value (like {"tasks": 5}). If it's 0 or missing, that's the immediate problem—you'll need to update the mock data or adjust your valueLocation to target a valid positive field.
Double-Check
valueLocationSyntax
If your API returns nested JSON (e.g.,{"data": {"tasks": 3}}), yourvalueLocationneeds to use dot notation to traverse the structure (likedata.tasksinstead of justtasks). Make sure the path directly points to a numeric field, not an object or array.Validate Cluster Network Access
KEDA needs to reach your metrics API from within the cluster. Test this by execing into your Nginx pod and running the curl command above:
kubectl exec -it $(kubectl get pods -n tsew -l app=nginx-keda-charlie -o name) -n tsew -- curl http://mockbin.org/bin/9cb318e0-e8d8-4dc5-a18f-f6135a0e2d4a
If the request fails, your cluster might lack external network access. Resolve this by configuring proxy settings, or host the metrics API inside the cluster instead.
- Adjusted ScaledObject Configuration
Assuming your API returns a valid positivetasksvalue, here's a cleaned-up version of your ScaledObject to confirm:
apiVersion: keda.sh/v1alpha1 kind: ScaledObject metadata: name: http-scaledobject namespace: tsew labels: deploymentName: nginx-keda-charlie spec: scaleTargetRef: name: nginx-keda-charlie triggers: - type: metrics-api metadata: metricType: Value targetValue: "1" # Ensure this is a positive number that fits your scaling logic url: "http://mockbin.org/bin/9cb318e0-e8d8-4dc5-a18f-f6135a0e2d4a" valueLocation: 'tasks' # Confirm this path matches your API's JSON structure
After making these changes, refresh your ScaledObject:
kubectl delete scaledobject http-scaledobject -n tsew kubectl apply -f deployment.yaml
内容的提问来源于stack exchange,提问作者Charlie

