K8s-自动伸缩

Pod的横向伸缩(水平拓展HPA)

Pod的横向伸缩是通过控制器管理Pod的副本数实现的,由Horizontal控制器负责,通过HPA资源管理,调整Pod的数量

自动伸缩过程

  1. 获取被伸缩的对象的Pod度量
  2. 计算需要达到的Pod数量
  3. 更新replicas字段

20240507181027

基于CPU使用率进行自动伸缩

CPU需要进行CPU高峰使用,进行HPA

SHELL

# CPU使用率到30的时候 进行伸缩 
kubectl autoscale deploymengt kubia --cpu-percent=30 --min=l --max=5

YAML

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: php-apache
spec:
# 指定的目标资源
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
minReplicas: 1
maxReplicas: 10
# 要求每个Pod CPU负载50%
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50

20240507182023

垂直拓展(VPA)

Vertical Pod Autoscaler: 工作负载Pod垂直(资源配置)自动伸缩,如自动计算或调整deployment的Pod模板limit/request,依赖业务历史负载指标

---
apiVersion: autoscaling.k8s.io/v1beta2
kind: VerticalPodAutoscaler
metadata:
name: redis-vpa
spec:
targetRef:
apiVersion: "apps/v1"
kind: Deployment
name: redis-master
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-master
labels:
app: redis
spec:
selector:
matchLabels:
app: redis
replicas: 3
template:
metadata:
labels:
app: redis
spec:
containers:
- name: master
image: registry.k8s.io/redis:e2e # or just image: redis
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379