在 AWS EKS 中,HPA (horizo​​ntal-pod-autoscaler) 未能获得 cpu 利用率

In AWS EKS, HPA (horizontal-pod-autoscaler) failed to get cpu utilization

作为初创公司的后端开发人员,第一次应用Kubernetis,找了AWS的EKS指南,找到了一个很好的文档,就照做了。

link对指南如下。 https://aws-eks-web-application.workshop.aws/en/10-intro.html

在这里,我从 2-1 的 AWS 账户方法开始,我省略了之前的所有选项。

例如。 5-2。 (选项)添加控制台凭据

在最初的尝试中,我们以Option进行了流程,因为我们在progress申请阶段不断失败,正在尝试新事物。

在“10-1。应用 HPA 阶段”之前,所有过程都很简单。

但是,当我通过 kubectl get hpa 命令检查 HPA 状态时,CPU 使用被标记为未知。

向导说再晚一点就可以正常出来了,所以我等了一个小时再试,还是一样。

所以,当我通过 kubectl describe hpa 命令查看状态时,发现由于缺少 cpu 请求而出错,如下所示。

Name:                                                  demo-flask-backend-hpa
Namespace:                                             default
Labels:                                                <none>
Annotations:                                           <none>
CreationTimestamp:                                     Tue, 14 Sep 2021 09:03:53 +0000
Reference:                                             Deployment/demo-flask-backend
Metrics:                                               ( current / target )
  resource cpu on pods  (as a percentage of request):  <unknown> / 30%
Min replicas:                                          1
Max replicas:                                          5
Deployment pods:                                       1 current / 0 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: failed to get cpu utilization: missing request for cpu
Events:
  Type     Reason                        Age   From                       Message
  ----     ------                        ----  ----                       -------
  Warning  FailedGetResourceMetric       5s    horizontal-pod-autoscaler  failed to get cpu utilization: missing request for cpu
  Warning  FailedComputeMetricsReplicas  5s    horizontal-pod-autoscaler  invalid metrics (1 invalid out of 1), first error is: failed to get cpu utilization: missing request for cpu

为了解决这个问题,我们找了很多方法,但是因为我们对Kubernetis的了解还很少,所以一直没有找到合适的解决方案。

目前创建的yaml设置文件如下。

Bashshell 中使用的所有指令都遵循该指南,除不推荐使用的错误外没有严重错误。

我该如何解决这个错误?


flask-hpa.yaml

---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: demo-flask-backend-hpa
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: demo-flask-backend
  minReplicas: 1
  maxReplicas: 5
  targetCPUUtilizationPercentage: 30

flask-deployment.yaml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-flask-backend
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo-flask-backend
  template:
    metadata:
      labels:
        app: demo-flask-backend
    spec:
      containers:
        - name: demo-flask-backend
          image: $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/demo-flask-backend:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
          resources:
            requests:
              cpu: 250m
            limits:
              cpu: 500m

ingress.yaml

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: "backend-ingress"
  namespace: default
  annotations:
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/target-type: ip
spec:
  rules:
    - http:
        paths:
          - path: /contents
            pathType: Prefix
            backend:
              service:
                name: "demo-flask-backend"
                port:
                  number: 8080
          - path: /services
            pathType: Prefix
            backend:
              service:
                name: "demo-nodejs-backend"
                port:
                  number: 8080
          - path: /
            pathType: Prefix
            backend:
              service:
                name: "demo-frontend"
                port:
                   number: 80

您需要 install metrics-server 才能获得 cpu 和内存指标。

HPA 处理 Metrics 服务器数据以缩放 POD。

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

安装:https://docs.aws.amazon.com/eks/latest/userguide/metrics-server.html

在 AWS 中你必须先安装它,在 GKE 中它已经默认安装了。

https://aws.amazon.com/premiumsupport/knowledge-center/eks-metrics-server/

您可以检查指标服务器 运行 或不使用

kubectl top pods

如果出现输出,资源数据和使用情况将是您的指标服务器已启动并且运行 HPA 存在另一个问题。