Kubernetes自动扩缩容不生效,HPA目标显示<unknown>求助
Hey there! I’ve helped tons of Kubernetes newbies work through this exact HPA issue, so let’s break down what’s likely going on and how to fix it.
1. Check if Metrics Server is Running (Most Common Culprit)
HPA v1 (which you’re using with autoscaling/v1) relies on CPU metrics, and the Metrics Server is the core component that collects and exposes these metrics to the cluster. If it’s missing or broken, HPA can’t get the data it needs, hence the <unknown> target.
- First, verify if Metrics Server is deployed:
kubectl get pods -n kube-system | grep metrics-server - If you don’t see any pods here, deploy it. For Minikube, you can enable it with
minikube addons enable metrics-server. For other clusters, usekubectl applywith the official Metrics Server manifest. - If the pod exists but isn’t in
Runningstate, check its logs for errors:
A common fix here is adding thekubectl logs <metrics-server-pod-name> -n kube-system--kubelet-insecure-tlsflag to the Metrics Server container (for clusters with self-signed Kubelet certificates) to bypass TLS validation.
2. Ensure Your Deployment Has CPU Requests
HPA calculates CPU usage as a percentage of the requested CPU for your pods. If your php-apache Deployment doesn’t define resources.requests.cpu, HPA has no baseline to calculate against, so it shows <unknown>.
Check your Deployment’s resource settings:
kubectl describe deployment php-apacheLook for the
Resourcessection—you should see something like:Requests:
cpu: 200mIf it’s missing, update your Deployment to add the CPU request:
apiVersion: apps/v1 kind: Deployment metadata: name: php-apache spec: template: spec: containers: - name: php-apache image: k8s.gcr.io/hpa-example resources: requests: cpu: "200m" # Add this line limits: cpu: "500m"Apply the update with
kubectl apply -f <your-updated-deployment.yaml>.
3. Verify HPA is Linked to the Correct Target
Double-check that your HPA is pointing to the right Deployment (not a Pod, Service, or other resource). Autoscaling/v1 only supports Deployments, ReplicaSets, and ReplicationControllers.
Inspect your HPA configuration:
kubectl describe hpa php-apacheLook for the
TargetRefsection—it should match:TargetRef:
API Version: apps/v1
Kind: Deployment
Name: php-apacheIf the target is incorrect, re-create the HPA with the right reference.
4. Wait for Metrics to Be Collected
Sometimes Metrics Server needs a minute or two to start collecting data from your pods. After fixing the above steps, wait 2-3 minutes, then check if metrics are available:
kubectl top pods
If you see CPU usage for your php-apache pods, HPA should start showing the target value instead of <unknown>.
5. Generate Load to Trigger Scaling
Once HPA is working, you need to create load on the php-apache service to hit the CPU threshold (default is 50% in the official example). Run this command to generate continuous traffic:
kubectl run -i --tty load-generator --rm --image=busybox:1.28 --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://php-apache; done"
After a minute or so, check the HPA status with kubectl get hpa—you should see the replica count increase as CPU usage rises.
内容的提问来源于stack exchange,提问作者Matt The Ninja

