如何解决Kubernetes AWS Cloudwatch适配器未获取EKS HPA自动缩放的自定义指标值
我正在尝试通过Kubernetes Cloudwatch适配器基于自定义Cloudwatch指标启用AWS EKS自动缩放。我已将自定义指标推送到AWS Cloudwatch,并验证它们是否出现在Cloudwatch控制台中以及是否可以使用boto3客户端get_metric_data检索到。这是我用来将自定义指标发布到Cloudwatch的代码:
import boto3
from datetime import datetime
client = boto3.client('cloudwatch')
cloudwatch_response = client.put_metric_data(
Namespace='TestMetricNS',MetricData=[
{
'MetricName': 'TotalUnprocessed','Timestamp': datetime.now(),'Value': 40,'Unit': 'Megabytes',}
]
)
我具有以下yaml文件,用于在kubernetes中建立外部指标和hpa自动缩放器:
extMetricCustom.yaml:
apiVersion: metrics.aws/v1alpha1
kind: ExternalMetric
metadata:
name: test-custom-metric
spec:
name: test-custom-metric
resource:
resource: "deployment"
queries:
- id: sqs_test
metricStat:
metric:
namespace: "TestMetricNS"
metricName: "TotalUnprocessed"
period: 60
stat: Average
unit: Megabytes
returnData: true
hpaCustomMetric.yaml
kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2beta1
metadata:
name: test-scaler
spec:
scaleTargetRef:
apiVersion: apps/v1beta1
kind: Deployment
name: sqs-consumer
minReplicas: 1
maxReplicas: 4
metrics:
- type: External
external:
metricName: test-custom-metric
targetAverageValue: 2
当我评估Kubernetes Cloudwatch适配器是否正确获取我的自定义指标(kubectl get hpa)时,它始终显示该指标为0:
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
test-scaler Deployment/sqs-consumer 0/2 (avg) 1 4 1 161m
如何根据Cloudwatch自定义指标正确地自动缩放?
解决方法
与OP一起进行了带外处理,并在当天晚些时候打开了该问题的选项卡,因此将结果张贴在这里,供那些偶然发现该问题的人使用。
此问题的根本原因是时区冲突。指标监控器基于“当前”指标,但是指标生成器脚本的以下代码行在未指定时区的情况下生成时间戳,并且该时间戳也在本地时区。
'Timestamp': datetime.now(),
由于当前时区没有“数据”(由于-X UTC偏移,过去只有X个小时的数据),因此系统未启动缩放,因为其值为“ 0” / nil / null有效。相反,可以指定UTC时间字符串以确保生成的度量标准是及时的:
'Timestamp': datetime.utcnow(),
次要考虑的是Kubernetes节点需要访问权才能从CloudWatch轮询指标。这是通过将this policy附加到节点的IAM角色来完成的:
{
"Version": "2012-10-17","Statement": [
{
"Effect": "Allow","Action": [
"cloudwatch:GetMetricData"
],"Resource": "*"
}
]
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。