自动扩缩容 #

一、自动扩缩容概述 #

Kubernetes提供多种自动扩缩容机制,实现应用和集群的弹性伸缩。

1.1 扩缩容类型 #

text
自动扩缩容类型
    │
    ├── HPA (Horizontal Pod Autoscaler)
    │   └── Pod水平扩缩容
    │
    ├── VPA (Vertical Pod Autoscaler)
    │   └── Pod垂直扩缩容
    │
    └── CA (Cluster Autoscaler)
        └── 节点自动扩缩容

1.2 扩缩容指标 #

指标类型 说明
CPU CPU使用率
内存 内存使用率
自定义 自定义指标
外部 外部系统指标

二、HPA水平扩缩容 #

2.1 创建HPA #

yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: web-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web-deployment
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70

2.2 多指标HPA #

yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: multi-metric-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web-deployment
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 80
  - type: Pods
    pods:
      metric:
        name: requests-per-second
      target:
        type: AverageValue
        averageValue: 1000

2.3 行为配置 #

yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: behavior-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web-deployment
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
      - type: Percent
        value: 10
        periodSeconds: 60
      - type: Pods
        value: 2
        periodSeconds: 60
      selectPolicy: Min
    scaleUp:
      stabilizationWindowSeconds: 60
      policies:
      - type: Percent
        value: 100
        periodSeconds: 15
      - type: Pods
        value: 4
        periodSeconds: 15
      selectPolicy: Max

2.4 HPA管理 #

bash
# 创建HPA
kubectl apply -f hpa.yaml

# 命令行创建
kubectl autoscale deployment web-deployment --min=2 --max=10 --cpu-percent=70

# 查看HPA
kubectl get hpa

# 查看详情
kubectl describe hpa web-hpa

# 查看HPA状态
kubectl get hpa web-hpa -o yaml

三、VPA垂直扩缩容 #

3.1 安装VPA #

bash
# 下载VPA
git clone https://github.com/kubernetes/autoscaler.git
cd autoscaler/vertical-pod-autoscaler

# 安装VPA
./hack/vpa-up.sh

3.2 创建VPA #

yaml
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: web-vpa
spec:
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web-deployment
  updatePolicy:
    updateMode: "Auto"
  resourcePolicy:
    containerPolicies:
    - containerName: "*"
      minAllowed:
        cpu: 100m
        memory: 128Mi
      maxAllowed:
        cpu: 1
        memory: 1Gi
      controlledResources: ["cpu", "memory"]

3.3 VPA模式 #

模式 说明
Auto 自动更新资源
Recreate 重建Pod更新
Off 仅推荐,不更新

3.4 VPA推荐 #

yaml
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: web-vpa-recommend
spec:
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web-deployment
  updatePolicy:
    updateMode: "Off"
bash
# 查看VPA推荐
kubectl describe vpa web-vpa-recommend

四、Cluster Autoscaler #

4.1 部署CA #

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cluster-autoscaler
  namespace: kube-system
spec:
  replicas: 1
  selector:
    matchLabels:
      app: cluster-autoscaler
  template:
    metadata:
      labels:
        app: cluster-autoscaler
    spec:
      containers:
      - name: cluster-autoscaler
        image: k8s.gcr.io/autoscaling/cluster-autoscaler:v1.25.0
        command:
        - ./cluster-autoscaler
        - --cloud-provider=aws
        - --nodes=1:10:node-group-1
        - --scale-down-unneeded-time=10m
        - --scale-down-delay-after-add=10m
        env:
        - name: AWS_REGION
          value: us-east-1
        resources:
          limits:
            cpu: 100m
            memory: 300Mi
          requests:
            cpu: 100m
            memory: 300Mi

4.2 CA参数 #

参数 说明
–nodes 节点组配置
–scale-down-unneeded-time 缩容等待时间
–scale-down-delay-after-add 扩容后缩容延迟
–expander 扩容策略

4.3 节点组配置 #

yaml
# AWS节点组配置
--nodes=1:10:node-group-1
# 格式:min:max:node-group-name

# 多节点组
--nodes=1:5:node-group-1
--nodes=1:10:node-group-2

五、扩缩容策略 #

5.1 扩容策略 #

text
扩容触发条件
    │
    ├── CPU使用率超过阈值
    │
    ├── 内存使用率超过阈值
    │
    ├── 自定义指标超过阈值
    │
    └── Pod调度失败(CA)

5.2 缩容策略 #

text
缩容触发条件
    │
    ├── CPU使用率低于阈值
    │
    ├── 内存使用率低于阈值
    │
    ├── 节点资源利用率低(CA)
    │
    └── 稳定窗口期过后

5.3 冷却时间 #

yaml
behavior:
  scaleDown:
    stabilizationWindowSeconds: 300
  scaleUp:
    stabilizationWindowSeconds: 60

六、监控扩缩容 #

6.1 查看HPA状态 #

bash
# 查看HPA
kubectl get hpa

# 查看HPA事件
kubectl describe hpa web-hpa

# 查看HPA指标
kubectl get --raw "/apis/autoscaling/v2/namespaces/default/horizontalpodautoscalers/web-hpa"

6.2 查看CA状态 #

bash
# 查看CA日志
kubectl logs -n kube-system -l app=cluster-autoscaler

# 查看节点状态
kubectl get nodes

# 查看CA配置
kubectl get configmap -n kube-system cluster-autoscaler-status -o yaml

七、最佳实践 #

7.1 HPA最佳实践 #

text
HPA建议
    │
    ├── 设置合理的requests
    │   └── HPA基于requests计算使用率
    │
    ├── 设置合理的阈值
    │   └── 避免频繁扩缩容
    │
    ├── 配置冷却时间
    │   └── 防止抖动
    │
    └── 监控扩缩容事件
        └── 及时发现问题

7.2 VPA最佳实践 #

text
VPA建议
    │
    ├── 先使用Off模式
    │   └── 观察推荐值
    │
    ├── 设置合理的范围
    │   └── minAllowed和maxAllowed
    │
    └── 注意与HPA冲突
        └── 避免同时使用CPU指标

7.3 CA最佳实践 #

text
CA建议
    │
    ├── 合理设置节点范围
    │   └── min和max节点数
    │
    ├── 配置Pod优先级
    │   └── 确保重要Pod优先调度
    │
    └── 监控节点状态
        └── 关注节点利用率

八、故障排查 #

8.1 HPA问题 #

bash
# 查看HPA状态
kubectl describe hpa <hpa-name>

# 查看Metrics Server
kubectl get deployment metrics-server -n kube-system

# 查看指标
kubectl top pods

8.2 常见问题 #

问题 原因 解决方案
HPA无法获取指标 Metrics Server未安装 安装Metrics Server
扩容不生效 达到maxReplicas 增加maxReplicas
频繁扩缩容 阈值不合理 调整阈值和冷却时间

九、总结 #

9.1 核心要点 #

组件 说明
HPA Pod水平扩缩容
VPA Pod垂直扩缩容
CA 节点自动扩缩容

9.2 下一步 #

掌握了自动扩缩容后,让我们学习 集群维护,了解Kubernetes集群的运维管理。

最后更新:2026-03-28