整点薯条148 2025-04-01 10:16 采纳率: 0%
浏览 12

kubernetes;HPA

kubenetes hpa部署后使用kubectl get hpa命令一直显示unkonwn
describe后显示如下报错:

Warning FailedComputeMetricsReplicas 12s (x3 over 42s) horizontal-pod-autoscaler invalid metrics (1 invalid out of 1), first error is: failed to get cpu utilization: unable to get metrics for resource cpu: unable to fetch metrics from resource metrics API: an error on the server ("Internal Server Error: "/apis/metrics.k8s.io/v1beta1/namespaces/default/pods?labelSelector=app%3Dnginx": the server could not find the requested resource") has prevented the request from succeeding (get pods.metrics.k8s.io)

  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2025-04-01 10:16
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    Kubernetes HPA部署后使用kubectl get hpa命令报错

    你遇到的问题是,部署了Kubernetes Horizontal Pod Autoscaler(HPA)后,使用kubectl get hpa命令时,显示Unknown状态,并且报错Warning FailedComputeMetricsReplicas

    这是因为HPA需要在Pod中 expose 一个可用的 metrics endpoint,以便HPA可以获取Pod的性能指标。默认情况下,Kubernetes不 expose 任何 metrics endpoint。

    解决方法是:

    1. 在Pod的Deployment中添加一个readinessProbe,以便HPA可以获取Pod的性能指标。
    2. 在Pod的Deployment中添加一个metrics端口,用于 expose metrics endpoint。

    以下是一个简单的示例:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-deployment
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: my-app
      template:
        metadata:
          labels:
            app: my-app
        spec:
          containers:
          - name: my-container
            image: my-image
            ports:
            - containerPort: 80
            readinessProbe:
              httpGet:
                path: /
                port: 80
              initialDelaySeconds: 30
              periodSeconds: 10
            metrics:
              ports:
              - name: metrics
                containerPort: 8080
    

    在上面的示例中,我们添加了一个readinessProbe来检查Pod的 readiness,和一个metrics端口来 expose metrics endpoint。

    然后,你可以使用kubectl get hpa命令来查看HPA的状态。

    评论

报告相同问题?

问题事件

  • 创建了问题 4月1日