【问题标题】:HPA cannot read metric value (CPU utilization) on GKEHPA 无法读取 GKE 上的指标值(CPU 利用率)
【发布时间】:2020-10-24 12:16:49
【问题描述】:

我正在单个集群上开发 Google Kubernetes Engine。 集群会自动扩展节点数量。 我已经创建了三个部署并使用网站设置了自动扩展策略(工作负载 -> 部署 -> 操作 -> 自动扩展),因此无需手动编写 YAML 配置。 基于官方guide,我没有犯任何错误。

如果您不指定请求,则可以仅基于 资源利用率的绝对值,例如 milliCPUs CPU 利用率。

以下是完整的部署 YAML:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: student
  name: student
  namespace: ulibretto
spec:
  replicas: 1
  selector:
    matchLabels:
      app: student
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: student
    spec:
      containers:
        - env:
            - name: CLUSTER_HOST
              valueFrom:
                configMapKeyRef:
                  key: CLUSTER_HOST
                  name: shared-env-vars
            - name: BIND_HOST
              valueFrom:
                configMapKeyRef:
                  key: BIND_HOST
                  name: shared-env-vars
            - name: TOKEN_TIMEOUT
              valueFrom:
                configMapKeyRef:
                  key: TOKEN_TIMEOUT
                  name: shared-env-vars
          image: gcr.io/ulibretto/github.com/ulibretto/studentservice
          imagePullPolicy: IfNotPresent
          name: studentservice-1
---
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  labels:
    app: student
  name: student-hpa-n3bp
  namespace: ulibretto
spec:
  maxReplicas: 100
  metrics:
    - resource:
        name: cpu
        targetAverageUtilization: 80
      type: Resource
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: student
---
apiVersion: v1
kind: Service
metadata:
  annotations:
    cloud.google.com/neg: '{"ingress":true}'
  labels:
    app: student
  name: student-ingress
  namespace: ulibretto
spec:
  clusterIP: 10.44.5.59
  ports:
    - port: 5000
      protocol: TCP
      targetPort: 5000
  selector:
    app: student
  sessionAffinity: None
  type: ClusterIP

问题在于 HPA 没有看到指标(平均 CPU 利用率),这真的很奇怪(见图)。 HPA cannot read metric value

我错过了什么?

【问题讨论】:

    标签: kubernetes yaml google-kubernetes-engine gcloud


    【解决方案1】:

    已编辑

    你是对的。你不需要像我之前提到的那样在scaleTargetRef: 中指定namespace: ulibretto

    由于您提供了所有 YAML,我能够找到正确的根本原因。

    如果您检查GKE docs,您会在代码中找到注释

        resources:
          # You must specify requests for CPU to autoscale
          # based on CPU utilization
          requests:
            cpu: "250m"
            
    

    您的部署没有指定resource requests。我已经尝试过了(我已经删除了一些部分,因为我无法部署您的容器并更改了 HPA 中的 apiVersion):

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: student
      name: student
      namespace: ulibretto
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: student
      strategy:
        rollingUpdate:
          maxSurge: 25%
          maxUnavailable: 25%
        type: RollingUpdate
      template:
        metadata:
          labels:
            app: student
        spec:
          containers:
          - image: nginx
            imagePullPolicy: IfNotPresent
            name: studentservice-1
            resources:
              requests:
                cpu: "250m"
    ---
    apiVersion: autoscaling/v1
    kind: HorizontalPodAutoscaler
    metadata:
      labels:
        app: student
      name: student-hpa
      namespace: ulibretto
    spec:
      maxReplicas: 100
      minReplicas: 1
      targetCPUUtilizationPercentage: 80
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: student
    
    $ kubectl get all -n ulibretto
    NAME                           READY   STATUS    RESTARTS   AGE
    pod/student-6f797d5888-84xfq   1/1     Running   0          7s
    pod/student-6f797d5888-b7ctq   1/1     Running   0          7s
    pod/student-6f797d5888-fbtmd   1/1     Running   0          7s
    NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
    deployment.apps/student   3/3     3            3           7s
    NAME                                 DESIRED   CURRENT   READY   AGE
    replicaset.apps/student-6f797d5888   3         3         3       7s
    NAME                                              REFERENCE            TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
    horizontalpodautoscaler.autoscaling/student-hpa   Deployment/student   <unknown>/80%   1         100       0          7s
    

    大约 1-5 分钟后,您将收到一些指标。

    $ kubectl get all -n ulibretto
    NAME                           READY   STATUS    RESTARTS   AGE
    pod/student-6f797d5888-84xfq   1/1     Running   0          95s
    pod/student-6f797d5888-b7ctq   1/1     Running   0          95s
    pod/student-6f797d5888-fbtmd   1/1     Running   0          95s
    
    NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
    deployment.apps/student   3/3     3            3           95s
    
    NAME                                 DESIRED   CURRENT   READY   AGE
    replicaset.apps/student-6f797d5888   3         3         3       95s
    
    NAME                                              REFERENCE            TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
    horizontalpodautoscaler.autoscaling/student-hpa   Deployment/student   0%/80%    1         100       3          95s
    

    如果您想使用 CLI 创建 HPA,情况相同:

    $ kubectl autoscale deployment student -n ulibretto --cpu-percent=50 --min=1 --max=100
    horizontalpodautoscaler.autoscaling/student autoscaled
    
    $ kubectl get hpa -n ulibretto
    NAME      REFERENCE            TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
    student   Deployment/student   <unknown>/50%   1         100       0          3s
    

    一段时间后你会收到0%而不是&lt;unknown&gt;

    $ kubectl get all -n ulibretto
    NAME                           READY   STATUS    RESTARTS   AGE
    pod/student-6f797d5888-84xfq   1/1     Running   0          4m4s
    pod/student-6f797d5888-b7ctq   1/1     Running   0          4m4s
    pod/student-6f797d5888-fbtmd   1/1     Running   0          4m4s
    NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
    deployment.apps/student   3/3     3            3           4m5s
    NAME                                 DESIRED   CURRENT   READY   AGE
    replicaset.apps/student-6f797d5888   3         3         3       4m5s
    NAME                                          REFERENCE            TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
    horizontalpodautoscaler.autoscaling/student   Deployment/student   0%/50%    1         100       3          58s
    

    【讨论】:

    • 查看第一篇文章:我刚刚更新了 YAML 配置。我试过了,但如果我使用“autoscaling/v1”(我在您共享的链接上使用示例conf)和“autoscaling/v2beta1”(由用户界面自动创建的版本),“命名空间”似乎是一个未知字段G云)。无论如何,要查看自动缩放,我必须调用“ kubectl get hpa --namespace "ulibretto" ”,因为它不存在于默认命名空间中。
    • 你,你是对的。这是我的错。我将在几分钟内编辑此答案。
    • 好吧,GKE 文档也说:imgur.com/a/5kmA2vk。这就是为什么我没有写部署请求的原因(也因为我无法估计它,我是K8S的新手?)。我要试试你的解决方案。
    • 你救了我。非常感谢您花时间帮助我。 ?
    猜你喜欢
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 2020-07-07
    • 2023-04-09
    • 1970-01-01
    • 2020-04-27
    相关资源
    最近更新 更多