在 AWS EKS 中,HPA(horizo​​ntal-pod-autoscaler)无法获取 CPU 利用率

Jun*_*Jun 3 amazon-web-services kubernetes amazon-eks

作为一个初创公司的后端开发人员,为了第一次应用 Kubernetes,我查找了 AWS 的 EKS 指南,找到了一个很好的文档并按照它进行操作。

该指南的链接如下。 https://aws-eks-web-application.workshop.aws/en/10-intro.html

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

前任。5-2. (选项)添加控制台凭据

在最初的尝试中,我们使用 Option 继续该流程,因为我们在进度申请阶段继续失败,并且正在尝试新事物。

所有过程都很简单,直到“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
Run Code Online (Sandbox Code Playgroud)

为了解决这个问题,我们想了很多办法,但还没有找到合适的解决方案,因为我们对Kubernetis还知之甚少。

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

Bashshell 中使用的所有指令均遵循指南,除了已弃用的错误之外,没有出现严重错误。

我该如何解决这个错误?


烧瓶 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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

入口.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
Run Code Online (Sandbox Code Playgroud)

Har*_*var 6

HPA 作用于 Metrics 服务器数据以扩展 POD 或不扩展 POD。

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
Run Code Online (Sandbox Code Playgroud)

安装: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
Run Code Online (Sandbox Code Playgroud)

如果输出带有资源使用情况,您的指标服务器已启动并正在运行,则 HPA 还存在其他问题。