繁体   English   中英

Kubernetes HPA 部署找不到目标资源

[英]Kubernetes HPA deployment cannot find target resource

我正在尝试部署我自己的 HPA,但没有成功。 虽然在尝试部署 kubernetes 的官方 PHP 时,它按计划工作。 当我尝试使用 HPA 部署我自己的测试部署时,它不起作用。

比较 2 个 HPA 部署流程——Kubernetes 官方部署 VS My Test 部署:

部署官方 Kubernetes 模板镜像:

$ kubectl run php-apache --image=gcr.io/google_containers/hpa-example --requests=cpu=200m --expose --port=80
service "php-apache" created
deployment "php-apache" created

我自己的测试部署结果

{
   "apiVersion": "autoscaling/v1",
   "kind": "HorizontalPodAutoscaler",
   "metadata": {
       "annotations": {
           "autoscaling.alpha.kubernetes.io/conditions": "[{\"type\":\"AbleToScale\",\"status\":\"False\",\"lastTransitionTime\":\"2019-12-22T20:39:59Z\",\"reason\":\"FailedGetScale\",\"message\":\"the HPA controller was unable to get the target's current scale: deployments/scale.apps \\\"gw-autoscale-t6\\\" not found\"}]"
       },
       "creationTimestamp": "2019-12-22T20:39:44Z",
       "labels": {
           "app": "gw-autoscale-t6"
       },
       "name": "gw-autoscale-t6",
       "namespace": "dev",
       "resourceVersion": "17299134",
       "selfLink": "/apis/autoscaling/v1/namespaces/dev/horizontalpodautoscalers/gw-autoscale-t6",
       "uid": "2f7e014c-24fb-11ea-a4d8-a28620329da6"
   },
   "spec": {
       "maxReplicas": 3,
       "minReplicas": 1,
       "scaleTargetRef": {
           "apiVersion": "apps/v1",
           "kind": "Deployment",
           "name": "gw-autoscale-t6"
       },
       "targetCPUUtilizationPercentage": 80
   },
   "status": {
       "currentReplicas": 0,
       "desiredReplicas": 0
   }
}

我用于上述两个部署(官方部署和我的测试部署)的 HPA 部署 yaml

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: {{ .Values.name }}
  labels:
    app: {{ .Values.name }}
  namespace: {{ .Values.namespace }}
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ .Values.name }}
  minReplicas: {{ .Values.spec.replicaCountMin }}
  maxReplicas: {{ .Values.spec.replicaCountMax }}
  targetCPUUtilizationPercentage: 50
  selector:
    matchLabels:
      app: {{ .Values.name }}
  template:
    metadata:
      labels:
        app: {{ .Values.name }}
        release: {{ .Release.Name }}
        heritage: {{ .Release.Service }}
    spec:
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"

结果:

PHP HPA 正在按计划运行

NAME        REFERENCE              TARGETS  MINPODS   MAXPODS   REPLICAS   AGE
php-apache  Deployment/php-apache  0%/50%    1        3         1          3d7h

测试部署 HPA 不起作用

NAME              REFERENCE                    TARGETS         MINPODS  MAXPODS   REPLICAS   AGE
gw-autoscale-t6   Deployment/gw-autoscale-t6   <unknown>/80%   1        3         0          11m

测试部署 HPA 错误

Name:                                                  gw-autoscale-t6
Namespace:                                             dev
Labels:                                                app=gw-autoscale-t6
Annotations:                                           <none>
CreationTimestamp:                                     Sun, 22 Dec 2019 22:39:44 +0200
Reference:                                             Deployment/gw-autoscale-t6
Metrics:                                               ( current / target )
  resource cpu on pods  (as a percentage of request):  <unknown> / 80%
Min replicas:                                          1
Max replicas:                                          3
Deployment pods:                                       0 current / 0 desired
Conditions:
  Type         Status  Reason          Message
  ----         ------  ------          -------
  AbleToScale  False   FailedGetScale  the HPA controller was unable to get the target's current scale: deployments/scale.apps "gw-autoscale-t6" not found
Events:
  Type     Reason          Age                 From                       Message
  ----     ------          ----                ----                       -------
  Warning  FailedGetScale  27s (x81 over 20m)  horizontal-pod-autoscaler  deployments/scale.apps "gw-autoscale-t6" not found
  • 我还尝试了很多其他类型的部署文件。

  • 我的 Metrics-Service 已安装。

  • 我正在使用 Helm 安装我的部署。

