![](/img/trans.png)
[英]Kubernetes HPA not scaling with custom metric using prometheus adapter on istio
[英]Kubernetes AWS Cloudwatch adapter not fetching custom metric value for EKS HPA autoscaling
我正在嘗試通過 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 輪詢指標。 這是通過將此策略附加到節點的 IAM 角色來完成的:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cloudwatch:GetMetricData"
],
"Resource": "*"
}
]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.