[英]which API should i use so as to access the V2beta2PodsMetricStatus Model of kubernetes-client in Python?
[英]How to create_namespaced_horizontal_pod_autoscaler() from AutoscalingV2beta2Api in kubernetes-client in python?
我正在嘗試在 python 中使用kubernetes-client以便在 kubernetes 中創建一個水平 pod 自動縮放器。 為此,我使用了屬於AutoscalingV2beta2Api的create_namespaced_horizontal_pod_autoscaler () 函數。
我的代碼如下:
my_metrics = []
my_metrics.append(client.V2beta2MetricSpec(type='Pods', pods= client.V2beta2PodsMetricSource(metric=client.V2beta2MetricIdentifier(name='cpu'), target=client.V2beta2MetricTarget(average_utilization='50',type='Utilization'))))
body = client.V2beta2HorizontalPodAutoscaler(
api_version='autoscaling/v2beta2',
kind='HorizontalPodAutoscaler',
metadata=client.V1ObjectMeta(name='php-apache'),
spec= client.V2beta2HorizontalPodAutoscalerSpec(
max_replicas=10,
min_replicas=1,
metrics = my_metrics,
scale_target_ref = client.V2beta2CrossVersionObjectReference(kind='Object',name='php-apache')
))
v2 = client.AutoscalingV2beta2Api()
ret = v2.create_namespaced_horizontal_pod_autoscaler(namespace='default', body=body, pretty=True)
我得到的回應是:
HTTP response body: {
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "HorizontalPodAutoscaler in version \"v2beta2\" cannot be handled as a HorizontalPodAutoscaler: v2beta2.HorizontalPodAutoscaler.Spec: v2beta2.HorizontalPodAutoscalerSpec.Metrics: []v2beta2.MetricSpec: v2beta2.MetricSpec.Pods: v2beta2.PodsMetricSource.Target: v2beta2.MetricTarget.AverageUtilization: readUint32: unexpected character: \ufffd, error found in #10 byte of ...|zation\": \"50\", \"type|..., bigger context ...|\"name\": \"cpu\"}, \"target\": {\"averageUtilization\": \"50\", \"type\": \"Utilization\"}}, \"type\": \"Pods\"}], \"m|...",
"reason": "BadRequest",
"code": 400
}
我只想使用 kubernetes-client 調用相當於:
kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
任何工作示例都非常受歡迎。
以下錯誤:
v2beta2.MetricTarget.AverageUtilization: readUint32: unexpected character: \�, error found in #10 byte of ..
意味着它期待Uint32
並且您正在傳遞一個string
:
target=client.V2beta2MetricTarget(average_utilization='50',type='Utilization')
HERE ------^^^^
將其更改為整數值,它應該可以工作。
您可以在python 客戶端文檔中找到的相同信息(注意類型列)。
上面的示例允許通過最新版本的k8s python 客戶端(v12.0.1) 創建基於內存的 hpa。
my_metrics = []
my_metrics.append(client.V2beta2MetricSpec(type='Resource', resource= client.V2beta2ResourceMetricSource(name='memory',target=client.V2beta2MetricTarget(average_utilization= 30,type='Utilization'))))
my_conditions = []
my_conditions.append(client.V2beta2HorizontalPodAutoscalerCondition(status = "True", type = 'AbleToScale'))
status = client.V2beta2HorizontalPodAutoscalerStatus(conditions = my_conditions, current_replicas = 1, desired_replicas = 1)
body = client.V2beta2HorizontalPodAutoscaler(
api_version='autoscaling/v2beta2',
kind='HorizontalPodAutoscaler',
metadata=client.V1ObjectMeta(name=self.app_name),
spec= client.V2beta2HorizontalPodAutoscalerSpec(
max_replicas=self.MAX_PODS,
min_replicas=self.MIN_PODS,
metrics = my_metrics,
scale_target_ref = client.V2beta2CrossVersionObjectReference(kind = 'Deployment', name = self.app_name, api_version = 'apps/v1'),
),
status = status)
v2 = client.AutoscalingV2beta2Api()
ret = v2.create_namespaced_horizontal_pod_autoscaler(namespace='default', body=body, pretty=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.