Kubernetes AutoScaler Not Scaling,HPA 显示目标 <unknown>

Posted

技术标签:

【中文标题】Kubernetes AutoScaler Not Scaling,HPA 显示目标 <unknown>【英文标题】:Kubernetes AutoScaler Not Scaling, HPA shows target <unknown> 【发布时间】:2018-09-24 23:24:41 【问题描述】:

我对 Kubernetes 很陌生,对 docker 不太了解。

我一直在研究这个示例,但我被自动缩放器卡住了(它似乎无法缩放)。

我正在处理这里的示例https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/#step-one-run--expose-php-apache-server

你会在底部找到构建

kubectl create -f https://k8s.io/docs/tasks/run-application/hpa-php-apache.yaml

看起来像这样

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: php-apache
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: php-apache
  minReplicas: 1
  maxReplicas: 10
  targetCPUUtilizationPercentage: 50

kubectl get hba 显示

NAME         REFERENCE               TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
php-apache   Deployment/php-apache   <unknown>/50%   1         10        0          7s

提示&lt;unknown&gt; 部分。

然后kubectl describe hba 显示

Name:                                                  php-apache
Namespace:                                             default
Labels:                                                <none>
Annotations:                                           <none>
CreationTimestamp:                                     Sat, 14 Apr 2018 23:05:05 +0100
Reference:                                             Deployment/php-apache
Metrics:                                               ( current / target )
  resource cpu on pods  (as a percentage of request):  <unknown> / 50%
Min replicas:                                          1
Max replicas:                                          10
Conditions:
  Type         Status  Reason          Message
  ----         ------  ------          -------
  AbleToScale  False   FailedGetScale  the HPA controller was unable to get the target's current scale: deployments/scale.extensions "php-apache" not found
Events:
  Type     Reason          Age                From                       Message
  ----     ------          ----               ----                       -------
  Warning  FailedGetScale  12s (x14 over 6m)  horizontal-pod-autoscaler  deployments/scale.extensions "php-apache" not found

然后我手动添加它...

kubectl run php-apache --image=k8s.gcr.io/hpa-example --requests=cpu=200m --expose --port=80

如果我再次运行kubectl describe hpa,我会收到此错误..

Name:                                                  php-apache
Namespace:                                             default
Labels:                                                <none>
Annotations:                                           <none>
CreationTimestamp:                                     Sat, 14 Apr 2018 23:05:05 +0100
Reference:                                             Deployment/php-apache
Metrics:                                               ( current / target )
  resource cpu on pods  (as a percentage of request):  <unknown> / 50%
Min replicas:                                          1
Max replicas:                                          10
Conditions:
  Type           Status  Reason                   Message
  ----           ------  ------                   -------
  AbleToScale    True    SucceededGetScale        the HPA controller was able to get the target's current scale
  ScalingActive  False   FailedGetResourceMetric  the HPA was unable to compute the replica count: unable to get metrics for resource cpu: unable to fetch metrics from API: the server could not find the requested resource (get pods.metrics.k8s.io)
Events:
  Type     Reason                        Age                From                       Message
  ----     ------                        ----               ----                       -------
  Warning  FailedGetScale                1m (x21 over 11m)  horizontal-pod-autoscaler  deployments/scale.extensions "php-apache" not found
  Warning  FailedGetResourceMetric       8s (x2 over 38s)   horizontal-pod-autoscaler  unable to get metrics for resource cpu: unable to fetch metrics from API: the server could not find the requested resource (get pods.metrics.k8s.io)
  Warning  FailedComputeMetricsReplicas  8s (x2 over 38s)   horizontal-pod-autoscaler  failed to get cpu utilization: unable to get metrics for resource cpu: unable to fetch metrics from API: the server could not find the requested resource (get pods.metrics.k8s.io)

【问题讨论】:

您的集群中是否已启动并运行Heapster?您是否可以使用其他命令(例如 kubectl top pods)检索 Pod CPU 利用率? 是的,全新的 Stackpoint 集群和 heapster 正在运行 我遇到了类似的问题。这解决了我的问题:***.com/q/54106725/2291510 【参考方案1】:

由于 Heapster 在 kubernetes 的更高版本(v 1.13)中已弃用,您也可以使用 metrics-server 公开您的指标,请查看以下答案以获取设置 HPA 的分步说明:

How to Enable KubeAPI server for HPA Autoscaling Metrics

【讨论】:

【参考方案2】:

直接回答这个问题,如果你设置了一个 HPA 资源并使用了指标服务器,并且遇到了这个错误。

此错误意味着在您的 pod 中,可能有多个容器,其中一个或两个容器没有定义资源请求:

Resources:
     requests:
       cpu: <this is missing! Add it>

检查所有容器是否都定义了资源请求。

要直接添加编辑和添加资源,请使用 kubectl edit &lt;resource&gt;

如果您使用 helm,即使带有 force 标志,Kubernetes 也可能不允许您更新。

注意,如果您没有设置PodDisruptionBudgets,这可能会导致停机,因此请在运行编辑之前设置它们

【讨论】:

不应该是资源限制而不是请求吗?【参考方案3】:

需要两件事来确保您的 HPA 正常运行

    指标服务器正在运行 可扩展资源(Pod/Statefulset)定义了 CPU/内存资源请求和限制。 (仅在 cpu/mem 范围内讨论,而其他自定义指标可以启用以供 HPA 使用)

检查指标服务器是否正在运行 -

kubectl get po -n kube-system | grep metric

如果您没有列出任何内容,则您的指标服务器尚未运行。 请参阅Readme 并安装指标服务器。除非您的指标服务正在运行,否则添加 CPU 限制将无济于事。

现在确保您在 pod 或可扩展目标中定义了资源请求和限制。

 resources:
  requests:
    memory: "64Mi"
    cpu: "250m"
  limits:
    memory: "128Mi"
    cpu: "250m"

此外,只有在您根据 cpu/内存进行扩展时,指标服务器才会有用。

【讨论】:

以上是关于Kubernetes AutoScaler Not Scaling,HPA 显示目标 <unknown>的主要内容,如果未能解决你的问题,请参考以下文章

Pod 副本缩减在 Kubernetes Horizo​​ntal Pod Autoscaler 中是如何工作的?

Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Horizontal Pod Autoscaler(HPA)

Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Horizontal Pod Autoscaler(HPA)

kubernetes Autoscaler - 无法获取 loadbalancing.googleapis.com|https|request_count

Kubernetes Pod Horizo​​ntal Autoscaler pod 和对象选择器字段是字符串吗?

16-Kubernetes-Pod控制器详解-Horizontal Pod Autoscaler(HPA)