Skip to content

Horizontal Pod Autoscaler

一句话解释

HPA 是 Kubernetes resource 那个 automatically scales 数字的 Pods in 部署 based on CPU, memory, or 自定义 metrics.

用大白话说

The Horizontal Pod Autoscaler (HPA) is Kubernetes' built-in auto scaler for Pods. "Horizontal" means it scales by adding or removing Pod instances, not by changing the size of existing Pods.

You define an HPA with a target — e.g., "keep CPU usage at 70%". The HPA continuously checks the actual CPU usage. If it is above 70%, it adds more Pods. If it is below 70%, it removes Pods (down to your configured minimum).

The HPA can scale based on CPU, memory, or custom metrics (like requests per second, queue depth). Custom metrics require a custom metrics adapter. The HPA checks metrics every 30 seconds by default and can scale from a minimum to a maximum number of replicas.

示例

bash
# Create an HPA that scales between 2 and 10 Pods
kubectl autoscale deployment my-app --min=2 --max=10 --cpu-percent=70

# Check HPA status
kubectl get hpa my-app

这个 command 创建 HPA for my-app 部署, keeping CPU at 70% 使用 2-10 replicas.

工作原理

  1. The HPA controller reads metrics from the metrics API every 30 seconds.
  2. It calculates the desired replica count based on the target metric.
  3. It updates the Deployment's replica count.
  4. The Deployment creates or removes Pods to match the new count.

常见混淆

  • 容易混淆的对象: Vertical Pod Autoscaler

    关键区别: HPA 添加 or 移除 Pods (horizontal scaling); VPA increases or decreases CPU/memory 每 Pod (vertical scaling). 你 cannot use 两者都 on 相同 resource for 相同 metric.

相关术语

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表