--- title: Kubernetes Cluster Autoscaler与HPA协同实践 keywords: - Cluster Autoscaler - HPA - 扩缩容 - 节点组 - 优先级 description: 配置Cluster Autoscaler与HPA协同工作,提供可验证的Deployment/HPA与CA参数,达成稳定的端到端自动伸缩。 date: 2025-11-26 categories: - 文章资讯 - 技术教程 --- 概述 - 目标:在工作负载与节点层实现协同伸缩,避免资源不足或过度扩容,保障成本与性能平衡。 - 适用:负载波动明显的服务,结合HPA与集群节点自动扩缩容。 核心与实战 - HPA示例(CPU驱动): ``` apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: api-hpa namespace: prod spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: api minReplicas: 2 maxReplicas: 50 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 60 ``` - Cluster Autoscaler关键参数(示例,不同云略有差异): ``` --balance-similar-node-groups=true --skip-nodes-with-local-storage=false --expander=least-waste --max-node-provision-time=10m --scale-down-utilization-threshold=0.5 --scale-down-delay-after-add=10m --scale-down-unneeded-time=10m ``` - 节点组标签与优先级: ``` # 为不同节点组打标签以便调度与成本优化 node-group: spot node-group: on-demand ``` 示例 - Deployment与资源请求: ``` apiVersion: apps/v1 kind: Deployment metadata: name: api namespace: prod spec: replicas: 2 selector: matchLabels: { app: api } template: metadata: labels: { app: api } spec: containers: - name: api image: repo/api:1.0.0 resources: requests: cpu: "200m" memory: "256Mi" limits: cpu: "1" memory: "512Mi" ``` - 验证伸缩链路: ``` kubectl get hpa -n prod -w kubectl get nodes -w ``` 验证与监控 - HPA与CA指标: - 观察HPA目标利用率与副本变化;CA事件与节点新增/移除。 - 资源请求与调度: - 合理设置container requests以触发CA;避免requests为0导致CA不生效。 - 成本控制: - 优先扩容低成本节点组(如spot),在可靠性要求下混合策略。 常见误区 - HPA目标设置不合理导致频繁伸缩;需结合稳定窗口与合适阈值。 - 未配置requests导致CA无法感知容量需求;必须设置requests。 - CA参数过于激进导致抖动;需设置合理scale-down延迟与阈值。 结语 - 通过HPA与Cluster Autoscaler协同,可以在负载波动下稳定控制副本与节点规模,实现可预测性能与成本优化。

发表评论 取消回复