You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用KEDA metrics-api触发器?请求解决配置后出现的HPA无效报错问题

Fixing HPA "must be positive" Error with KEDA Metrics API Trigger

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 0 for the tasks field you're targeting
  • The valueLocation path in your ScaledObject isn't correctly pointing to a valid numeric value in the API's response

Step-by-Step Fixes

  1. 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.

  1. Double-Check valueLocation Syntax
    If your API returns nested JSON (e.g., {"data": {"tasks": 3}}), your valueLocation needs to use dot notation to traverse the structure (like data.tasks instead of just tasks). Make sure the path directly points to a numeric field, not an object or array.

  2. 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.

  1. Adjusted ScaledObject Configuration
    Assuming your API returns a valid positive tasks value, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 15:17:38