Horizontal Pod Autoscaler(HPA)根据指标自动调整 Pod 副本数,是云原生弹性能力的核心组件。
一、HPA 配置示例
yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
behavior:
scaleDown:
stabilizationWindowSeconds: 300二、冷却与稳定性
stabilizationWindowSeconds 防止指标抖动导致频繁扩缩。扩容通常立即执行,缩容则需等待窗口内所有推荐值中的最大值,避免流量回落时误缩容。自定义指标需部署 Prometheus Adapter 或 KEDA。