kubernetes HPA部署找不到目标资源

时间:2019-12-22 21:08:38

标签: node.js kubernetes kubernetes-helm kubectl

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

过程:

kubernetes official command

$ 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

我的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按计划工作

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

我的发布没有

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

我发布的HPA Json

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已安装。

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

该如何解决?


回答问题:

部署结果

{
   "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
   }
}

2 个答案:

答案 0 :(得分:2)

我解决问题的方法是将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 }}

希望它会有所帮助:)

答案 1 :(得分:0)

部署似乎有问题。

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

这就是为什么您会收到以下错误:

  

HPA控制器无法获取目标的当前比例:Deployments / scale.apps \   “找不到gw-autoscale-t6”

对于我的测试,我创建了一个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

如您所见,部署本身(我们正在尝试使用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显示了正确的值。

希望有帮助:) 附言让我知道您是否已解决问题。

相关问题