Kubernetes弹性伸缩的核心机制
生产环境流量的波动性决定了固定副本数无法兼顾成本与稳定性。Kubernetes Horizontal Pod Autoscaler(HPA)根据观测指标自动调整Pod副本数,在流量高峰扩容保障服务可用,低谷缩容释放资源。HPA不是孤立组件,它依赖Metrics Server采集CPU/内存指标,或Prometheus Adapter提供自定义指标,控制器定期计算目标副本数并驱动Deployment扩缩。
Metrics Server部署与指标采集
Metrics Server是Kubernetes集群指标的核心来源,聚合各节点kubelet的cAdvisor数据,提供Pod级别的CPU和内存使用率。
安装Metrics Server
# 直接应用官方YAML
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
# 如果是自签证书环境,需要添加参数
# 编辑deployment,在args中添加:
# - --kubelet-insecure-tls
# - --kubelet-preferred-address-types=InternalIP
验证指标采集正常
# 查看节点指标
kubectl top node
# 查看Pod指标
kubectl top pod -n production
# 预期输出示例:
# NAME CPU(cores) MEMORY(bytes)
# web-app-7d9f8b6c4-x2kjl 45m 128Mi
如果kubectl top返回error,检查Metrics Server Pod是否Running,以及kubelet的10250端口是否可达。
基础HPA配置:CPU利用率触发
最简单的HPA策略基于CPU使用率。当Pod平均CPU使用率超过阈值时扩容,低于阈值时缩容。
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-app-hpa
namespace: production
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
关键参数说明
– minReplicas:最小副本数,即使流量为零也保持的底线
– maxReplicas:最大副本数,防止无限扩容耗尽集群资源
– averageUtilization:目标CPU利用率,HPA试图维持的实际值
HPA的控制逻辑:当actual/expected > target时扩容,actual/expected < target时缩容。扩容是即时的,缩容有5分钟稳定窗口(由–horizontal-pod-autoscaler-downscale-stabilization控制),避免流量波动导致反复缩扩。
Deployment必须配置resources
HPA依赖Pod的resources.requests计算利用率百分比。没有配置requests的Pod,HPA无法工作:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
template:
spec:
containers:
- name: app
image: web-app:1.0
resources:
requests:
cpu: 200m
memory: 256Mi
limits:
cpu: 500m
memory: 512Mi
多指标HPA:CPU与内存联合触发
单靠CPU指标在内存密集型场景下不够用。Kubernetes v2版本的HPA支持同时配置多个指标:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-app-hpa-multi
namespace: production
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web-app
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
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 10
periodSeconds: 60
scaleUp:
stabilizationWindowSeconds: 0
policies:
- type: Percent
value: 100
periodSeconds: 15
- type: Pods
value: 4
periodSeconds: 60
selectPolicy: Max
多指标时HPA取各指标计算结果的最大值作为目标副本数,即”哪个指标需要更多副本就用哪个”。
自定义指标:基于QPS的弹性伸缩
CPU和内存是间接指标,真正反映负载的是请求量。通过Prometheus Adapter接入自定义指标,实现基于QPS的HPA。
部署Prometheus Adapter
# 添加Helm仓库
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
# 安装Adapter
helm install prometheus-adapter prometheus-community/prometheus-adapter \
--set prometheus.url=http://prometheus-server.monitoring.svc \
--set prometheus.port=9090 \
-n monitoring
配置自定义指标规则
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-adapter-custom-rules
namespace: monitoring
data:
custom-rules.yaml: |
rules:
- seriesQuery: 'http_requests_total{namespace!="",pod!=""}'
resources:
overrides:
namespace: {resource: "namespace"}
pod: {resource: "pod"}
name:
matches: "http_requests_total"
as: "http_requests_per_second"
metricsQuery: |
sum(rate(http_requests_total{<<.LabelMatchers>>}[2m])) by (<<.GroupBy>>)
基于QPS的HPA
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-app-hpa-qps
namespace: production
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web-app
minReplicas: 2
maxReplicas: 20
metrics:
- type: Pods
pods:
metric:
name: http_requests_per_second
target:
type: AverageValue
averageValue: 1000
扩缩容行为精细化控制
Kubernetes 1.18+引入的behavior字段让扩缩容策略更精细可控。
防止扩容过猛
behavior:
scaleUp:
stabilizationWindowSeconds: 0
policies:
- type: Percent
value: 50
periodSeconds: 60
- type: Pods
value: 3
periodSeconds: 60
selectPolicy: Min
防止缩容过快
behavior:
scaleDown:
stabilizationWindowSeconds: 600
policies:
- type: Percent
value: 25
periodSeconds: 120
selectPolicy: Min
HPA排障手册
HPA显示desired replicas为nil
Metrics Server未部署或未就绪,HPA无法获取指标数据:
# 检查Metrics Server
kubectl get pods -n kube-system -l k8s-app=metrics-server
# 检查API注册
kubectl get apiservice v1beta1.metrics.k8s.io
扩缩容未触发
检查Pod是否配置了resources.requests,以及当前指标是否超过阈值:
kubectl describe hpa web-app-hpa -n production
# 输出中的Events会显示扩缩决策过程
缩容抖动
增大stabilizationWindowSeconds,或配置缩容策略的Percent类型限制:
behavior:
scaleDown:
stabilizationWindowSeconds: 600
policies:
- type: Percent
value: 10
periodSeconds: 120
从Metrics Server部署到自定义指标接入,再到扩缩容行为精细化控制,Kubernetes HPA为生产环境提供了完整的弹性伸缩能力。关键在于选择合适的指标类型和策略参数,在响应速度和稳定性之间找到平衡。
原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/kuberneteshpa-zi-dong-kuo-suo-rong-pei-zhi-cong-zhi-biao/