5

我正在尝试为我的 AKS 群集设置 HPA。以下是 Kubernetes 清单文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: XXXXXX\tools\kompose.exe
      convert
    kompose.version: 1.21.0 (992df58d8)
  creationTimestamp: null
  labels:
    io.kompose.service: loginservicedapr
  name: loginservicedapr
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: loginservicedapr
  strategy: {}
  template:
    metadata:
      annotations:
        kompose.cmd: XXXXXX\kompose.exe
          convert
        kompose.version: 1.21.0 (992df58d8)
      creationTimestamp: null
      labels:
        io.kompose.service: loginservicedapr
    spec:
      containers:          
        image: XXXXXXX.azurecr.io/loginservicedapr:latest
        imagePullPolicy: ""
        name: loginservicedapr
        resources:
          requests:
            cpu: 250m
          limits:
            cpu: 500m            
        ports:
        - containerPort: 80
        resources: {}
      restartPolicy: Always
      serviceAccountName: ""
      volumes: null
status: {}
---
apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: XXXXXXXXXX\kompose.exe
      convert
    kompose.version: 1.21.0 (992df58d8)
  creationTimestamp: null
  labels:
    io.kompose.service: loginservicedapr
  name: loginservicedapr
spec:
  type: LoadBalancer
  ports:
  - name: "5016"
    port: 5016
    targetPort: 80
  selector:
    io.kompose.service: loginservicedapr
status:
  loadBalancer: {}

以下是我的 HPA yaml 文件:

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:  
  name: loginservicedapr-hpa
spec:
  maxReplicas: 10 # define max replica count
  minReplicas: 3  # define min replica count
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: loginservicedapr
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50
  - type: Pods
    pods:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50

但是当 HPA 失败并出现错误“FailedGetResourceMetric”-“缺少 CPU 请求”时。

我还使用以下语句安装了 metrics-server(尽管不确定是否需要): kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.6/components.yaml

但是当我执行“kubectl describe hpa”时,我仍然得到以下输出:

Name:                                                  loginservicedapr-hpa
Namespace:                                             default
Labels:                                                fluxcd.io/sync-gc-mark=sha256.Y6dHhIOs-hNYbDmJ25Ijw1YsJ_8f0PH3Vlruj5rfbFk
Annotations:                                           fluxcd.io/sync-checksum: d5c0d9eda6db0c40f1e5e23e1356d0268dbccc8f
                                                       kubectl.kubernetes.io/last-applied-configuration:
                                                         {"apiVersion":"autoscaling/v1","kind":"HorizontalPodAutoscaler","metadata":{"annotations":{"fluxcd.io/sync-checksum":"d5c0d9eda6db0c40f1e5...
CreationTimestamp:                                     Wed, 08 Jul 2020 17:19:47 +0530
Reference:                                             Deployment/loginservicedapr
Metrics:                                               ( current / target )
  resource cpu on pods  (as a percentage of request):  <unknown> / 50%
Min replicas:                                          3
Max replicas:                                          10
Deployment pods:                                       3 current / 3 desired
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: missing request for cpu
Events:
  Type     Reason                        Age                      From                       Message
  ----     ------                        ----                     ----                       -------
  Warning  FailedComputeMetricsReplicas  33m (x1234 over 6h3m)    horizontal-pod-autoscaler  Invalid metrics (1 invalid out of 1), last error was: failed to get cpu utilization: missing request for cpu
  Warning  FailedGetResourceMetric       3m11s (x1340 over 6h3m)  horizontal-pod-autoscaler  missing request for cpu

我与“loginservicedapr”一起部署了另外 2 个服务。但是我还没有为这些服务编写 HPA。但是我在它们的 YAML 文件中也包含了这些服务的资源限制。如何使这个 HPA 工作?

4

3 回答 3

5

resources在您的 pod 规格中出现两次。

        resources:         # once here
          requests:
            cpu: 250m
          limits:
            cpu: 500m            
        ports:
        - containerPort: 80
        resources: {}      # another here, clearing it
于 2020-07-08T18:25:04.267 回答
3

我可以通过在我的 kubernetes 清单文件中更改以下内容来解决此问题:

resources:
          requests:
            cpu: 250m
          limits:
            cpu: 500m 

到以下:

resources:
          requests:
            cpu: "250m"
          limits:
            cpu: "500m" 

HPA 在那之后工作。以下是给出解决方案的 GitHub 链接: https ://github.com/kubernetes-sigs/metrics-server/issues/237 但我没有添加任何内部 IP 地址命令或其他任何内容。

于 2020-07-09T04:38:34.993 回答
2

这通常与指标服务器有关。

确保您没有看到有关度量服务器安装的任何异常:

# This should show you metrics (they come from the metrics server)
$ kubectl top pods
$ kubectl top nodes

或检查日志:

$ kubectl logs <metrics-server-pod>

此外,请检查您的kube-controller-manager日志以获取与 HPA 事件相关的条目。

此外,如果您想进一步了解您的 pod 是否缺少请求/限制,您可以简单地查看 HPA 管理的正在运行的 pod 的完整输出:

$ kubectl get pod <pod-name> -o=yaml

其他一些人也有幸删除和重命名了 HPA

于 2020-07-08T20:12:51.647 回答