我能做些什么来解决这个问题?


我找到的解决方案是将“资源”属性添加到测试部署 yaml 文件中:

例如:在按 cpu 使用情况扩展的情况下,按照部署文件中的方式使用它。

resources:
    requests:
       cpu: {{ .Values.request.cpu }}
    limits:
       cpu: {{ .Values.limits.cpu }}

我解决它的方法是将 cpu 资源添加到部署的文件中,并只保留必要的 HPA 部署 yaml 字段。

HPA 部署文件

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: {{ .Values.name }}
  labels:
    app: {{ .Values.name }}
  namespace: {{ .Values.namespace }}
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ .Values.name }}
  minReplicas: {{ .Values.spec.replicaCountMin }} 
  maxReplicas: {{ .Values.spec.replicaCountMax }} 
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: {{ .Values.spec.cpuUtil }} 

部署文件 - 资源添加

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: {{ .Values.name }}
  labels:
    app: {{ .Values.name }}
  namespace: {{ .Values.namespace }}
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: {{ .Values.name }}
        release: {{ .Release.Name }}
        heritage: {{ .Release.Service }}
    spec:
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        resources:
          requests:
            cpu: {{ .Values.request.cpu }}
          limits:
            cpu: {{ .Values.limits.cpu }} 

您必须添加以下代码:

resources:
requests:
cpu: {{ .Values.request.cpu }}
limits:
cpu: {{ .Values.limits.cpu }}

希望能帮助到你 :)

部署似乎有问题。

我试图重现这种情况,看起来您正在使用kubectl create -f hpa.yaml从文件创建自动缩放器,该文件查找集群中不存在的Deployment/gw-autoscale-t6

这就是您收到以下错误的原因:

HPA 控制器无法获得目标的当前规模:deployments/scale.apps\\“gw-autoscale-t6” not found”

对于我的测试,我创建了一个 hpa.yaml,应用它并寻找情况:

$ date && kubectl get hpa && kubectl get deployments php-apache 

Tue 31 Dec 2019 10:59:37 AM CET

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

Error from server (NotFound): deployments.extensions "php-apache" not found

它给了我<unknown>作为TARGETS值。 零作为REPLICAS

如您所见,Deployment 本身(我们试图用 hpa.yaml 扩展的那个)还不存在。

创建部署后:

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

service/php-apache created
deployment.apps/php-apache created

我已经等了一分钟让HPA预热并计算一切:

$ kubectl get hpa -o yaml

apiVersion: v1
items:
- apiVersion: autoscaling/v1
  kind: HorizontalPodAutoscaler
  metadata:
    annotations:
      autoscaling.alpha.kubernetes.io/conditions: '[{"type":"AbleToScale","status":"True","lastTransitionTime":"2019-12-31T09:59:47Z","reason":"ScaleDownStabilized","message":"recent
        recommendations were higher than current one, applying the highest recent
        recommendation"},{"type":"ScalingActive","status":"True","lastTransitionTime":"2019-12-31T10:00:33Z","reason":"ValidMetricFound","message":"the
        HPA was able to successfully calculate a replica count from cpu resource utilization
        (percentage of request)"},{"type":"ScalingLimited","status":"False","lastTransitionTime":"2019-12-31T10:00:33Z","reason":"DesiredWithinRange","message":"the
        desired count is within the acceptable range"}]'
      autoscaling.alpha.kubernetes.io/current-metrics: '[{"type":"Resource","resource":{"name":"cpu","currentAverageUtilization":0,"currentAverageValue":"1m"}}]'
    creationTimestamp: "2019-12-31T09:49:16Z"
    name: php-apache
...
 spec:
    maxReplicas: 10
    minReplicas: 1
    scaleTargetRef:
      apiVersion: extensions/v1beta1
      kind: Deployment
      name: php-apache
    targetCPUUtilizationPercentage: 50
  status:
    currentCPUUtilizationPercentage: 0
    currentReplicas: 1
    desiredReplicas: 1

并再次检查:

$ date && kubectl get hpa && kubectl get deployments php-apache 

Tue 31 Dec 2019 11:01:16 AM CET
NAME         REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
php-apache   Deployment/php-apache   0%/50%    1         10        1          12m

NAME         READY   UP-TO-DATE   AVAILABLE   AGE
php-apache   1/1     1            1           97s

所以现在,我可以看到REPLICA的非零值和TARGETS显示正确的值。

希望有帮助:) PS 如果您解决了问题,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM