我正在尝试使用Deployment
扩展Kubernetes HorizontalPodAutoscaler
,它通过Stackdriver监听自定义指标。
我有一个GKE集群,启用了Stackdriver适配器。我能够将自定义度量标准类型发布到Stackdriver,以下是它在Stackdriver的度量标准资源管理器中显示的方式。
这就是我定义我的HPA
的方式:
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: example-hpa
spec:
minReplicas: 1
maxReplicas: 10
metrics:
- type: External
external:
metricName: custom.googleapis.com|worker_pod_metrics|baz
targetValue: 400
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: test-app-group-1-1
成功创建example-hpa
后,执行kubectl get hpa example-hpa
,始终将TARGETS
显示为<unknown>
,并且永远不会检测自定义指标的值。
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
example-hpa Deployment/test-app-group-1-1 <unknown>/400 1 10 1 18m
我正在使用本地运行的Java客户端来发布我的自定义指标。我已经提供了适当的资源标签,如here(硬编码 - 以便它可以在本地环境中运行而没有问题)。我跟着this document创建了Java客户端。
private static MonitoredResource prepareMonitoredResourceDescriptor() {
Map<String, String> resourceLabels = new HashMap<>();
resourceLabels.put("project_id", "<<<my-project-id>>>);
resourceLabels.put("pod_id", "<my pod UID>");
resourceLabels.put("container_name", "");
resourceLabels.put("zone", "asia-southeast1-b");
resourceLabels.put("cluster_name", "my-cluster");
resourceLabels.put("namespace_id", "mynamespace");
resourceLabels.put("instance_id", "");
return MonitoredResource.newBuilder()
.setType("gke_container")
.putAllLabels(resourceLabels)
.build();
}
我在上述步骤中做错了什么?提前感谢您提供的任何答案!
编辑[决定]:我认为我有一些错误的配置,因为kubectl describe hpa [NAME] --v=9
向我展示了一些403
状态代码,以及我使用type: External
而不是type: Pods
(感谢MWZ为你的答案,指出这个错误)。
我设法通过创建一个新项目,一个新的服务帐户和一个新的GKE集群来解决它(基本上从一开始就是一切)。然后我改变了我的yaml文件如下,正如this document解释的那样。
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: test-app-group-1-1
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1beta1
kind: Deployment
name: test-app-group-1-1
minReplicas: 1
maxReplicas: 5
metrics:
- type: Pods # Earlier this was type: External
pods: # Earlier this was external:
metricName: baz # metricName: custom.googleapis.com|worker_pod_metrics|baz
targetAverageValue: 20
我现在出口为custom.googleapis.com/baz
,而不是custom.googleapis.com/worker_pod_metrics/baz
。此外,现在我明确地为yaml中的HPA指定了namespace
。
由于您可以在Stackdriver GUI中看到自定义指标,因此我猜测指标已正确导出。基于Autoscaling Deployments with Custom Metrics,我认为您错误地定义了HPA用于扩展部署的度量标准。
请尝试使用此YAML:
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: example-hpa
spec:
minReplicas: 1
maxReplicas: 10
metrics:
- type: Pods
pods:
metricName: baz
targetAverageValue: 400
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: test-app-group-1-1
请记住:
HPA使用指标计算平均值并将其与目标平均值进行比较。在application-to-Stackdriver导出示例中,Deployment包含导出度量标准的Pod。以下清单文件描述了一个HorizontalPodAutoscaler对象,该对象根据度量标准的目标平均值来扩展部署。
page above上描述的故障排除步骤也很有用。
旁注从上面HPA使用beta API autoscaling/v2beta1
我在运行kubectl describe hpa [DEPLOYMENT_NAME]
时遇到错误。我跑了kubectl describe hpa [DEPLOYMENT_NAME] --v=9
并得到了JSON的回应。
最好使用一些独特的标签来定位您的指标。现在,根据您的Java客户端中标记的指标,只有pod_id
看起来很独特,由于其无状态特性而无法使用。
因此,我建议您尝试引入部署/指标范围的唯一标识符。
resourceLabels.put("<identifier>", "<could-be-deployment-name>");
在此之后,您可以尝试使用类似以下内容修改HPA:
kind: HorizontalPodAutoscaler
metadata:
name: example-hpa
spec:
minReplicas: 1
maxReplicas: 10
metrics:
- type: External
external:
metricName: custom.googleapis.com|worker_pod_metrics|baz
metricSelector:
matchLabels:
# define labels to target
metric.labels.identifier: <deployment-name>
# scale +1 whenever it crosses multiples of mentioned value
targetAverageValue: "400"
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: test-app-group-1-1
除此之外,这个设置没有问题,应该顺利工作。
帮助程序命令,以查看HPA公开的指标:
kubectl get --raw "/apis/external.metrics.k8s.io/v1beta1/namespaces/default/custom.googleapis.com|worker_pod_metrics|baz" | jq