以下是 Kubernetes 中 Horizontal Pod Autoscaler (HPA) 和 Vertical Pod Autoscaler (VPA) 的详细 YAML 配置过程及说明:
一、Horizontal Pod Autoscaler (HPA)
1. 前提条件:安装 Metrics Server
HPA 依赖资源指标(如 CPU/内存),需先安装 Metrics Server:
bash
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
2. 示例 Deployment
部署一个需要自动扩缩容的应用:
yaml
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
resources:
requests:
cpu: "100m" # 必须定义资源请求,HPA 才能计算利用率
memory: "128Mi"
limits:
cpu: "200m"
memory: "256Mi"
3. 创建 HPA 规则
基于 CPU 利用率的自动扩缩容:
yaml
# hpa-cpu.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: nginx-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx-deployment
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50 # 当 CPU 使用率超过 50% 时扩容
基于 内存利用率的自动扩缩容:
yaml
# hpa-memory.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: nginx-hpa-memory
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx-deployment
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 70 # 当内存使用率超过 70% 时扩容
4. 验证 HPA
bash
kubectl get hpa # 查看 HPA 状态
kubectl describe hpa nginx-hpa # 查看详细事件
二、Vertical Pod Autoscaler (VPA)
1. 前提条件:安装 VPA
VPA 需要独立组件,安装步骤如下:
bash
# 克隆 VPA 仓库
git clone https://github.com/kubernetes/autoscaler.git
cd autoscaler/vertical-pod-autoscaler
# 安装 VPA 组件
./hack/vpa-up.sh
2. 创建 VPA 规则
yaml
# vpa.yaml
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: nginx-vpa
spec:
targetRef:
apiVersion: "apps/v1"
kind: Deployment
name: nginx-deployment
updatePolicy:
updateMode: "Auto" # 模式:Auto/Off/Initial
resourcePolicy:
containerPolicies:
- containerName: "*" # 匹配所有容器
minAllowed:
cpu: "50m"
memory: "64Mi"
maxAllowed:
cpu: "500m"
memory: "1Gi"
参数说明:
updateMode
:Auto
:自动调整 Pod 资源请求并重启 Pod。Initial
:仅调整新 Pod 的资源请求。Off
:仅提供建议,不自动执行。
minAllowed
/maxAllowed
:资源调整的上下限。
3. 验证 VPA
bash
kubectl describe vpa nginx-vpa # 查看 VPA 建议
kubectl get pods -l app=nginx # 观察 Pod 是否重启(Auto 模式下)
三、HPA + VPA 结合使用注意事项
-
优先级问题:
- HPA 和 VPA 同时作用于同一个 Deployment 时,可能因资源调整冲突导致不稳定。
- 建议优先使用 HPA,VPA 仅在资源请求明显不合理时启用。
-
VPA 的限制:
- VPA 的
Auto
模式会重启 Pod,可能影响服务可用性。 - 不支持 StatefulSet 的所有场景(需谨慎使用)。
- VPA 的
-
资源请求定义:
- 即使使用 VPA,部署时仍需定义初始
resources.requests
,否则 Pod 可能无法调度。
- 即使使用 VPA,部署时仍需定义初始
四、完整流程示例
1. 部署应用
bash
kubectl apply -f deployment.yaml
2. 部署 HPA
bash
kubectl apply -f hpa-cpu.yaml
3. 部署 VPA
bash
kubectl apply -f vpa.yaml
五、常见问题
1. HPA 不生效
-
检查 Metrics Server :
bashkubectl top pods # 若无数据,说明 Metrics Server 未正常工作
-
确认资源请求 :确保 Deployment 中定义了
resources.requests
。
2. VPA 未调整资源
-
查看 VPA 建议 :
bashkubectl describe vpa <vpa-name>
-
检查模式 :确认
updateMode
设置为Auto
。
总结
- HPA:通过调整 Pod 副本数应对流量波动,适合无状态服务。
- VPA:通过调整 Pod 资源请求优化资源利用率,适合资源需求变化大的场景。
- 两者结合时需谨慎,避免资源调整冲突。